缩小图像尺寸C#
我的情景:
我希望减少图像大小(10 KB到3 KB)
那么,你想要的是这样的:
// Make sure to include this at the top using System.Drawing.Imaging;
/// /// Saves an image as a jpeg image, with the given quality /// /// Path to which the image would be saved. /// An integer from 0 to 100, with 100 being the highest quality. 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); } /// /// Returns the image codec with the given mime type /// 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
然后你可以这样做:
// First load the image somehow Image myImage = Image.FromFile(pathToImage, true); // Save the image with a quality of 50% SaveJpeg (destImagePath, myImage, 50);
请参阅VB论坛和此博客文章中的这篇文章 。
使用GDI
请看这个样本。 使用GDI +调整照片大小。 希望它可以帮助你。
好吧,图像不能神奇地变小,所以你要么必须压缩它,要么缩小它。