title: After resizing white image gets gray border
body: <p>i was searching google for some kind solution and i found one, i tried to implement it in my code but it doesn't work. The problem is that after resizing white images they gets gray border.</p>
  
  <p><strong>Here is the link of soloution i found:</strong></p>
  
  <p><strong>It says:</strong>
  This problem is occuring because you are interpolating your image data to a
  new size, but along the edges there are no pixels to interpolate and .NET
  uses black pixels for these edges by default. To fix this you need to use an
  ImageAttributes class in your DrawImage call.... </p>
  
  <p><a href="https://groups.google.com/group/microsoft.public.dotnet.framework.drawing/browse_thread/thread/d834851b49274fd9/81a4fd43694457ac?hl=en&amp;lnk=st&amp;q=DrawImage+resized+border#81a4fd43694457ac" rel="nofollow noreferrer">https://groups.google.com/group/microsoft.public.dotnet.framework.drawing/browse_thread/thread/d834851b49274fd9/81a4fd43694457ac?hl=en&amp;lnk=st&amp;q=DrawImage+resized+border#81a4fd43694457ac</a></p>
  
  <p><strong>CODE 1: And this is my code WITH IMPLEMENTATION OF ImageAttributes:</strong></p>
  
  <pre><code>Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
  
      Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
  
          Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)
  
          Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)
  
              Using canvas As Graphics = Graphics.FromImage(newImage)
  
                  Using ia As New ImageAttributes
  
                      ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
                      canvas.SmoothingMode = SmoothingMode.AntiAlias
                      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                      canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                      canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)
  
                      Dim m As New MemoryStream()
  
                      newImage.Save(m, ImageFormat.Png)
                      Return m.GetBuffer()
  
                  End Using
  
              End Using
  
          End Using
  
      End Using
  
  End Function
  
  Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
  
      Dim newSize As New Size()
  
      If oldSize.Height &gt; oldSize.Width Then
  
          newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
          newSize.Height = targetSize
  
      Else
  
          newSize.Width = targetSize
          newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))
  
      End If
  
      Return newSize
  
  End Function
  </code></pre>
  
  <p><strong>CODE 2: CODE THAT COUSES A GRAY BORDER ON WHITE IMAGE</strong></p>
  
  <p><strong>Here is the image after resizing:</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/na3Hd.jpg" alt="enter image description here"></p>
  
  <p>new image size in width = 400px</p>
  
  <pre><code>Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
  
      Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
  
          Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)
  
          Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)
  
              Using canvas As Graphics = Graphics.FromImage(newImage)
  
                      canvas.SmoothingMode = SmoothingMode.AntiAlias
                      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                      canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                      canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize))
  
                      Dim m As New MemoryStream()
  
                      newImage.Save(m, ImageFormat.Png)
                      Return m.GetBuffer()
  
              End Using
  
          End Using
  
      End Using
  
  End Function
  
  Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
  
      Dim newSize As New Size()
  
      If oldSize.Height &gt; oldSize.Width Then
  
          newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
          newSize.Height = targetSize
  
      Else
  
          newSize.Width = targetSize
          newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))
  
      End If
  
      Return newSize
  
  End Function
  </code></pre>
  
  <p><strong>UPDATE 30.07.2011.:</strong></p>
  
  <p>CODE 1 solved the problem with the gray borders on white images, but there is <strong>new problem</strong>. The problem is in this line of code:  </p>
  
  <p>canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)</p>
  
  <p>With this code I get output image with desired width and height and without gray borders but the oldImage isn't scaled.</p>
  
  <p><strong>For example:</strong> </p>
  
  <p>If I want to upload, resize and save the image that is orginaly for instance 640x480px and that the  targetSize is 400px. As output I get an image that is width: 400px, height: 300px, without gray borders, but oldImage isn't resized/scaled to 400px. Insted of this, oldImage is drawn with the original resolution. <strong>How to scale oldImage to be drawn correctly? Can someone point me to the right solution or modify the code?</strong></p>
  
  <p><strong>Thanx to everyone, but I found the solution to all my problems.</strong></p>
  
  <p><strong>The CODE 1 <em>didn't work correctly</em> because of the following line of code:</strong></p>
  
  <p>canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, <strong>oldImage.Width</strong>, <strong>oldImage.Height</strong>, GraphicsUnit.Pixel, ia)</p>
  
  <p><strong>HERE IS THE FULL WORKING CODE (resized image without gray/black borders):</strong> </p>
  
  <pre><code>Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
  
      Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
  
          Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)
  
          Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)
  
              Using canvas As Graphics = Graphics.FromImage(newImage)
  
                  Using ia As New ImageAttributes
  
                      ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
                      canvas.SmoothingMode = SmoothingMode.AntiAlias
                      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                      canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                      canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, oldImage.Width, oldImage.Height, GraphicsUnit.Pixel, ia)
  
                      Dim m As New MemoryStream()
  
                      newImage.Save(m, ImageFormat.Png)
                      Return m.GetBuffer()
  
                  End Using
  
              End Using
  
          End Using
  
      End Using
  
  End Function
  
  Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
  
      Dim newSize As New Size()
  
      If oldSize.Height &gt; oldSize.Width Then
  
          newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
          newSize.Height = targetSize
  
      Else
  
          newSize.Width = targetSize
          newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))
  
      End If
  
      Return newSize
  
  End Function
  </code></pre>