<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sorted Bits &#187; image</title>
	<atom:link href="http://www.sortedbits.com/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sortedbits.com</link>
	<description>objectiveC, C# and more interesting stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Resizing an image in C#</title>
		<link>http://www.sortedbits.com/resizing-an-image-in-c/</link>
		<comments>http://www.sortedbits.com/resizing-an-image-in-c/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 14:15:14 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.depl0y.com/?p=477</guid>
		<description><![CDATA[For a project I am working on, I was looking for a good way to resize images in a few different ways. I have done image resizing before, but it really feels that I am re-inventing the wheel every time I do this. So now I decided [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I am working on, I was looking for a good way to resize images in a few different ways. I have done image resizing before, but it really feels that I am re-inventing the wheel every time I do this. So now I decided to write a nice static method which can resize an Image to certain dimensions and outputs an Image object again.</p>
<p>The method requires 3 parameters:</p>
<p>1. An image object. I hope you know how to load a file in an Image object (hint: FromFile).<br />
2. A Resolution object, being nothing more than a small class I created, which also contains the width and the height.<br />
3. The mode you want to use for resizing the image. I explained the modes in the enum listed below.</p>
<p>It probably isnt the neatest code you&#8217;ve ever seen, so if you have any comments, please let me know. Code and more explanation after the break&#8230;<span id="more-477"></span></p>
<pre>///
/// Method resizes the image so it fits the wanted resolution best.
///
///The image to be resized
///The resolution which the image should be when the method is ready.
///The mode for resizing the image.
public static System.Drawing.Image Resize(System.Drawing.Image image, Resolution targetResolution, ResizeMode resizeMode)
{
	int sourceWidth = image.Width;
	int sourceHeight = image.Height;

	int targetWidth = targetResolution.Width;
	int targetHeight = targetResolution.Height;

	// Supplied image is landscape, while the target resolution is portait OR
	// supplied image is in portait, while the target resolution is in landscape.
	// switch target resolution to match the image.
	if ((sourceWidth &gt; sourceHeight &amp;&amp; targetWidth &lt; targetHeight) || (sourceWidth &lt; sourceHeight &amp;&amp; targetWidth &gt; targetHeight))
	{
		targetWidth = targetResolution.Height;
		targetHeight = targetResolution.Width;
	}

	float ratio = 0;
	float ratioWidth = ((float)targetWidth / (float)sourceWidth);
	float ratioHeight = ((float)targetHeight / (float)sourceHeight);

	if (ratioHeight &lt; ratioWidth)
		ratio = ratioHeight;
	else
		ratio = ratioWidth;

	Bitmap newImage = null;

	switch (resizeMode)
	{
		case ResizeMode.Normal:
		default:
			{
				int destWidth = (int)(sourceWidth * ratio);
				int destHeight = (int)(sourceHeight * ratio);

				newImage = new Bitmap(destWidth, destHeight);
				using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage))
				{
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.DrawImage(image, 0, 0, destWidth, destHeight);
				}

				break;
			}
		case ResizeMode.Crop:
			{
				if (ratioHeight &gt; ratioWidth)
					ratio = ratioHeight;
				else
					ratio = ratioWidth;

				int destWidth = (int)(sourceWidth * ratio);
				int destHeight = (int)(sourceHeight * ratio);

				newImage = new Bitmap(targetWidth, targetHeight);

				int startX = 0;
				int startY = 0;

				if (destWidth &gt; targetWidth)
					startX = 0 - ((destWidth - targetWidth) / 2);

				if (destHeight &gt; targetHeight)
					startY = 0 - ((destHeight - targetHeight) / 2);

				using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage))
				{
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.DrawImage(image, startX, startY, destWidth, destHeight);
				}

				break;
			}
		case ResizeMode.Stretch:
			{
				newImage = new Bitmap(targetWidth, targetHeight);
				using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage))
				{
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.DrawImage(image, 0, 0, targetWidth, targetHeight);
				}
				break;
			}
		case ResizeMode.Fill:
			{
				newImage = new Bitmap(targetWidth, targetHeight);

				int destWidth = (int)(sourceWidth * ratio);
				int destHeight = (int)(sourceHeight * ratio);

				int startX = 0;
				int startY = 0;

				if (destWidth &lt; targetWidth)
					startX = 0 + ((targetWidth - destWidth) / 2);

				if (destHeight &lt; targetHeight)
					startY = 0 + ((targetHeight - destHeight) / 2);

				newImage = new Bitmap(targetWidth, targetHeight);
				using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage))
				{
					graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
					graphics.DrawImage(image, startX, startY, destWidth, destHeight);
				}

				break;
			}
	}

	return (System.Drawing.Image)newImage;
}</pre>
<p>The <strong>resizeMode</strong> parameter is of the type ResizeMode, which is nothing more than a simple enumeration, like this:</p>
<pre>///
/// Modes available for the resize method of images.
///
public enum ResizeMode
{
	///
	/// This will resize images to the resolution nearest to the target resolution. Images can become smaller when using this option
	///
	Normal = 1,
	///
	/// This will stretch an image so it always is the exact dimensions of the target resolution
	///
	Stretch = 2,
	///
	/// This will size an image to the exact dimensions of the target resolution, keeping ratio in mind and cropping parts that can't
	/// fit in the picture.
	///
	Crop = 3,
	///
	/// This will size an image to the exact dimensions of the target resolution, keeping ratio in mind and filling up the image
	/// with black bars when some parts remain empty.
	///
	Fill = 4
}</pre>
<p>And the <strong>targetResolution</strong> parameter, is a simple class containing some properties like <strong>Name</strong>, <strong>Width</strong> and <strong>Height</strong>.</p>
<p>This method seems to do everything I need, if you need anything more, or any information about this code, just email me or reply here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/resizing-an-image-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

