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. php - 縮小圖像尺寸算法
- 4. 從gridview縮小圖像尺寸image
- 5. Android縮小大圖像尺寸
- 6. 縮小圖像以適合ScrollViewer尺寸
- 7. 如何上傳圖像縮小尺寸
- 8. 使用壓縮縮小圖像大小不改變尺寸
- 9. 使用Silverlight進行圖像縮放(縮小尺寸)
- 10. ModalPopupExtender縮略圖和全尺寸圖像
- 11. HighCharts:縮小圖表的尺寸
- 12. 從現有圖像創建較小尺寸的縮略圖
- 13. Scikit-learn Imputer縮小尺寸
- 14. 縮小尺寸列表1
- 15. 縮小JRE的尺寸
- 16. 縮小UIImage的尺寸
- 17. 圖像尺寸,
- 18. 獲取縮放圖像的尺寸
- 19. 當屏幕尺寸減小時自動調整圖像尺寸
- 20. ImageView裁剪和縮放圖像到較小的尺寸
- 21. Rails的縮小圖像尺寸上有些%
- 22. 我該如何縮小圖像的尺寸
- 23. 有沒有辦法將圖像縮小到特定尺寸?
- 24. 將圖像縮小爲135x135的固定尺寸
- 25. 將圖像縮放到swift3中的較小尺寸
- 26. 如何在圖像中縮放各種尺寸的Sprites大小?
- 27. 將圖像大小解壓縮至最大尺寸
- 28. 如何輕鬆將圖像文件壓縮成小尺寸?
- 29. 安卓:如何壓縮圖像與最小尺寸
- 30. 尺寸圖像以適合WebView尺寸
看看http://stackoverflow.com/questions/2933918/reduce-image-size-in-bytes-without-resize-and-quality-lose-in-c – Pratik 2010-11-12 05:37:36