2016-05-27 50 views
2

我使用Graphics.DrawImage(DrawText())將DrawText拖拽到Image中。如何減少C#中的圖像大小?

問題是:我只繪製三個文字,但是原始圖像的大小是:226kb,當Save()〜3.45mb時輸出圖像。它太大了。

圖片尺寸:2732 * 3200

我只循環我的清單textFileSplit,而這個清單隻有三個項目。

這是我所有的代碼保存圖像:

foreach (string text in lstTextFromFile) 
{ 
    count++; 
    if (text == "") continue; 
    Graphics gra = Graphics.FromImage(img); 
    string st = lstImgAdded.Items[k].Text; 
    Bitmap bmp = new Bitmap(@"" + st); 
    bmp = (Bitmap)ResizePanel(bmp, panel2); 
    panel2.BackgroundImage = bmp; 
    Graphics gbmp = Graphics.FromImage(bmp); 
    string[] textFileSplit = text.Split('-'); 
    for (int u = 0; u < textFileSplit.Count(); u++) 
    { 
     myColorLabel = activeLabels[u+1].ForeColor; 
     gbmp.DrawImage(
     DrawText(textFileSplit[u], fontType, myColorLabel, 
      Color.Transparent), 
     Point.Round(StretchImageSize(new Point(activeLabels[u+1].Location.X, activeLabels[u+1].Location.Y), panel2))); 
    } 
    gra.Dispose(); 
    Guid id = Guid.NewGuid(); 
    ScaleImage(bmp, witdhImg, heightImg) 
     .Save(linkLocation + "\\" + id + "." + imgType, 
      ImageFormat.Png); 
} 

ScaleImage()類我試圖保持尺寸一樣出身圖片:

public Image ScaleImage(Image image, int maxWidth, int maxHeight) 
{ 
    var ratioX = (double)maxWidth/image.Width; 
    var ratioY = (double)maxHeight/image.Height; 
    var ratio = Math.Min(ratioX, ratioY); 
    var ratio2 = Math.Max(ratioX, ratioY); 

    var newWidth = (int)(image.Width * ratio); 
    var newHeight = (int)(image.Height * ratio2); 

    var newImage = new Bitmap(newWidth, newHeight); 
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 
    return newImage; 
} 

回答

2

確保設置分辨率和質量的尺度功能:

public Image ScaleImage(Image image, int maxWidth, int maxHeight) 
    { 
     var ratioX = (double)maxWidth/image.Width; 
     var ratioY = (double)maxHeight/image.Height; 
     var ratio = Math.Min(ratioX, ratioY); 
     var ratio2 = Math.Max(ratioX, ratioY); 

     var newWidth = (int)(image.Width * ratio); 
     var newHeight = (int)(image.Height * ratio2); 

     var newImage = var newImage = new Bitmap(newWidth, newHeight, image.PixelFormat); 
     newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(newImage); 
     grPhoto.InterpolationMode = InterpolationMode.High; 

     grPhoto.DrawImage(image, 0, 0, newWidth, newHeight); 

     grPhoto.Dispose(); 
     return newImage;   
    } 

應用低質量的編碼器參數:

ImageCodecInfo pngEncoder = ImageCodecInfo.GetImageDecoders().Where(k=> k.FormatID == ImageFormat.Png.Guid).First(); 
    EncoderParameters encoderParameters; 
    encoderParameters = new EncoderParameters(1); 
    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 60L); 
    ScaleImage(bmp, witdhImg, heightImg) 
    .Save(linkLocation + "\\" + id + "." + imgType, pngEncoder ,encoderParameters); 
+0

我用'高模式'檢查過。 「Hight」與「HighQualityBicubic」沒有區別。 – vanloc

+0

最後一件事,試圖通過EncoderParameters低質量保存()方法,答題更新。 –

+0

這是工作。但之前,我用Image.Png保存它,保持透明。我更新了代碼和背景更改爲黑色。 – vanloc