我们正在调整图片大小,我们正面临一个问题。图像的一部分正在调整大小,我们正在使用下面的代码。
public Bitmap ScaleImage(Bitmap image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = 1.0;
if (maxWidth == 0)
{
ratio = ratioY;
}
else if (maxHeight == 0)
{
ratio = ratioX;
}
else
{
ratio = Math.Min(ratioX, ratioY);
}
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
Graphics graphicsObj;
var newImage = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//var newImage = new Bitmap(newWidth, newHeight);
graphicsObj = Graphics.FromImage(newImage);
graphicsObj.Clear(Color.White);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
请按照此link进行操作。