<?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; image viewer</title>
	<atom:link href="http://www.farooqazam.net/tag/image-viewer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.farooqazam.net</link>
	<description>C# &#38; .NET Programming Blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 20:15:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Image Viewer application using C#</title>
		<link>http://www.farooqazam.net/image-viewer-application-using-c-sharp/</link>
		<comments>http://www.farooqazam.net/image-viewer-application-using-c-sharp/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:38:45 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[image viewer]]></category>
		<category><![CDATA[picturebox]]></category>
		<category><![CDATA[rotate]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=126</guid>
		<description><![CDATA[A detailed tutorial about creating an Image Viewer or Picture Viewer application in C#. This application also converts Images from one type to another. Following are the features of this application: Open / Save images. Convert images. Rotate Images. All popular image formats are supported. Let&#8217;s get started! Setting up the Designer For this application [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fimage-viewer-application-using-c-sharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fimage-viewer-application-using-c-sharp%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Image Viewer" href="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif"><img class="alignnone size-full wp-image-127" title="image-viewer" src="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif" alt="image-viewer" width="469" height="411" /></a></p>
<p>A detailed tutorial about creating an Image Viewer or Picture Viewer application in C#. This application also converts Images from one type to another.</p>
<p>Following are the features of this application:</p>
<ul>
<li>Open / Save images.</li>
<li>Convert images.</li>
<li>Rotate Images.</li>
<li>All popular image formats are supported.</li>
</ul>
<p>Let&#8217;s get started!<span id="more-126"></span></p>
<h3>Setting up the Designer</h3>
<p>For this application you will need an openFileDialog, saveFileDialog, pictureBox and a few buttons.</p>
<ol>
<li>Add openFileDialog and saveFileDialog to the form.</li>
<li>Use this filter for the openFileDialog and saveFileDialog: &#8221; JPEG File|*.jpg|GIF File|*.gif|PNG File|*.png|BMP File|*.bmp &#8220;</li>
<li>Add 4 buttons (<a title="Image Viewer" href="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif">refer to the image above</a>). Name the buttons just like I&#8217;ve did in that image.</li>
<li>Set the &#8220;Enabled&#8221; property for &#8220;Save&#8221; button to &#8220;False&#8221;. And set &#8220;Visible&#8221; to &#8220;False&#8221; for &#8220;RotateClockwise&#8221; and &#8220;RotateCounterclockwise&#8221; buttons.</li>
<li>Add a pictureBox and place it under the buttons (refer to the image above).</li>
</ol>
<p>That&#8217;s it. Now lets get on with the code.</p>
<h3>The Code</h3>
<p>First of all, add this code to the very top of your form:</p>
<pre class="brush: csharp;">
using System.Drawing.Imaging;
</pre>
<p>Declare these methods after the &#8220;public Form1() { &#8230;.&#8221; method:</p>
<pre class="brush: csharp;">// This method will be used to save the image
private void saveImage(string ext)
{
   ImageFormat format = null;

   if(ext == &quot;.gif&quot;)
      format = ImageFormat.Gif;
   else if(ext == &quot;.jpg&quot; || ext == &quot;.jpeg&quot;)
      format = ImageFormat.Jpeg;
   else if(ext == &quot;png&quot;)
      format = ImageFormat.Png;
   else if(ext == &quot;.bmp&quot;)
      format = ImageFormat.Bmp;

   pictureBox1.Image.Save(saveFileDialog1.FileName, format);

}

// This method will be used for rotating the image.
private void rotateImage(int angle)
{
   Bitmap img = new Bitmap(pictureBox1.Image);

   // 0 = Clockwise, 1 = counter-clockwise

   if (angle == 0)
      img.RotateFlip(RotateFlipType.Rotate90FlipNone);
   else if (angle == 1)
      img.RotateFlip(RotateFlipType.Rotate90FlipXY);

   pictureBox1.Size = img.Size;
   pictureBox1.Image = img;
}
</pre>
<p>Double-click the &#8220;Open&#8221; button to add the &#8220;Click()&#8221; event for it and use this code:</p>
<pre class="brush: csharp;">private void openBtn_Click(object sender, EventArgs e)
{
   DialogResult result = openFileDialog1.ShowDialog();
   if (result == DialogResult.OK)
   {
      Image img = Image.FromFile(openFileDialog1.FileName);

      // Assigns the opened image to picturebox.
      pictureBox1.Image = img;

      // Makes the pictureBox width/height same as the opened image.
      pictureBox1.Width = img.Width;
      pictureBox1.Height = img.Height;
   }

   // Enables the Save button.
   saveBtn.Enabled = true;

   // Shows the RotateClockwise and Counterclockwise buttons.
   rotateClockwise.Show();
   rotateCounter.Show();
 }</pre>
<p>Double-click the Save button and use this code:</p>
<pre class="brush: csharp;">private void saveBtn_Click(object sender, EventArgs e)
{
   DialogResult res = saveFileDialog1.ShowDialog();
   if (res == DialogResult.OK)
   {
      // Extracts the extension from the opened file.
      string ext = System.IO.Path.GetExtension(saveFileDialog1.FileName);
      ext = ext.ToLower();

      saveImage(ext);
   }
}</pre>
<p>Double-click the RotateClockwise button and use this code:</p>
<pre class="brush: csharp;">private void rotateClockwise_Click(object sender, EventArgs e)
{
   // Calls the rotateImage method and passes the parameter &quot;0&quot;
   // Refer to the rotateImage method to see how it works.
   rotateImage(0);
}
</pre>
<p>And lastly, double-click the Rotate Counterclockwise button and use this code:</p>
<pre class="brush: csharp;"> private void rotateCounter_Click(object sender, EventArgs e)
{
   // 1 = counter-clockwise
   rotateImage(1);
}
</pre>
<p>That&#8217;s it! We are done.</p>
<h3>Source Code</h3>
<p>ImageViewer Source – <a title="Download ScreenCapture class" href="../source/ImageViewer.zip"><strong>Download Now</strong></a> (42 Kb)</p>
<p>&#8212;&#8212;</p>
<p>Feedback and comments are warmly welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/image-viewer-application-using-c-sharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
