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

本文介绍了一种从中间剪裁图片并返回左右两页的方法,详细解释了如何通过参数调整来精确控制剪裁区域。通过使用位图文件和图形API,实现了灵活且高效的图像处理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 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         }

 

转载于:https://www.cnblogs.com/wupeihong/p/3919626.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值