C#图像处理(4):图像的剪裁

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

 1         /// <summary>
 2         /// 图片裁剪,返回左右两页
 3         /// </summary>
 4         /// <param name="Img">图片</param>
 5         /// <param name="RightMargin">从中间剪裁时左边图片向右偏移量</param>
 6         /// <param name="LeftMargin">从中间剪裁时右边图片向左偏移量</param>
 7         /// <returns>Dictionary</returns>
 8         public Dictionary<string, Bitmap> ImgDiv(Image Img, int RightMargin, int LeftMargin)
 9         {
10             Dictionary<string, Bitmap> DictImg = new Dictionary<string, Bitmap>();
11             //获取图片宽高
12             int Width = Img.Width;
13             int Height = Img.Height;
14             //获取图片水平和垂直的分辨率
15             float dpiX = Img.HorizontalResolution;
16             float dpiY = Img.VerticalResolution;
17             //创建一个位图文件
18             Bitmap BitmapResult = new Bitmap((Width / 2) + RightMargin, Height, PixelFormat.Format24bppRgb);
19             //设置位图文件的水平和垂直分辨率  与Img一致
20             BitmapResult.SetResolution(dpiX, dpiY);
21             //在位图文件上填充一个矩形框
22             Graphics Grp = Graphics.FromImage(BitmapResult);
23             System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, (Width / 2) + RightMargin, Height);
24             Grp.DrawImage(Img, 0, 0, Rec, GraphicsUnit.Pixel);
25             //返回位图文件
26             Grp.Dispose();
27             //获取左边图像
28             DictImg["Left"] = BitmapResult;
29             //在位图文件上填充一个矩形框
30             Bitmap BitmapRight = new Bitmap((Width / 2) + LeftMargin, Height, PixelFormat.Format24bppRgb);
31             BitmapRight.SetResolution(dpiX, dpiY);
32             Graphics GrpRight = Graphics.FromImage(BitmapRight);
33             System.Drawing.Rectangle RecRight = new System.Drawing.Rectangle((Width / 2) - LeftMargin, 0, (Width / 2) + LeftMargin, Height);
34             GrpRight.DrawImage(Img, 0, 0, RecRight, GraphicsUnit.Pixel);
35             //获取右边图像
36             DictImg["Right"] = BitmapRight;
37             GrpRight.Dispose();
38             GC.Collect();
39             //返回位图文件
40             return DictImg;
41         }

 

posted on 2014-08-18 15:22  吴培洪  阅读(2878)  评论(0编辑  收藏  举报

导航