使用 C# 调整图像大小 [英] Image resizing using C#

查看:54
本文介绍了使用 C# 调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数码相机中新鲜出炉的图像通常大于 2-3 MB,因此很难通过电子邮件和其他方式进行传输.这需要调整图像大小(根据文件大小而不是高度或宽度).非常类似于 MS Paint 提供图像大小调整功能.我在图像文件理论方面没有受过良好的教育.如果有人能给我指出以下信息来源,我将不胜感激:

The images that are fresh out from digital cameras are often above the size of 2-3 MB making it difficult for it to get transferred over email and other ways. This requires the image to be resized (in terms of file size and not height or width). Quite similar to MS Paint offering image resizing functionality. I am not well educated on image file theories. I would appreciate if someone can point me towards following information sources:

  • 图像理论(各种图像格式如何工作 jpeg、png、tiff 等)?

  • Image theory (how various image formats work jpeg, png, tiff etc).?

图像在调整大小时如何失去清晰度?有没有

How does the image looses its sharpness when resized? Is there some

是否有任何免费的 .Net(我使用的是 4.0)库可用于执行此操作?如果不能,我可以使用任何使用 com 互操作性的库吗?

Are there any free .Net (I am using 4.0) libraries available for doing this? If not can I use any library using com interoperabilty?

非常感谢,

推荐答案

图像大小调整是 .NET 框架中内置的功能.有几种不同的方法:

Image resizing is functionality is built right into the .NET framework. There are a couple of different approaches:

  • GDI+
  • WIC
  • WPF

这是一个不错的 博文 介绍了它们之间的差异.

Here's a nice blog post covering the differences between them.

以下是 GDI+ 的示例:

Here's an example with GDI+:

public void Resize(string imageFile, string outputFile, double scaleFactor)
{
    using (var srcImage = Image.FromFile(imageFile))
    {
        var newWidth = (int)(srcImage.Width * scaleFactor);
        var newHeight = (int)(srcImage.Height * scaleFactor);
        using (var newImage = new Bitmap(newWidth, newHeight))
        using (var graphics = Graphics.FromImage(newImage))
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
            newImage.Save(outputFile);
        }
    }
}

这篇关于使用 C# 调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆