<?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>Farooq Azam &#187; VB.NET</title>
	<atom:link href="http://www.farooqazam.net/category/vb-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.farooqazam.net</link>
	<description>Unity 3D, Game Development and C# Programming.</description>
	<lastBuildDate>Tue, 31 Jan 2012 19:59:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Crop Image &#8211; C# and VB.NET</title>
		<link>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/</link>
		<comments>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/#comments</comments>
		<pubDate>Mon, 24 May 2010 04:00:00 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[bitmap]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[crop]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=239</guid>
		<description><![CDATA[For cropping an image in C# &#38; VB.NET we can use the Graphics class. We&#8217;ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  &#8230; <a href="http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For cropping an image in C# &amp; VB.NET we can use the <em>Graphics</em> class. We&#8217;ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  Let&#8217;s code!</p>
<p><a href="http://www.farooqazam.net/wp-content/uploads/2010/05/crop.gif"><img title="crop" src="http://www.farooqazam.net/wp-content/uploads/2010/05/crop.gif" alt="" width="350" height="234" /></p>
<p><span id="more-239"></span></a></p>
<h3>The Code</h3>
<p><strong><br />
C#:</strong></p>
<pre class="brush: csharp; title: ; notranslate">
public Bitmap CropImage(Bitmap source, Rectangle section)
{

 // An empty bitmap which will hold the cropped image
 Bitmap bmp = new Bitmap(section.Width, section.Height);

 Graphics g = Graphics.FromImage(bmp);

 // Draw the given area (section) of the source image
 // at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

 return bmp;
}

// Example use:

Bitmap source = new Bitmap(@&quot;C:\tulips.jpg&quot;);
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));

Bitmap CroppedImage = CropImage(source, section);
</pre>
<p><strong><br />
VB.NET</strong></p>
<pre class="brush: vb; title: ; notranslate">
Function CropImage(ByVal source As Bitmap, ByVal section As Rectangle) As Bitmap

 ' An empty bitmap which will hold the cropped image
 Dim bmp As New Bitmap(section.Width, section.Height)

 Dim g As Graphics = Graphics.FromImage(bmp)

 ' Draw the given area (section) of the source image
 ' at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel)

 return bmp
End Function

' Example use:

Dim source As New Bitmap(&quot;C:\tulips.jpg&quot;)
Dim section As New Rectangle(new Point(12, 50), new Size(150, 150))

Dim CroppedImage As Bitmap = CropImage(source, section)
</pre>
<h3>Explanation</h3>
<p>The above code will work perfectly but it seems confusing, doesn&#8217;t it? You might have been wondering how it works. Well&#8230;</p>
<p>We first make an empty bitmap of the same width/height as the <em>section</em> rectangle. This bitmap will hold the cropped image. We use the <em>section</em> rectangle&#8217;s width/height because it determines the size of the cropped image. Then we use the <em>Graphics</em> class&#8217;s <em>DrawImage()</em> method (function) for the cropping. It draws the given area (<em>section</em>) of the <em>source</em> image on the empty bitmap and that&#8217;s it. The <span style="text-decoration: line-through;">empty</span> bitmap now holds the cropped image.</p>
<h3>Source Code</h3>
<p><a href="http://www.farooqazam.net/source/CropImage-Csharp.zip">Download Source Code for C#</a></p>
<p><a href="http://www.farooqazam.net/source/CropImage-VB.zip">Download Source Code for VB.NET</a></p>
<p>If you have any questions, don&#8217;t hesitate to post a comment.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

