<?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>.NET Tips and Tricks &#187; Graphics</title>
	<atom:link href="http://kossovsky.net/index.php/tag/graphics/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>C# Image Processing with AForge.NET Framework</title>
		<link>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 21:35:20 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[computer vision library]]></category>
		<category><![CDATA[genetic evolution]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[grayscale image]]></category>
		<category><![CDATA[grayscale images]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[image edges]]></category>
		<category><![CDATA[imaging library]]></category>
		<category><![CDATA[Jpg]]></category>
		<category><![CDATA[machine learning library]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Resize]]></category>
		<category><![CDATA[Resizer]]></category>
		<category><![CDATA[robotics kits]]></category>
		<category><![CDATA[robotics library]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[vision computer]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=436</guid>
		<description><![CDATA[AForge.NET is a C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, machine learning, robotics, etc.<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago i found a great framework called AForge.NET.</p>
<p><a title="AForge.NET" href="http://www.aforgenet.com/framework/" target="_blank">AForge.NET</a> is a C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence &#8211; image processing, neural networks, genetic algorithms, machine learning, robotics, etc.</p>
<p><span id="more-436"></span></p>
<p>The framework is published under <a title="GNU LESSER GENERAL PUBLIC LICENSE" href="http://www.gnu.org/licenses/lgpl.html" target="_blank">LGPL v3 license</a> and comprised by the set of libraries and sample applications, which demonstrate their features:</p>
<ul>
<li>AForge.Imaging &#8211; library with image processing routines and filters</li>
<li>AForge.Vision &#8211; computer vision library</li>
<li>AForge.Neuro &#8211; neural networks computation library</li>
<li>AForge.Genetic &#8211; evolution programming library</li>
<li>AForge.MachineLearning &#8211; machine learning library</li>
<li>AForge.Robotics &#8211; library providing support of some robotics kits</li>
<li>AForge.Video &#8211; set of libraries for video processing</li>
</ul>
<p>The framework is provided not only with different libraries and their sources, but with many sample applications, which demonstrate the use of this framework, and with documentation help files, which are provided in HTML Help format.</p>
<p>As you can see this framework contains varied libraries, but as for me i was especially interested in the image processing routines and filters.</p>
<p>Let&#8217;s see  the  CannyEdgeDetector in action.</p>
<pre class="c-sharp" name="code">using AForge.Imaging.Filters;

// Loading some file
using (Bitmap SampleImage = (Bitmap)Image.FromFile("test.jpg"))
{
    // We must convert it to grayscale because
    // the filter accepts 8 bpp grayscale images
    Grayscale GF = new Grayscale(0.2125, 0.7154, 0.0721);
    using (Bitmap GSampleImage = GF.Apply(SampleImage))
    {
        // Saving the grayscale image, so we could see it later
        GSampleImage.SaveAsJpeg("testBW.jpg", 100);

        // Detecting image edges and saving the result
        CannyEdgeDetector CED = new CannyEdgeDetector(0,70);
        CED.ApplyInPlace(GSampleImage);
        GSampleImage.SaveAsJpeg("testEDGED.jpg",100);
    }
}

// SaveAsJpeg is an extension method i wrote to be able to save
// Jpeg images with parametrized quality
// You can use Image.Save ("image.jpg", ImageFormat.Jpeg) instead
</pre>
<p>Now, let see the results ( click the image to see full size )</p>

<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamples/' title='The original image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamples-150x150.jpg" class="attachment-thumbnail" alt="The original image" title="The original image [ AForge Test ]" /></a>
<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamplesgrayscale/' title='Grayscaled image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamplesGrayscale-150x150.jpg" class="attachment-thumbnail" alt="Grayscaled image" title="Grayscaled image [ AForge Test ]" /></a>
<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamplesresult/' title='Edged image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamplesResult-150x150.jpg" class="attachment-thumbnail" alt="Edged image" title="Edged image [ AForge Test ]" /></a>

<p>The result is great, and of course we can modify the threshold levels to gain even better results.<br />
As for other filter and effects there are many of them in this library.<br />
There are Noise generators, color correction filters, smoothing filters and <a title="AForge.Imaging filters and effects" href="http://www.aforgenet.com/framework/features/" target="_blank">more</a>.</p>
<p>Sure, it&#8217;s no Photoshop, but i must say it&#8217;s very close to it.</p>
<p>Hope you will find this useful, I did.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Faforge-net-image-processing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# Rectangle Packing</title>
		<link>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:45:48 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[ascii character]]></category>
		<category><![CDATA[bitmap fonts]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[common interface]]></category>
		<category><![CDATA[cygon]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[humble webmaster]]></category>
		<category><![CDATA[lightmaps]]></category>
		<category><![CDATA[linear increase]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[space efficiency]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[suitable position]]></category>
		<category><![CDATA[support assembly]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=414</guid>
		<description><![CDATA[Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without wasting much space.</p>
<p>What you need then, is a rectangle packer, an algorithm that arranges as many smaller rectangles on a larger rectangle as can possibly fit.</p>
<p><span id="more-414"></span></p>
<p>The <a title="Nuclex - Rectangle Packing" href="http://www.nuclex.org/pages/framework/rectangle-packing" target="_blank">Nuclex.Support</a> assembly contains several rectangle packing algorithms in the Nuclex.Support.Packing namespace offering various levels of compromises between space efficiency and runtime performance. All of these algorithms have been implemented as classes with a common interface, so, once implemented, you can easily switch forth and back between different algorithms to find the one best suites for your purpose.</p>
<p>Currently, there are three different packing algorithms for you to choose:</p>
<table style="width: 100%; border: none;" border="0" cellspacing="2" cellpadding="0">
<tbody>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="The Simple Packer" src="http://kossovsky.net/wp-content/uploads/2009/07/simple-rectangle-packer.png" alt="The Simple Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Simple Packer</strong><br />
This packer is optimized for runtime performance. It does sacrifice a bit of space, but the time needed to find a position for a new rectangle is O(1). In english, that means it doesn&#8217;t take longer to search for a suitable position, no matter how many rectangles have been added to the packing area.</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="cygon-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/cygon-rectangle-packer.png" alt="The Cygon Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Cygon Packer</strong><br />
Named after your humble webmaster, this algorithm is highly efficient in its space usage and offers reasonable performance. It will never exceed O(n) time but generally achieves almost O(1) on average. English translation: Search time is usually constant and guaranteed to never exceed a linear increase (so when you&#8217;ve got 99 rectangles already packed and add the 100th one, it guarantees that search time will at most be 1% longer than the time taken for the previous rectangle).</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="arevalo-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/arevalo-rectangle-packer.png" alt="The Arevalo Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Arevalo Packer</strong><br />
Named after Javier Arevalo, who posted his implementation of this packer on flipcode back in the golden times. This algorithm offers very good space efficiency and runs in slightly less then O(n) time. Just to be consistent, in english, that means, the time needed to find a suitable place for a new rectangle will only increase linearly.</td>
</tr>
</tbody>
</table>
<p>You can download the C# version of the algorithms from <a title="Nuclex Framework - C# Source Code" href="https://devel.nuclex.org/framework/browser/game/Nuclex.Game/trunk/Source/Packing" target="_blank">here</a>.</p>
<p>The usage is very simple :</p>
<pre class="c-sharp" name="code">
	Point PP;
	var ARP = new ArevaloRectanglePacker(200, 200);

	if(!ARP.TryPack(Width, Height, out PP)) {
		// Do something
	}
	else {
		// Do something else
	}
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcshar-rectangle-packing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C# Captcha</title>
		<link>http://kossovsky.net/index.php/2009/06/captcha/</link>
		<comments>http://kossovsky.net/index.php/2009/06/captcha/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 08:38:50 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[c class]]></category>
		<category><![CDATA[Captcha]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[secret key]]></category>
		<category><![CDATA[suits]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[web control]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=159</guid>
		<description><![CDATA[This is a C# class which allows you to create Web and WinForm captcha control. The class is very configurable, so finaly you could create a Captcha that suits your needs. You can hold the Captcha secret key as a session or an encrypted cookie variable. Coming soon&#8230; Rating: 0.0/5 (0 votes cast)<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This is a C# class which allows you to create Web and WinForm captcha control.<br />
The class is very configurable, so finaly you could create a Captcha that suits your needs.<br />
You can hold the Captcha secret key as a session or an encrypted cookie variable.</p>
<p>Coming soon&#8230;</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fcaptcha%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/captcha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CTRL+A Image maker</title>
		<link>http://kossovsky.net/index.php/2009/06/ctrla-image-maker/</link>
		<comments>http://kossovsky.net/index.php/2009/06/ctrla-image-maker/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 21:31:46 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[c code]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[CTRL+A]]></category>
		<category><![CDATA[Hidden Image]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[IE8]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[image maker]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=7</guid>
		<description><![CDATA[Well, this is not a new technique, but i couldn’t find any decent c# code that makes it work – so i’ve decided to write my own. Coming soon… Rating: 0.0/5 (0 votes cast)<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Well, this is not a new technique, but i couldn’t find any decent c# code that makes it work – so i’ve decided to write my own.</p>
<p>Coming soon…</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fctrla-image-maker%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/ctrla-image-maker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Resizing in C#</title>
		<link>http://kossovsky.net/index.php/2009/06/image-resizing/</link>
		<comments>http://kossovsky.net/index.php/2009/06/image-resizing/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 21:30:37 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[encoder]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[high quality]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[image img]]></category>
		<category><![CDATA[image resize]]></category>
		<category><![CDATA[image resizing]]></category>
		<category><![CDATA[Jpg]]></category>
		<category><![CDATA[lossless]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[public static void]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Resize]]></category>
		<category><![CDATA[resize image]]></category>
		<category><![CDATA[Resizer]]></category>
		<category><![CDATA[resizing]]></category>
		<category><![CDATA[static class]]></category>
		<category><![CDATA[static image]]></category>
		<category><![CDATA[string filename]]></category>
		<category><![CDATA[using graphics]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=5</guid>
		<description><![CDATA[Once upon a time there were objects like ASPJeg which made it possible to manipulate images files &#8211; resize, convert and much more&#8230; Lucky for us those times are gone and now we have a great framework called .NET, so why not use it ? Some Image extension methods that will make your life easier. [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (4 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Once upon a time there were objects like ASPJeg which made it possible to manipulate images files &#8211; resize, convert and much more&#8230;</p>
<p>Lucky for us those times are gone and now we have a great framework called .NET, so why not use it ?</p>
<p>Some Image extension methods that will make your life easier.</p>
<p><span id="more-5"></span></p>
<pre class="c-sharp" name="code">
    public static class ImageExtensionMethods
    {

        static private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
        }

        public static void SaveAsJpeg(this Image Img, string FileName, Int64 Quality)
        {
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
            Encoder QualityEncoder = Encoder.Quality;

            using (EncoderParameters EP = new EncoderParameters(1))
            {
                using (EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder, Quality))
                {
                    EP.Param[0] = QualityEncoderParameter;
                    Img.Save(FileName, jgpEncoder, EP);
                }
            }
        }

        public static void SaveAsGif(this Image Img, string FileName, Int64 Quality)
        {
            ImageCodecInfo gifEncoder = GetEncoder(ImageFormat.Gif);
            Encoder QualityEncoder = Encoder.Quality;

            using (EncoderParameters EP = new EncoderParameters(1))
            {
                using (EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder, Quality))
                {
                    EP.Param[0] = QualityEncoderParameter;
                    Img.Save(FileName, gifEncoder, EP);
                }
            }
        }

        public static Image Resize(this Image Img, int Width, int Height, InterpolationMode InterpolationMode)
        {
            Image CropedImage = new Bitmap(Width, Height);
            using (Graphics G = Graphics.FromImage(CropedImage))
            {
                G.SmoothingMode = SmoothingMode.HighQuality;
                G.InterpolationMode = InterpolationMode;
                G.PixelOffsetMode = PixelOffsetMode.HighQuality;
                G.DrawImage(Img, 0, 0, Width, Height);
            }

            return CropedImage;
        }

        public static Image Resize(this Image Img, int Width, int Height)
        {
            return Img.Resize(Width, Height, InterpolationMode.HighQualityBicubic);
        }

        private static Rectangle EnsureAspectRatio(this Image Image, int Width, int Height)
        {
            float AspectRatio = Width / (float)Height;
            float CalculatedWidth = Image.Width, CalculatedHeight = Image.Height;

            if (Image.Width >= Image.Height)
            {
                if (Width > Height)
                {
                    CalculatedHeight = Image.Width / AspectRatio;
                    if (CalculatedHeight > Image.Height)
                    {
                        CalculatedHeight = Image.Height;
                        CalculatedWidth = CalculatedHeight * AspectRatio;
                    }
                }
                else
                {
                    CalculatedWidth = Image.Height * AspectRatio;
                    if (CalculatedWidth > Image.Width)
                    {
                        CalculatedWidth = Image.Width;
                        CalculatedHeight = CalculatedWidth / AspectRatio;
                    }
                }
            }
            else
            {
                if (Width < Height)
                {
                    CalculatedHeight = Image.Width / AspectRatio;
                    if (CalculatedHeight > Image.Height)
                    {
                        CalculatedHeight = Image.Height;
                        CalculatedWidth = CalculatedHeight * AspectRatio;
                    }
                }
                else
                {
                    CalculatedWidth = Image.Height * AspectRatio;
                    if (CalculatedWidth > Image.Width)
                    {
                        CalculatedWidth = Image.Width;
                        CalculatedHeight = CalculatedWidth / AspectRatio;
                    }
                }
            }
            return Rectangle.Ceiling(new RectangleF((Image.Width - CalculatedWidth) / 2, 0, CalculatedWidth, CalculatedHeight));
        }

        public static Image ResizeToCanvas(this Image Img, int Width, int Height, out Rectangle CropRectangle)
        {
            return Img.ResizeToCanvas(Width, Height, InterpolationMode.HighQualityBicubic, out CropRectangle);
        }

        public static Image ResizeToCanvas(this Image Img, int Width, int Height,InterpolationMode InterpolationMode, out Rectangle CropRectangle)
        {
            CropRectangle = EnsureAspectRatio(Image, Width, Height);
            Image CropedImage = new Bitmap(Width, Height);

            using (Graphics G = Graphics.FromImage(CropedImage))
            {
                G.SmoothingMode = SmoothingMode.HighQuality;
                G.InterpolationMode = InterpolationMode;
                G.PixelOffsetMode = PixelOffsetMode.HighQuality;
                G.DrawImage(Img, new Rectangle(0, 0, Width, Height), CropRectangle, GraphicsUnit.Pixel);
            }

            return CropedImage;
        }

        public static Image ResizeToCanvas(this Image Img, int Width, int Height, RectangleF CR)
        {
            return Img.ResizeToCanvas(Width, Height, InterpolationMode.HighQualityBicubic, CR);
        }

        public static Image ResizeToCanvas(this Image Img, int Width, int Height, InterpolationMode InterpolationMode, RectangleF CR)
        {
            Image CropedImage = new Bitmap(Width, Height);
            using (Graphics G = Graphics.FromImage(CropedImage))
            {
                G.SmoothingMode = SmoothingMode.HighQuality;
                G.InterpolationMode = InterpolationMode;
                G.PixelOffsetMode = PixelOffsetMode.HighQuality;
                G.DrawImage(Img, new Rectangle(0, 0, Width, Height), CR, GraphicsUnit.Pixel);
            }

            return CropedImage;
        }

    }
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (4 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fimage-resizing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/image-resizing/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

