C# 创建缩略图并缩小图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/684092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create thumbnail and reduce image size
提问by leora
I have very large images (jpg) and i want to write a csharp program to loop through the files and reduce the size of each image by 75%.
我有非常大的图像 (jpg),我想编写一个 csharp 程序来遍历文件并将每个图像的大小减小 75%。
I tried this:
我试过这个:
Image thumbNail = image.GetThumbnailImage(800, 600, null, new IntPtr());
but the file size is still very large.
但文件大小仍然很大。
Is there anyway to create thumbnails and have the filesize be much smaller?
无论如何要创建缩略图并使文件大小小得多?
采纳答案by David Brown
private void CompressAndSaveImage(Image img, string fileName,
long quality) {
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
img.Save(fileName, GetCodecInfo("image/jpeg"), parameters);
}
private static ImageCodecInfo GetCodecInfo(string mimeType) {
foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
if (encoder.MimeType == mimeType)
return encoder;
throw new ArgumentOutOfRangeException(
string.Format("'{0}' not supported", mimeType));
}
Usage:
用法:
Image myImg = Image.FromFile(@"C:\Test.jpg");
CompressAndSaveImage(myImg, @"C:\Test2.jpg", 10);
That will compress Test.jpg with a quality of 10 and save it as Test2.jpg.
这将压缩质量为 10 的 Test.jpg 并将其保存为 Test2.jpg。
EDIT:Might be better as an extension method:
编辑:作为扩展方法可能更好:
private static void SaveCompressed(this Image img, string fileName,
long quality) {
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
img.Save(fileName, GetCodecInfo("image/jpeg"), parameters);
}
Usage:
用法:
Image myImg = Image.FromFile(@"C:\Test.jpg");
myImg.SaveCompressed(@"C:\Test2.jpg", 10);
回答by strager
Compress your image. For thumbnails, JPEG is sufficient, as you're not looking for quality.
压缩您的图像。对于缩略图,JPEG 就足够了,因为您不是在寻找质量。
Image thumbNail = image.GetThumbnailImage(800, 600, null, new IntPtr());
thumbNail.Save(fileName, ImageFormat.Jpeg);
回答by dirkgently
From GetThumbnailImage
's documentation:
FromGetThumbnailImage
的文档:
If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image.
如果 Image 包含嵌入的缩略图图像,则此方法检索嵌入的缩略图并将其缩放到请求的大小。如果 Image 不包含嵌入的缩略图图像,则此方法通过缩放主图像来创建缩略图图像。
I'd suggest you use smaller width and height values. Try:
我建议您使用较小的宽度和高度值。尝试:
// reduce the size of each image by 75% from original 800x600
Image thumbNail = image..GetThumbnailImage(200, 150, null, IntPtr.Zero);
See samplecode.
请参阅示例代码。
Also read the documentation:
另请阅读文档:
The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.
当请求的缩略图图像大小约为 120 x 120 像素时,GetThumbnailImage 方法运行良好。如果您从具有嵌入缩略图的图像中请求大缩略图图像(例如,300 x 300),缩略图图像的质量可能会明显下降。通过调用 DrawImage 方法来缩放主图像(而不是缩放嵌入的缩略图)可能会更好。
I think you may want to take a look at the scaling API.
我想你可能想看看缩放 API。
回答by Dav Evans
ImageMagickis a command line tool which is hugely powerful for doing image manipulation. I've used it for resizing large images and thumbnail creation in circumstances where the aspect ratio of the source image is unknown or is unreliable. ImageMagick is able to resize images to a specific height or width while maintaining the original aspect ratio of your picture. It can also add space around an image if required. All in all very powerful and a nice abstraction from having to deal with .nets Image APIs. To use the imageMagick command line tool from within C# I recommend using the System.Diagnostics.ProcessStartInfo object like so:
ImageMagick是一个命令行工具,在进行图像处理方面非常强大。在源图像的纵横比未知或不可靠的情况下,我使用它来调整大图像的大小和创建缩略图。ImageMagick 能够将图像调整到特定的高度或宽度,同时保持图片的原始纵横比。如果需要,它还可以在图像周围添加空间。总而言之,非常强大,并且是一个很好的抽象,无需处理 .nets Image API。要在 C# 中使用 imageMagick 命令行工具,我建议使用 System.Diagnostics.ProcessStartInfo 对象,如下所示:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\Program Files\ImageMagick-6.5.0-Q16\convert.exe";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Arguments = string.Format("-size x{0} \"{1}\" -thumbnail 200x140 -background transparent -gravity center -extent 200x140 \"{2}\"", heightToResizeTo, originalTempFileLocation, resizedTempFileLocation);
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
Using the scale% paramater you can easily reduce the size of your image by 75%
使用 scale% 参数,您可以轻松地将图像的大小减小 75%
回答by Arun Prasad E S
This method helped me very well. Low image size, maintains aspect ratio
这个方法对我帮助很大。低图像尺寸,保持纵横比
ShellFile shellFile = ShellFile.FromFilePath(ImageWithPath); //original image path
Bitmap shellThumbSmall = shellFile.Thumbnail.ExtraLargeBitmap; //change image size
shellThumbSmall.Save(ImageWithPathThumbnail, ImageFormat.Jpeg); // new image path and format
These are needed
需要这些
PM> Install-Package WindowsAPICodePack-Core
PM> Install-Package WindowsAPICodePack-Shell
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack;