/// <summary>
/// Cut specified image with specified size
/// </summary>
/// <param name="src">the source image</param>
/// <param name="x">start point_x</param>
/// <param name="y">start point_y</param>
/// <param name="width">width of new image</param>
/// <param name="height">height of new image</param>
/// <param name="dst">new image after being cut</param>
/// <returns> 0 = normally return;
/// -1 = the source image is null
/// -2 = the start point is beyond the range of source image
/// -9 = cutting error
/// </returns>
public static int CutImage(Bitmap src, int x, int y, int width, int height, out Bitmap dst)
{
dst = null;
if (src == null)
{
return -1;
}
if ((x >= src.Width) || (y >= src.Height))
{
return -2;
}
width = ((x + width) > src.Width) ? (src.Width - x) : width;
height = ((y + height) > src.Height) ? (src.Height - y) : height;
try
{
dst = new Bitmap(width, height, src.PixelFormat);
Graphics g = Graphics.FromImage(dst);
g.DrawImage(src, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
g.Dispose();
}
catch (Exception e)
{
StackTrace st = new StackTrace(new StackFrame(true));
StackFrame sf = st.GetFrame(0);
Debug.WriteLine(String.Format("Line Number: {0}, {1}", sf.GetFileLineNumber(), e.Message));
return -9;
}
return 0;
}
裁减Image
最新推荐文章于 2021-04-29 15:47:14 发布