<?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; Resize</title>
	<atom:link href="http://kossovsky.net/index.php/tag/resize/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>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>

