Image loss while shrinking certain images with Graphics.DrawImage()
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/6170912/how-can-i-get-better-results-when-shrinking-an-image">How can I get better results when shrinking an image</a>  </p>
</blockquote>



<p>I am trying to generate a thumbnail of size 200 x 200px using the code below. It works fine for certain images but does not for others. When it does not work, the thumbnail is generated with that size but only partial image is seen and the other part is gray (like you have used a gray brush and smeared on top of the thumbnail). I haven't been able to see a trend when it fails. For example, it fails for a JPEG image with 400px x 400px size. If I try to generate the thumbnail with size 150px x 150px, there is no image loss.
If this helps here is one image that causes problems - <a href="http://s11.postimage.org/sse5zhpqr/Drawing_8.jpg" rel="nofollow">http://s11.postimage.org/sse5zhpqr/Drawing_8.jpg</a></p>

<p>Appreciate your time.</p>

<pre><code>    public Bitmap GenerateThumbnail(Bitmap sourceBitmap, int thumbnailWidth, int thumbnailHeight)
    {
        Bitmap thumbnailBitmap = null;

        decimal ratio;
        int newWidth = 0;
        int newHeight = 0;

        // If the image is smaller than the requested thumbnail size just return it
        if (sourceBitmap.Width &lt; thumbnailWidth &amp;&amp;
            sourceBitmap.Height &lt; thumbnailHeight)
        {
            newWidth = sourceBitmap.Width;
            newHeight = sourceBitmap.Height; 
        }
        else if (sourceBitmap.Width &gt; sourceBitmap.Height)
        {
            ratio = (decimal)thumbnailWidth / sourceBitmap.Width;
            newWidth = thumbnailWidth;
            decimal tempDecimalHeight = sourceBitmap.Height * ratio;
            newHeight = (int)tempDecimalHeight;
        }
        else
        {
            ratio = (decimal)thumbnailHeight / sourceBitmap.Height;
            newHeight = thumbnailHeight;
            decimal tempDecimalHeight = sourceBitmap.Width * ratio;
            newWidth = (int)tempDecimalHeight;
        }

        thumbnailBitmap = new Bitmap(newWidth, newHeight);

        Graphics g = Graphics.FromImage(thumbnailBitmap);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

        g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
        g.DrawImage(sourceBitmap, 0, 0, newWidth, newHeight);
        g.Dispose();


        return thumbnailBitmap;
    }
</code></pre>

<p>Update:</p>

<p>I did some more analysis and it seems that the issue is while saving the thumbnail to the SQL Server database in a varbinary column. I am using a MemoryStream to do that as below. If I save it to the disk it appears just fine. Here is my thumbnail from the database - <a href="http://s12.postimage.org/a7j50vr8d/Generated_Thumbnail.jpg" rel="nofollow">http://s12.postimage.org/a7j50vr8d/Generated_Thumbnail.jpg</a></p>

<pre><code> using (MemoryStream thumbnailStream = new MemoryStream())
{             
          thumbnailBitmap.Save(thumbnailStream, System.Drawing.Imaging.ImageFormat.Jpeg);               
          return thumbnailStream.ToArray();

}
</code></pre>

<p>Update - This problem is resolved. The issue was NHibernate mapping that I was using for the varbinary column. I had to specify the type as "BinaryBlob" to make sure there is no silent truncation of data > 8KB.</p>
