tags:

views:

872

answers:

3

I want to resize an image with the GDI library so that when I resize it to be larger than before there is no blending. (Like when you zoom in on an image in a paint program)

EG: If my image is 2px wide, and 2px tall
(white, white,
white, black)
, and I resize it to be 100% larger, it is 4px by 4px tall
(white, white, white, white,
white, white, white, white,
white, white, black, black,
white, white, black, black)

What InterpolationMode or Smoothing mode (or other properties) of a graphics object can I use to achieve this? The combinations that I have tried so far all cause grey to appear in the test image.

Here is the code that I'm using

    /// <summary>
    /// Do the resize using GDI+
    /// Credit to the original author
    /// http://www.bryanrobson.net/dnn/Code/Imageresizing/tabid/69/Default.aspx
    /// </summary>
    /// <param name="srcBitmap">The source bitmap to be resized</param>
    /// <param name="width">The target width</param>
    /// <param name="height">The target height</param>
    /// <param name="isHighQuality">Shoule the resize be done at high quality?</param>
    /// <returns>The resized Bitmap</returns>

    public static Bitmap Resize(Bitmap srcBitmap, int width, int height, bool isHighQuality)
    {
        // Create the destination Bitmap, and set its resolution
        Bitmap destBitmap = new Bitmap((int)Convert.ToInt32(width), (int)Convert.ToInt32(height), PixelFormat.Format24bppRgb);
        destBitmap.SetResolution(srcBitmap.HorizontalResolution, srcBitmap.VerticalResolution);

        // Create a Graphics object from the destination Bitmap, and set the quality
        Graphics grPhoto = Graphics.FromImage(destBitmap);
        if (isHighQuality)
        {
            grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        }
        else
        {
            grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; //? this doesn't work
            grPhoto.InterpolationMode = InterpolationMode.NearestNeighbor; //? this doesn't work


        }
        // Do the resize
        grPhoto.DrawImage(srcBitmap,
              new Rectangle(0, 0, width, height),
              new Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height),
              GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return destBitmap;
    }
A: 

When you are drawing the image and passing in the source rectangle, pass in only the zoomed-in portion of the original image and draw that to the larger area. When the user zooms in, at some point they will not be able to see the entire image in the viewing area. So, figure out which source region should still be in view and paint only that.

Ed Swangren
The problem is not the portion of the image that gets passed, but the fact that some smoothing always takes place no matter what in interpolation mode I select. I will try to describe it better in the post.
NetHawk
A: 

It doesn't look like you're doing anything wrong to me. See the instructions from Microsoft on the InterpolationMode:

http://msdn.microsoft.com/en-us/library/ms533836(VS.85).aspx

Maybe this function is working perfectly, but you're giving it the wrong parameters or displaying the result incorrectly?

Mark Ransom
+1  A: 

You were on the right track by using InterpolationMode.NearestNeighbor. However, with the default PixelOffsetMode, GDI+ will try and sample at the pixel edges, causing the blending.

To get the scaling without the blending, you also need to use PixelOffsetMode.Half. Change your non-high quality case to:

else
{
    grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
    grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
}
Chris Ostler
Chris, thanks, I will try this later. Since I asked the question a while ago, and it was just a personal project, I let it go, as it was 'good enough'.Your answer looks right (didn't try it yet, like I said), and I hope it helps some people who find this question.
NetHawk