How to resize image and save - jpeg imagehttp://forums.asp.net/t/1795562.aspx/1?How+to+resize+image+and+save+jpeg+imageMon, 23 Apr 2012 17:29:34 -040017955624944647http://forums.asp.net/p/1795562/4944647.aspx/1?How+to+resize+image+and+save+jpeg+imageHow to resize image and save - jpeg image <p>Dear All,</p> <p>I have a requirement, to resize the image.</p> <p>Ex: i have some image with 254pixels&nbsp;width and 150pixels height. I want to resize the image to 150pixels and 100px width. Again i have to notify the user, that changes had done, want to store in which location.</p> <p>BR</p> <p>MM</p> <p>&nbsp;</p> 2012-04-22T09:04:07-04:004944649http://forums.asp.net/p/1795562/4944649.aspx/1?Re+How+to+resize+image+and+save+jpeg+imageRe: How to resize image and save - jpeg image <h4>Resizing</h4> <p>This next set of code is a slightly longer and more complex. The main reason this code is longer is because this resize function will keep the height and width proportional.</p> <p>To start with we see that the input parameters are the image to resize (System.Drawing.Image) and the size (System.Drawing.Size). Also in this set of code are a few variables we use. The first two are the source height and width which is used later. And there are 3 other variables to calculate the proportion information.</p> <p></p> &lt;div class=&quot;geshifilter&quot;&gt; &lt;div class=&quot;csharp geshifilter-csharp&quot;&gt;<span>private</span>&nbsp;<span>static</span>&nbsp;<span>Image</span>&nbsp;resizeImage<span>(</span><span>Image</span>&nbsp;imgToResize,&nbsp;<span>Size</span>&nbsp;size<span>)</span><br> <span>{</span><br> &nbsp; &nbsp;<span>int</span>&nbsp;sourceWidth&nbsp;<span>=</span>&nbsp;imgToResize.<span>Width</span><span>;</span><br> &nbsp; &nbsp;<span>int</span>&nbsp;sourceHeight&nbsp;<span>=</span>&nbsp;imgToResize.<span>Height</span><span>;</span><br> <br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercent&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercentW&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercentH&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> <span>}</span>&lt;/div&gt; &lt;/div&gt; <p></p> <p>The next step is to actually figure out what the size of the resized image should be. The first step is to calculate the percentages of the new size compared to the original. Next we need to decide which percentage is smaller because this is the percent of the original image we will use for both height and width. And now we calculate the number of height and width pixels for the destination image.</p> <p></p> &lt;div class=&quot;geshifilter&quot;&gt; &lt;div class=&quot;csharp geshifilter-csharp&quot;&gt;nPercentW&nbsp;<span>=</span>&nbsp;<span>(</span><span>(</span><span>float</span><span>)</span>size.<span>Width</span>&nbsp;<span>/</span>&nbsp;<span>(</span><span>float</span><span>)</span>sourceWidth<span>)</span><span>;</span><br> nPercentH&nbsp;<span>=</span>&nbsp;<span>(</span><span>(</span><span>float</span><span>)</span>size.<span>Height</span>&nbsp;<span>/</span>&nbsp;<span>(</span><span>float</span><span>)</span>sourceHeight<span>)</span><span>;</span><br> <br> <span>if</span>&nbsp;<span>(</span>nPercentH&nbsp;<span>&lt;</span>&nbsp;nPercentW<span>)</span><br> &nbsp; &nbsp;nPercent&nbsp;<span>=</span>&nbsp;nPercentH<span>;</span><br> <span>else</span><br> &nbsp; &nbsp;nPercent&nbsp;<span>=</span>&nbsp;nPercentW<span>;</span><br> <br> <span>int</span>&nbsp;destWidth&nbsp;<span>=</span>&nbsp;<span>(</span><span>int</span><span>)</span><span>(</span>sourceWidth&nbsp;<span>*</span>&nbsp;nPercent<span>)</span><span>;</span><br> <span>int</span>&nbsp;destHeight&nbsp;<span>=</span>&nbsp;<span>(</span><span>int</span><span>)</span><span>(</span>sourceHeight&nbsp;<span>*</span>&nbsp;nPercent<span>)</span><span>;</span>&lt;/div&gt; &lt;/div&gt; <p></p> <p>The final thing to do is create the bitmap (System.Drawing.Bitmap) which we will draw the resized image on using a Graphics (System.Drawing.Graphics) object. I also set the interpolation mode, which is the algorithm used to resize the image. I prefer HighQualityBicubic, which from my testing seems to return the highest quality results. And just to clean up a little I dispose the Graphics object.</p> <p></p> &lt;div class=&quot;geshifilter&quot;&gt; &lt;div class=&quot;csharp geshifilter-csharp&quot;&gt;<span>Bitmap</span>&nbsp;b&nbsp;<span>=</span>&nbsp;<span>new</span>&nbsp;<span>Bitmap</span><span>(</span>destWidth, destHeight<span>)</span><span>;</span><br> <span>Graphics</span>&nbsp;g&nbsp;<span>=</span>&nbsp;<span>Graphics</span>.<span>FromImage</span><span>(</span><span>(</span><span>Image</span><span>)</span>b<span>)</span><span>;</span><br> g.<span>InterpolationMode</span>&nbsp;<span>=</span>&nbsp;<span>InterpolationMode</span>.<span>HighQualityBicubic</span><span>;</span><br> <br> g.<span>DrawImage</span><span>(</span>imgToResize,&nbsp;<span>0</span>,&nbsp;<span>0</span>, destWidth, destHeight<span>)</span><span>;</span><br> g.<span>Dispose</span><span>(</span><span>)</span><span>;</span>&lt;/div&gt; &lt;/div&gt; <p></p> <p>And this gives us the final code.</p> <p></p> &lt;div class=&quot;geshifilter&quot;&gt; &lt;div class=&quot;csharp geshifilter-csharp&quot;&gt;<span>private</span>&nbsp;<span>static</span>&nbsp;<span>Image</span>&nbsp;resizeImage<span>(</span><span>Image</span>&nbsp;imgToResize,&nbsp;<span>Size</span>&nbsp;size<span>)</span><br> <span>{</span><br> &nbsp; &nbsp;<span>int</span>&nbsp;sourceWidth&nbsp;<span>=</span>&nbsp;imgToResize.<span>Width</span><span>;</span><br> &nbsp; &nbsp;<span>int</span>&nbsp;sourceHeight&nbsp;<span>=</span>&nbsp;imgToResize.<span>Height</span><span>;</span><br> <br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercent&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercentW&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> &nbsp; &nbsp;<span>float</span>&nbsp;nPercentH&nbsp;<span>=</span>&nbsp;<span>0</span><span>;</span><br> <br> &nbsp; &nbsp;nPercentW&nbsp;<span>=</span>&nbsp;<span>(</span><span>(</span><span>float</span><span>)</span>size.<span>Width</span>&nbsp;<span>/</span>&nbsp;<span>(</span><span>float</span><span>)</span>sourceWidth<span>)</span><span>;</span><br> &nbsp; &nbsp;nPercentH&nbsp;<span>=</span>&nbsp;<span>(</span><span>(</span><span>float</span><span>)</span>size.<span>Height</span>&nbsp;<span>/</span>&nbsp;<span>(</span><span>float</span><span>)</span>sourceHeight<span>)</span><span>;</span><br> <br> &nbsp; &nbsp;<span>if</span>&nbsp;<span>(</span>nPercentH&nbsp;<span>&lt;</span>&nbsp;nPercentW<span>)</span><br> &nbsp; &nbsp; &nbsp; nPercent&nbsp;<span>=</span>&nbsp;nPercentH<span>;</span><br> &nbsp; &nbsp;<span>else</span><br> &nbsp; &nbsp; &nbsp; nPercent&nbsp;<span>=</span>&nbsp;nPercentW<span>;</span><br> <br> &nbsp; &nbsp;<span>int</span>&nbsp;destWidth&nbsp;<span>=</span>&nbsp;<span>(</span><span>int</span><span>)</span><span>(</span>sourceWidth&nbsp;<span>*</span>&nbsp;nPercent<span>)</span><span>;</span><br> &nbsp; &nbsp;<span>int</span>&nbsp;destHeight&nbsp;<span>=</span>&nbsp;<span>(</span><span>int</span><span>)</span><span>(</span>sourceHeight&nbsp;<span>*</span>&nbsp;nPercent<span>)</span><span>;</span><br> <br> &nbsp; &nbsp;<span>Bitmap</span>&nbsp;b&nbsp;<span>=</span>&nbsp;<span>new</span>&nbsp;<span>Bitmap</span><span>(</span>destWidth, destHeight<span>)</span><span>;</span><br> &nbsp; &nbsp;<span>Graphics</span>&nbsp;g&nbsp;<span>=</span>&nbsp;<span>Graphics</span>.<span>FromImage</span><span>(</span><span>(</span><span>Image</span><span>)</span>b<span>)</span><span>;</span><br> &nbsp; &nbsp;g.<span>InterpolationMode</span>&nbsp;<span>=</span>&nbsp;<span>InterpolationMode</span>.<span>HighQualityBicubic</span><span>;</span><br> <br> &nbsp; &nbsp;g.<span>DrawImage</span><span>(</span>imgToResize,&nbsp;<span>0</span>,&nbsp;<span>0</span>, destWidth, destHeight<span>)</span><span>;</span><br> &nbsp; &nbsp;g.<span>Dispose</span><span>(</span><span>)</span><span>;</span><br> <br> &nbsp; &nbsp;<span>return</span>&nbsp;<span>(</span><span>Image</span><span>)</span>b<span>;</span><br> <span>}</span>&lt;/div&gt; &lt;/div&gt; <p></p> <p>Here is the source code and a C# VS2005 Express Edition solution with the needed methods and some test code.<a href="http://www.switchonthecode.com/sites/default/files/27/source/SOTC-TestResizeCrop.zip">Click Me for Code</a></p> <p><a href="http://www.codeproject.com/Articles/76206/Image-in-C-Save-Resize-and-Convert-to-Binary">http://www.codeproject.com/Articles/76206/Image-in-C-Save-Resize-and-Convert-to-Binary</a>\</p> <p><a href="http://kossovsky.net/index.php/2009/06/image-resizing/">http://kossovsky.net/index.php/2009/06/image-resizing/</a></p> <p>thank u</p> 2012-04-22T09:07:37-04:004944777http://forums.asp.net/p/1795562/4944777.aspx/1?Re+How+to+resize+image+and+save+jpeg+imageRe: How to resize image and save - jpeg image <p>refer&nbsp;<a href="http://csharpdotnetfreak.blogspot.com/2010/02/resize-image-upload-ms-sql-database.html">http://csharpdotnetfreak.blogspot.com/2010/02/resize-image-upload-ms-sql-database.html</a></p> 2012-04-22T12:39:22-04:004945132http://forums.asp.net/p/1795562/4945132.aspx/1?Re+How+to+resize+image+and+save+jpeg+imageRe: How to resize image and save - jpeg image <p>I use the following code to resize an image down to 700(w) x 100(h) px.&nbsp; It preserves the aspect ratio of the original image.&nbsp; The result is saved as a gif, but you can change that in the obvious place.&nbsp; (You will need using SystemDrawing if not already imported).</p> <pre class="prettyprint">using (Image logo = Image.FromFile(uploadedFileName)) { Single xScale = 1.0F; Single yScale = 1.0F; if (logo.Height &gt; 100) yScale = 100F / logo.Height; if (logo.Width &gt; 700) xScale = 700F / logo.Width; Single scale = Math.Min(xScale, yScale); if (scale &lt; 1.0F) { Size logoSize = new Size((int)(logo.Width * scale), (int)(logo.Height * scale)); Rectangle logoRect = new Rectangle(new Point(0, 0), logoSize); Bitmap scaledLogo = new Bitmap(logoSize.Width, logoSize.Height); Graphics canvas = Graphics.FromImage(scaledLogo); canvas.FillRectangle(new SolidBrush(Color.White), logoRect); canvas.DrawImage(logo, new Rectangle(0, 0, (int)(logo.Width * scale), (int)(logo.Height * scale))); scaledLogo.Save(tmpFile, ImageFormat.Gif); } else { logo.Save(tmpFile, ImageFormat.Gif); }</pre> <p></p> 2012-04-22T22:58:23-04:004946969http://forums.asp.net/p/1795562/4946969.aspx/1?Re+How+to+resize+image+and+save+jpeg+imageRe: How to resize image and save - jpeg image <p>Please check following link, it may be helpful to you.</p> <ol> <li><a target="_blank" href="http://www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET">http://www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET</a> </li><li><a target="_blank" href="http://www.codeproject.com/Articles/48350/Resizing-images-in-ASP-NET-using-GDI">http://www.codeproject.com/Articles/48350/Resizing-images-in-ASP-NET-using-GDI</a> </li><li><a target="_blank" href="http://stackoverflow.com/questions/9964018/image-quality-loss-while-resizing-image-in-c-sharp-for-asp-net-webform">http://stackoverflow.com/questions/9964018/image-quality-loss-while-resizing-image-in-c-sharp-for-asp-net-webform</a> </li><li><a target="_blank" href="http://stackoverflow.com/questions/2473386/resizing-images-with-asp-net-and-saving-to-database">http://stackoverflow.com/questions/2473386/resizing-images-with-asp-net-and-saving-to-database</a> </li></ol> 2012-04-23T17:29:34-04:00