我想使用 GDI 库调整图像的大小,这样当我将其调整为比以前更大时,就不会出现混合。(就像在绘画程序中放大图像时一样)
EG:如果我的图像是 2px 宽和 2px 高
(白色、白色、
白色、黑色)
,并且我将其调整为 100% 大,则它是 4px x 4px 高
(白色、白色、白色、白色、
白色、白色) , 白色, 白色,
白色, 白色, 黑色, 黑色,
白色, 白色, 黑色, 黑色)
我可以使用图形对象的哪些 InterpolationMode 或 Smoothing 模式(或其他属性)来实现这一点?到目前为止我尝试过的组合都会导致灰色出现在测试图像中。
这是我正在使用的代码
/// <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;
}