创建缩略图并缩小图像尺寸

发布于 2024-07-16 00:27:55 字数 228 浏览 7 评论 0原文

我有非常大的图像 (jpg),我想编写一个 csharp 程序来循环遍历文件并将每个图像的大小减小 75%。

我尝试了这个:

Image thumbNail = image.GetThumbnailImage(800, 600, null, new IntPtr());

但文件大小仍然很大。

有没有办法创建缩略图并使文件大小小得多?

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%.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟若柳尘 2024-07-23 00:27:55
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));
}

用法:

Image myImg = Image.FromFile(@"C:\Test.jpg");
CompressAndSaveImage(myImg, @"C:\Test2.jpg", 10);

将以10的质量压缩Test.jpg并保存为Test2.jpg。

编辑:作为扩展方法可能会更好:

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);
}

用法:

Image myImg = Image.FromFile(@"C:\Test.jpg");
myImg.SaveCompressed(@"C:\Test2.jpg", 10);
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.

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);
空城之時有危險 2024-07-23 00:27:55

ImageMagick 是一个命令行工具,对于图像处理来说非常强大。 在源图像的纵横比未知或不可靠的情况下,我使用它来调整大图像的大小和创建缩略图。 ImageMagick 能够将图像大小调整为特定的高度或宽度,同时保持图片的原始纵横比。 如果需要,它还可以在图像周围添加空间。 总而言之,它非常强大,并且对处理 .nets 图像 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();

使用scale% 参数,您可以轻松地将图像的大小减小 75%

ImageMagick is 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:

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%

南街女流氓 2024-07-23 00:27:55

压缩你的图像。 对于缩略图,JPEG 就足够了,因为您不追求质量。

Image thumbNail = image.GetThumbnailImage(800, 600, null, new IntPtr());

thumbNail.Save(fileName, ImageFormat.Jpeg);

Compress your image. For thumbnails, JPEG is sufficient, as you're not looking for quality.

Image thumbNail = image.GetThumbnailImage(800, 600, null, new IntPtr());

thumbNail.Save(fileName, ImageFormat.Jpeg);
锦爱 2024-07-23 00:27:55

来自 GetThumbnailImage 的文档:

如果图像包含嵌入的缩略图,则此方法将检索嵌入的缩略图并将其缩放到请求的大小。 如果图像不包含嵌入的缩略图,则此方法通过缩放主图像来创建缩略图。

我建议您使用较小的宽度和高度值。 尝试:

// reduce the size of each image by 75% from original 800x600
Image thumbNail = image..GetThumbnailImage(200, 150, null, IntPtr.Zero);

参见示例代码。

另请阅读文档:

当请求的缩略图图像大小约为 120 x 120 像素时,GetThumbnailImage 方法效果很好。 如果您从嵌入缩略图的图像中请求大缩略图(例如,300 x 300),则缩略图的质量可能会明显下降。 通过调用 DrawImage 方法来缩放主图像(而不是缩放嵌入的缩略图)可能会更好。

我想您可能想看看缩放 API。

From GetThumbnailImage's documentation:

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.

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 sample code.

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.

I think you may want to take a look at the scaling API.

鹿! 2024-07-23 00:27:55

这个方法对我很有帮助。 低图像尺寸,保持纵横比

 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

这些都是需要的

PM> Install-Package WindowsAPICodePack-Core
PM> Install-Package WindowsAPICodePack-Shell

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文