【问题标题】:C# generating thumbnails filesizeC# 生成缩略图文件大小
【发布时间】:2010-11-26 02:38:17
【问题描述】:

我有以下代码来拍摄图像并生成缩略图。

如何更改质量或压缩以通过编程获得更小的文件?

Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr());

【问题讨论】:

标签: c# thumbnails


【解决方案1】:

如果您确实需要更好地控制生成的缩略图,最好通过手动生成更小尺寸和不同质量的图像来制作自己的缩略图。 GetThumbnailImage 不会给您太多控制权。

请参阅这篇文章了解它是如何完成的。 http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

【讨论】:

    【解决方案2】:

    使用Image.Save 保存thumbNail 时,您可以通过传递EncoderParameter 来指定质量。请参阅:Reducing JPEG Picture Quality using C#

    EncoderParameter epQuality = new EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality,
        (int)numQual.Value);
    
    ...
    newImage.Save(..., iciJpegCodec, epParameters);
    

    【讨论】:

      【解决方案3】:

      您不使用 GetThumbnailImage API:

      protected Stream ResizeImage(string source, int width, int height) {
                  using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(source))
                  using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) 
                  using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp))
                  {
      
                      graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                      graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                      graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                      graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
      
                      graphic.DrawImage(bmp, 0, 0, width, height);
      
                      MemoryStream ms = new MemoryStream();
                      newBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                      return ms;
      
                  }             
              }
      

      【讨论】:

        猜你喜欢
        • 2011-09-03
        • 1970-01-01
        • 2018-10-28
        • 2011-07-16
        • 2013-03-24
        • 2012-08-31
        • 2016-05-03
        • 2011-07-19
        • 1970-01-01
        相关资源
        最近更新 更多