<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: images code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 23:30:27 GMT</pubDate>
    <description>DZone Snippets: images code</description>
    <item>
      <title>Resize image with .NET</title>
      <link>http://snippets.dzone.com/posts/show/1485</link>
      <description>using System;&lt;br /&gt;using System.Drawing;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;using System.Drawing.Imaging;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)&lt;br /&gt;{&lt;br /&gt;    Image original = Image.FromStream(new MemoryStream(imageFile));&lt;br /&gt;    int targetH, targetW;&lt;br /&gt;    if (original.Height &gt; original.Width)&lt;br /&gt;    {&lt;br /&gt;        targetH = targetSize;&lt;br /&gt;        targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        targetW = targetSize;&lt;br /&gt;        targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));&lt;br /&gt;    }&lt;br /&gt;    Image imgPhoto = Image.FromStream(new MemoryStream(imageFile));&lt;br /&gt;    // Create a new blank canvas.  The resized image will be drawn on this canvas.&lt;br /&gt;    Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);&lt;br /&gt;    bmPhoto.SetResolution(72, 72);&lt;br /&gt;    Graphics grPhoto = Graphics.FromImage(bmPhoto);&lt;br /&gt;    grPhoto.SmoothingMode = SmoothingMode.AntiAlias;&lt;br /&gt;    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;&lt;br /&gt;    grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;&lt;br /&gt;    grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);&lt;br /&gt;    // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.&lt;br /&gt;    MemoryStream mm = new MemoryStream();&lt;br /&gt;    bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);&lt;br /&gt;    original.Dispose();&lt;br /&gt;    imgPhoto.Dispose();&lt;br /&gt;    bmPhoto.Dispose();&lt;br /&gt;    grPhoto.Dispose();&lt;br /&gt;    return mm.GetBuffer();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 14 Feb 2006 23:35:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1485</guid>
      <author>Edgardo (Edgardo Rossetto)</author>
    </item>
    <item>
      <title>Crop image with .NET</title>
      <link>http://snippets.dzone.com/posts/show/1484</link>
      <description>using System;&lt;br /&gt;using System.Drawing;&lt;br /&gt;using System.Drawing.Drawing2D;&lt;br /&gt;using System.Drawing.Imaging;&lt;br /&gt;using System.IO;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public static byte[] CropImageFile(byte[] imageFile, int targetW, int targetH, int targetX, int targetY)&lt;br /&gt;{&lt;br /&gt;    Image imgPhoto = Image.FromStream(new MemoryStream(imageFile));&lt;br /&gt;    Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);&lt;br /&gt;    bmPhoto.SetResolution(72, 72);&lt;br /&gt;    Graphics grPhoto = Graphics.FromImage(bmPhoto);&lt;br /&gt;    grPhoto.SmoothingMode = SmoothingMode.AntiAlias;&lt;br /&gt;    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;&lt;br /&gt;    grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;&lt;br /&gt;    grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);&lt;br /&gt;    // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.&lt;br /&gt;    MemoryStream mm = new MemoryStream();&lt;br /&gt;    bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);&lt;br /&gt;    imgPhoto.Dispose();&lt;br /&gt;    bmPhoto.Dispose();&lt;br /&gt;    grPhoto.Dispose();&lt;br /&gt;    return mm.GetBuffer();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 14 Feb 2006 23:34:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1484</guid>
      <author>Edgardo (Edgardo Rossetto)</author>
    </item>
  </channel>
</rss>
