2010-11-12 19 views
16

mio scenario:Ridurre Dimensione di C#

voglio aspettare ridurre la dimensione dell'immagine (10 KB a 3 KB)

+1

dai uno sguardo a http://stackoverflow.com/questions/2933918/reduce-image-size-in-bytes-without-resize-and-quality-lose-in-c – Pratik

risposta

41

Ebbene, ciò che si vuole può essere questo:

// Make sure to include this at the top 
using System.Drawing.Imaging; 

/// <summary> 
/// Saves an image as a jpeg image, with the given quality 
/// </summary> 
/// <param name="path"> Path to which the image would be saved. </param> 
/// <param name="quality"> An integer from 0 to 100, with 100 being the highest quality. </param> 
public static void SaveJpeg (string path, Image img, int quality) 
{ 
    if (quality<0 || quality>100) 
     throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); 

    // Encoder parameter for image quality 
    EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); 
    // JPEG image codec 
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); 
    EncoderParameters encoderParams = new EncoderParameters(1); 
    encoderParams.Param[0] = qualityParam; 
    img.Save(path, jpegCodec, encoderParams); 
} 

/// <summary> 
/// Returns the image codec with the given mime type 
/// </summary> 
private static ImageCodecInfo GetEncoderInfo(string mimeType) 
{ 
    // Get image codecs for all image formats 
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

    // Find the correct image codec 
    for(int i=0; i<codecs.Length; i++) 
     if(codecs[i].MimeType == mimeType) 
      return codecs[i]; 

    return null; 
} 

allora si può fare questo:

// First load the image somehow 
Image myImage = Image.FromFile(pathToImage, true); 
// Save the image with a quality of 50% 
SaveJpeg (destImagePath, myImage, 50); 

Fare riferimento a this post on VB Forums e this blog post.

+1

Questa è una soluzione perfetta, ha ridotto le dimensioni dell'immagine da 5,5 MB a 1,7 MB con il 70% di qualità. –

0

Beh, le immagini non possono magicamente diventare più piccolo, in modo da, o saranno per comprimerlo o ridimensionarlo.

+4

Sì, è possibile. Dalla magia di C#! Ridurre la qualità mantenendo la scala. Non è necessaria alcuna compressione – hofnarwillie

2

Use GDI

Si prega di guardare questo campione. Usa GDI + per ridimensionare la foto. Vorrei che ti possa aiutare.

Problemi correlati