Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

c# 4.0 - Image resizing using C#

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:

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

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

  • 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?

Many thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...