Q
缩小图像尺寸C#
16
A
回答
41
好吧,你想要什么可以这样:
// 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;
}
然后,你可以这样做:
// First load the image somehow
Image myImage = Image.FromFile(pathToImage, true);
// Save the image with a quality of 50%
SaveJpeg (destImagePath, myImage, 50);
+1
这是一个完美的解决方案,以70%的质量将我的图像尺寸从5.5MB减少到1.7MB。 – 2017-11-17 18:45:13
0
2
请看看这个样本。使用GDI +调整照片大小。希望它可以帮助你。
相关问题
- 1. 缩小图像尺寸imagemagic
- 2. Android - 缩小图像尺寸不会减小尺寸的比例
- 3. Android缩小大图像尺寸
- 4. php - 缩小图像尺寸算法
- 5. 缩小图像以适合ScrollViewer尺寸
- 6. 以KB缩小图像尺寸
- 7. 将标题缩小为图像尺寸
- 8. 如何上传图像缩小尺寸
- 9. 从gridview缩小图像尺寸image
- 10. fabricjs中缩小尺寸的图像pixelated
- 11. 如何使用PDFLib缩小图像尺寸以缩小图像大小?
- 12. 与Div尺寸缩小同步缩小背景图像
- 13. 使用压缩缩小图像大小不改变尺寸
- 14. EWS - 图像大小/尺寸
- 15. 尝试缩小图片尺寸时图片尺寸变大了
- 16. SurfaceView,绘制图像并缩小尺寸,旋转图像
- 17. 使用Silverlight进行图像缩放(缩小尺寸)
- 18. ModalPopupExtender缩略图和全尺寸图像
- 19. 调整大小(缩小尺寸)YUV420sp图像
- 20. 上传时缩小图片尺寸
- 21. HighCharts:缩小图表的尺寸
- 22. UIScroll视图设置初始图像尺寸小于全尺寸
- 23. 缩小尺寸列表1
- 24. 缩小UIImage的尺寸
- 25. Scikit-learn Imputer缩小尺寸
- 26. 缩小JRE的尺寸
- 27. woocommerce使用全尺寸图像,调整大小,缩略图
- 28. 从现有图像创建较小尺寸的缩略图
- 29. 如果图像尺寸未知,请缩略图大小相同
- 30. 如何在不改变图像尺寸的情况下缩小图像大小?
看看http://stackoverflow.com/questions/2933918/reduce-image-size-in-bytes-without-resize-and-quality-lose-in-c – Pratik 2010-11-12 05:37:36