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

查看:126
本文介绍了图像大小调整使用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,

  • 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)库可以做到这一点?

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天全站免登陆