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;
}