<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: height code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 15:21:17 GMT</pubDate>
    <description>DZone Snippets: height code</description>
    <item>
      <title>C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height</title>
      <link>http://snippets.dzone.com/posts/show/4336</link>
      <description>// This allows us to resize the image. It prevents skewed images and &lt;br /&gt;// also vertically long images caused by trying to maintain the aspect &lt;br /&gt;// ratio on images who's height is larger than their width&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)&lt;br /&gt;{&lt;br /&gt;	System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);&lt;br /&gt;&lt;br /&gt;	// Prevent using images internal thumbnail&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;&lt;br /&gt;	if (OnlyResizeIfWider)&lt;br /&gt;	{&lt;br /&gt;		if (FullsizeImage.Width &lt;= NewWidth)&lt;br /&gt;		{&lt;br /&gt;			NewWidth = FullsizeImage.Width;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;&lt;br /&gt;	if (NewHeight &gt; MaxHeight)&lt;br /&gt;	{&lt;br /&gt;		// Resize with height instead&lt;br /&gt;		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;&lt;br /&gt;		NewHeight = MaxHeight;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);&lt;br /&gt;&lt;br /&gt;	// Clear handle to original file so that we can overwrite it if necessary&lt;br /&gt;	FullsizeImage.Dispose();&lt;br /&gt;&lt;br /&gt;	// Save resized picture&lt;br /&gt;	NewImage.Save(NewFile);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:55:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4336</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
  </channel>
</rss>
