本文介绍了缩小图像尺寸的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的情况:
我想期待缩小图像尺寸(10 KB到3 KB)
i want to expect reduce image size (10 KB to 3 KB)
推荐答案
好了,你要什么都可以这样:
Well, what you want can be this :
// 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;
}
然后,你可以这样做:
Then you can do this:
// First load the image somehow
Image myImage = Image.FromFile(pathToImage, true);
// Save the image with a quality of 50%
SaveJpeg (destImagePath, myImage, 50);
参阅这个职位上VB论坛并的。
这篇关于缩小图像尺寸的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!