我有以下代码来拍摄图像并生成缩略图。
如何改变质量或压缩以编程方式获得更小的文件?
Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr());
答案 0 :(得分:2)
如果您真的需要更好地控制所生成的缩略图,最好通过手动生成较小尺寸和不同质量的图像来制作自己的缩略图。 GetThumbnailImage不会给你太多控制权。
请参阅此文章了解其完成方式。 http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing
答案 1 :(得分:1)
使用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);
答案 2 :(得分:1)
您不使用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;
}
}