comparison Chronosv2/source/Extensions/ImageExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 using System;
2 using System.Drawing;
3 using System.Drawing.Drawing2D;
4
5 namespace Chronos.Extensions
6 {
7 public static class ImageExtensions
8 {
9 #region · Extensions ·
10
11 /// <summary>
12 /// Resizes the specified image.
13 /// </summary>
14 /// <param name="image">The image.</param>
15 /// <param name="size">The size.</param>
16 /// <returns></returns>
17 public static Image Resize(this Image image, Size size)
18 {
19 Size newSize = new Size(size.Width, size.Height);
20
21 if (image.Size.Width > image.Size.Height)
22 {
23 newSize.Height = (image.Size.Height * size.Width) / image.Size.Width;
24 }
25 else
26 {
27 newSize.Width = (image.Size.Width * size.Height) / image.Size.Height;
28 }
29
30 Rectangle rectangle = new Rectangle(0, 0, newSize.Width, newSize.Height);
31 Image resized = new Bitmap(newSize.Width, newSize.Height, image.PixelFormat);
32
33 using (Graphics graphic = Graphics.FromImage(resized))
34 {
35 graphic.CompositingQuality = CompositingQuality.HighQuality;
36 graphic.SmoothingMode = SmoothingMode.HighQuality;
37 graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
38
39 graphic.DrawImage((System.Drawing.Image)image.Clone(), rectangle);
40 }
41
42 return resized;
43 }
44
45 #endregion
46 }
47 }