/**/
/// <summary>
/// Resize图片(不成功返回null)
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <returns>处理以后的图片</returns>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
... {
try
...{
if (bmp.Width == newW && bmp.Height == newH)
return bmp;
float newW_t = 0; //临时宽
float newH_t = 0; //临时高
float xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
float yPoint = 0;
if (bmp.Width / bmp.Height < newW / newH) //图片太宽
...{
newH_t = newH;
newW_t = bmp.Width * newH / bmp.Height;
xPoint = (newW - newW_t) / 2;
}
else
...{
newW_t = newW;
newH_t = bmp.Height * newW / bmp.Width;
yPoint = (newH - newH_t) / 2;
}
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, newW, newH);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(Convert.ToInt32(xPoint), Convert.ToInt32(yPoint), Convert.ToInt32(newW_t), Convert.ToInt32(newH_t)), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
...{
return null;
}
}
/// Resize图片(不成功返回null)
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <returns>处理以后的图片</returns>
public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
... {
try
...{
if (bmp.Width == newW && bmp.Height == newH)
return bmp;
float newW_t = 0; //临时宽
float newH_t = 0; //临时高
float xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。
float yPoint = 0;
if (bmp.Width / bmp.Height < newW / newH) //图片太宽
...{
newH_t = newH;
newW_t = bmp.Width * newH / bmp.Height;
xPoint = (newW - newW_t) / 2;
}
else
...{
newW_t = newW;
newH_t = bmp.Height * newW / bmp.Width;
yPoint = (newH - newH_t) / 2;
}
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, newW, newH);
// 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bmp, new Rectangle(Convert.ToInt32(xPoint), Convert.ToInt32(yPoint), Convert.ToInt32(newW_t), Convert.ToInt32(newH_t)), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose();
return b;
}
catch
...{
return null;
}
}