/// <summary>
/// 图片裁剪工具方法
/// </summary>
/// <param name="Bitmap"></param>
/// <param name="Rect"></param>
/// <returns></returns>
public static Bitmap CutImage(Bitmap Bitmap, ref Rectangle Rect)
{
if (Rect.X < 0)
{
Rect.X = 0;
}
if (Rect.Y < 0)
{
Rect.Y = 0;
}
if (Rect.X + Rect.Width > Bitmap.Width)
{
Rect.Width = Bitmap.Width - Rect.X;
}
if (Rect.Y + Rect.Height > Bitmap.Height)
{
Rect.Height = Bitmap.Height - Rect.Y;
}
if (0 == Rect.Width)
{
Rect.Width = 4;
}
if (0 == Rect.Height)
{
Rect.Height = 4;
}
return Bitmap.Clone(Rect, Bitmap.PixelFormat);
}
/// <summary>
/// 图片按比例缩放的工具方法
/// </summary>
/// <param name="bitmap"></param>
/// <param name="Scale"></param>
/// <returns></returns>
public static Bitmap ResizeImage(Bitmap bitmap, float Scale = 1f)
{
try
{
if (null != bitmap)
{
int w = bitmap.Width;
}
}
catch (ArgumentException)//图像被释放了
{
bitmap = null;
}
if (1f == Scale || null == bitmap)
{
return bitmap;
}
Bitmap result;
try
{
int width = (int)(bitmap.Width * Scale);
int height = (int)(bitmap.Height * Scale);
Bitmap bitmap2 = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.DrawImage(bitmap, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
graphics.Dispose();
result = bitmap2;
}
catch
{
result = bitmap;
}
return result;
}