图像的剪裁方法,从中间剪裁返回剪裁后的左右两页,方法实现如下:

         /// <summary>
/// 图片裁剪,返回左右两页
/// </summary>
/// <param name="Img">图片</param>
/// <param name="RightMargin">从中间剪裁时左边图片向右偏移量</param>
/// <param name="LeftMargin">从中间剪裁时右边图片向左偏移量</param>
/// <returns>Dictionary</returns>
public Dictionary<string, Bitmap> ImgDiv(Image Img, int RightMargin, int LeftMargin)
{
Dictionary<string, Bitmap> DictImg = new Dictionary<string, Bitmap>();
//获取图片宽高
int Width = Img.Width;
int Height = Img.Height;
//获取图片水平和垂直的分辨率
float dpiX = Img.HorizontalResolution;
float dpiY = Img.VerticalResolution;
//创建一个位图文件
Bitmap BitmapResult = new Bitmap((Width / ) + RightMargin, Height, PixelFormat.Format24bppRgb);
//设置位图文件的水平和垂直分辨率 与Img一致
BitmapResult.SetResolution(dpiX, dpiY);
//在位图文件上填充一个矩形框
Graphics Grp = Graphics.FromImage(BitmapResult);
System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(, , (Width / ) + RightMargin, Height);
Grp.DrawImage(Img, , , Rec, GraphicsUnit.Pixel);
//返回位图文件
Grp.Dispose();
//获取左边图像
DictImg["Left"] = BitmapResult;
//在位图文件上填充一个矩形框
Bitmap BitmapRight = new Bitmap((Width / ) + LeftMargin, Height, PixelFormat.Format24bppRgb);
BitmapRight.SetResolution(dpiX, dpiY);
Graphics GrpRight = Graphics.FromImage(BitmapRight);
System.Drawing.Rectangle RecRight = new System.Drawing.Rectangle((Width / ) - LeftMargin, , (Width / ) + LeftMargin, Height);
GrpRight.DrawImage(Img, , , RecRight, GraphicsUnit.Pixel);
//获取右边图像
DictImg["Right"] = BitmapRight;
GrpRight.Dispose();
GC.Collect();
//返回位图文件
return DictImg;
}
05-06 06:27