目录

一、需求

Rectangle(Point, Size)

Rectangle(Int32, Int32, Int32, Int32)

二、常用的功能

1.判断两个矩形是否相交

2.求出两矩形相交重叠处的矩形

3.案例

三、切图Demo

结束


一、需求

矩形在开发中非常常见,比如截图功能,GDI+ 画图,Rectangle 的构造函数中,需要提供四个参数,坐标x,y,宽度,高度,另外还封装了其他的一写方法,比如是否相交,两矩形相交处重叠的矩形,根据左上,和右下坐标生成矩形等等方法,下面是构造函数的一些介绍。

构造函数:

Rectangle(Point, Size)

用指定的位置和大小初始化 Rectangle 类的新实例。

public Rectangle (System.Drawing.Point location, System.Drawing.Size size);

参数:location Point  Point,它表示矩形区域的左上角。size Size  Size,它表示矩形区域的宽度和高度。

Rectangle(Int32, Int32, Int32, Int32)

用指定的位置和大小初始化 Rectangle 类的新实例。

public Rectangle (int x, int y, int width, int height);

参数:x Int32 矩形左上角的 x 坐标。y Int32 矩形左上角的 y 坐标。width Int32 矩形的宽度。height Int32 矩形的高度。

二、常用的功能

1.判断两个矩形是否相交

判断连个矩形是否相交,返回值是布尔类型

Rectangle rectangle1 = new Rectangle(100,100,50,50);
Rectangle rectangle2 = new Rectangle(110,110,50,50);
bool isIntersect = rectangle1.IntersectsWith(rectangle2);

2.求出两矩形相交重叠处的矩形

两个矩形相交后,根据两个矩形的重叠处,得出一个新的矩形

Rectangle rectangle1 = new Rectangle(100,100,50,50);
Rectangle rectangle2 = new Rectangle(110,110,50,50);
Rectangle overlap = Rectangle.Intersect(rectangle1, rectangle2);

3.案例

上面这两个API,下面就用一个案例来展示

效果

上图可以看到,当两个图像重合后,就立马将重合的部分图像显示在旁边的小图中了,另外,我将是否重合的判断输出在控制台中了

界面设计

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 矩形
{public partial class Form1 : Form{public Form1(){InitializeComponent();}//图片列表private string Img1Path = Application.StartupPath + "\\test.jpg";//存储矩形列表List<Rectangle> PictureCuttingList = new List<Rectangle>();//边长int SideLength = 180;private void Form1_Load(object sender, EventArgs e){}private void Button_Examine_Click(object sender, EventArgs e){if (!System.IO.File.Exists(Img1Path)){Console.WriteLine("图片路径不存在:" + Img1Path);return;}Bitmap bitmaps = new Bitmap(Img1Path);Random random = new Random();PictureCuttingList.Clear();PictureBox_Coincide.Image = null;for (int i = 0; i < 2; i++){int x = random.Next(10, bitmaps.Width - SideLength);int y = random.Next(10, bitmaps.Height - SideLength);PictureCuttingList.Add(new Rectangle(new Point(x, y), new Size(SideLength, SideLength)));}//给图片画矩形PictureBox_Template.Image = PaintingRectangle(PictureCuttingList, bitmaps);//判断两个矩形是否相交Rectangle rectangle1 = PictureCuttingList[0];Rectangle rectangle2 = PictureCuttingList[1];bool isRect = rectangle1.IntersectsWith(rectangle2);Console.WriteLine("两个矩形是否相交:" + isRect);if (isRect){//两个矩形相交部分图片显示到小图中Rectangle overlap = Rectangle.Intersect(rectangle1, rectangle2);Bitmap newBitmap = new Bitmap(Img1Path);Bitmap cuttingBitmap = newBitmap.Clone(overlap, System.Drawing.Imaging.PixelFormat.DontCare);PictureBox_Coincide.Image = cuttingBitmap;}}/// <summary>/// 给图片画矩形/// </summary>/// <param name="rectanglesList"></param>/// <param name="bitmap"></param>public Bitmap PaintingRectangle(List<Rectangle> rectanglesList, Bitmap bitmap){Bitmap bit = null;for (int i = 0; i < rectanglesList.Count; i++){if (bit == null){bit = DrawRectangleInPicture(bitmap, rectanglesList[i], Color.Red, 4, "索引:" + i, 25);}else{Bitmap newBit = new Bitmap(bit);bit.Dispose();bit = DrawRectangleInPicture(newBit, rectanglesList[i], Color.Red, 4, "索引:" + i, 25);}}if (bit == null){Console.WriteLine("bit等于null");return null;}return bit;}/// <summary>/// 图片上画矩形和标记文字/// </summary>/// <param name="bmp">图片bitmap</param>/// <param name="imgRectangle">矩形</param>/// <param name="lineColor">线条的颜色</param>/// <param name="lineWidth">线条</param>/// <param name="text">矩形的文本</param>/// <param name="fontSize">字体大小</param>/// <param name="ds">线条的线型</param>/// <returns></returns>public Bitmap DrawRectangleInPicture(Bitmap bmp, Rectangle imgRectangle, Color lineColor, int lineWidth, string text, int fontSize, DashStyle ds = DashStyle.Solid){if (bmp == null) return null;if (imgRectangle == null) return null;if (lineColor == null) return null;if (lineWidth == 0) return null;if (fontSize == 0) return null;if (string.IsNullOrEmpty(text)) return null;Point pos = imgRectangle.Location;Size size = imgRectangle.Size;Graphics g = Graphics.FromImage(bmp);Brush brush = new SolidBrush(lineColor);Pen pen = new Pen(brush, lineWidth);pen.DashStyle = ds;//画坐标的原点(用于测试)g.DrawEllipse(pen, new Rectangle(pos.X, pos.Y, 15, 15));//画矩形//int rectX = pos.X - (size.Width / 2);//int rectY = pos.Y - (size.Height / 2);//g.DrawRectangle(pen, rectX, rectY, size.Width, size.Height);g.DrawRectangle(pen, pos.X, pos.Y, size.Width, size.Height);Font myFont = new Font("宋体", fontSize, FontStyle.Regular);Brush bush = new SolidBrush(lineColor);//填充的颜色//字体位置的计算SizeF sizeF = g.MeasureString(text, myFont);//int fontPosX = (int)(pos.X - (sizeF.Width / 2));//int fontPosY = (int)(pos.Y + (sizeF.Height / 2) + (size.Height / 2));//g.DrawString(text, myFont, bush, fontPosX, fontPosY);int fontPosX = (int)(pos.X + (size.Width / 2) - (sizeF.Width / 2));int fontPosY = (int)(pos.Y + (size.Height / 2) - (sizeF.Height / 2));g.DrawString(text, myFont, bush, fontPosX, fontPosY);g.Dispose();return bmp;}}
}

三、切图Demo

切图的demo和上面的案例的功能类似

界面

效果

上面gif图片的最后画面

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 裁切小图
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string Img1Path = Application.StartupPath + "\\test.jpg";List<PictureBox> PictureBoxesList = new List<PictureBox>();List<Rectangle> PictureCuttingList = new List<Rectangle>();//切割小图的宽度int ImgWidth = 180;private void Form1_Load(object sender, EventArgs e){TextBox_ImaPath.Text = Img1Path;PictureBoxesList.Add(pictureBox1);PictureBoxesList.Add(pictureBox2);PictureBoxesList.Add(pictureBox3);PictureBoxesList.Add(pictureBox4);PictureBoxesList.Add(pictureBox5);}/// <summary>/// 选择图片路径/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Button_SelectImg_Click(object sender, EventArgs e){OpenFileDialog dialog = new OpenFileDialog();dialog.Multiselect = true;//该值确定是否可以选择多个文件dialog.Title = "请选择文件";dialog.Filter = "所有文件(*.*)|*.*";if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){TextBox_ImaPath.Text = dialog.FileName;Img1Path = dialog.FileName;PictureBox_Template.Image = new Bitmap(Img1Path);}}/// <summary>/// 生成矩形图/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Button_Examine_Click(object sender, EventArgs e){if (!System.IO.File.Exists(Img1Path)){Console.WriteLine("图片路径不存在:" + Img1Path);return;}Bitmap bitmaps = new Bitmap(Img1Path);Random random = new Random();//Console.WriteLine(string.Format("总宽度:{0} 总高度:{1}", Bitmaps.Width, Bitmaps.Height));PictureCuttingList.Clear();for (int i = 0; i < 2; i++){int x = random.Next(10, bitmaps.Width - ImgWidth);int y = random.Next(10, bitmaps.Height - ImgWidth);//这样写矩形会超出图片范围,可以用来测试//int x = random.Next(10, Bitmaps.Width);//int y = random.Next(10, Bitmaps.Height);//Console.WriteLine(string.Format("索引值: {0} x: {1}, y: {2}", i, x, y));PictureCuttingList.Add(new Rectangle(new Point(x, y), new Size(ImgWidth, ImgWidth)));}//给图片画矩形PictureBox_Template.Image = PaintingRectangle(PictureCuttingList, bitmaps);}/// <summary>/// 开始切割/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Button_Cutting_Click(object sender, EventArgs e){if (!System.IO.File.Exists(Img1Path)){Console.WriteLine("图片路径不存在:" + Img1Path);return;}if (PictureCuttingList.Count == 0){Console.WriteLine("PictureCuttingList不能为空");return;}Bitmap bitmaps = new Bitmap(Img1Path);List<Bitmap> cuttingList = CuttingHandle(PictureCuttingList, bitmaps);if (cuttingList != null && cuttingList.Count > 0){for (int i = 0; i < cuttingList.Count; i++){PictureBoxesList[i].Image = cuttingList[i];}}}/// <summary>/// 给图片画矩形/// </summary>/// <param name="rectanglesList"></param>/// <param name="bitmap"></param>public Bitmap PaintingRectangle(List<Rectangle> rectanglesList, Bitmap bitmap){Bitmap bit = null;for (int i = 0; i < rectanglesList.Count; i++){if (bit == null){bit = DrawRectangleInPicture(bitmap, rectanglesList[i], Color.Red, 4, "索引:" + i, 25);}else{Bitmap newBit = new Bitmap(bit);bit.Dispose();bit = DrawRectangleInPicture(newBit, rectanglesList[i], Color.Red, 4, "索引:" + i, 25);}}if (bit == null){Console.WriteLine("bit等于null");return null;}return bit;}/// <summary>/// 裁切图片(切片的坐标以左上角为原点)/// </summary>/// <param name="pictureCuttingList">区域数据列表</param>/// <param name="bitmap">图片bitmap</param>/// <returns>切片的图片列表</returns>public List<Bitmap> CuttingHandle(List<Rectangle> pictureCuttingList, Bitmap bitmap){if (pictureCuttingList == null || pictureCuttingList.Count == 0){Console.WriteLine("[CuttingHandle]pictureCuttingList不能为空");return null;}if (bitmap == null){Console.WriteLine("[CuttingHandle]图片bitmap不能为空");return null;}List<Bitmap> bitmapList = new List<Bitmap>();for (int i = 0; i < pictureCuttingList.Count; i++){Rectangle rectangle = pictureCuttingList[i];if(rectangle == null){Console.WriteLine("[CuttingHandle]pictureCuttingList有值为空,index:" + i);return null;}//判断坐标是否在图片范围内Point pos = rectangle.Location;Size size = rectangle.Size;if(pos.X < 0 || pos.X + size.Width > bitmap.Width){Console.WriteLine("[CuttingHandle]当前区域X轴超出图片范围,索引是:" + i);return null;}if(pos.Y < 0 || pos.Y + size.Height > bitmap.Height){Console.WriteLine("[CuttingHandle]当前区域Y轴超出图片范围,索引是:" + i);return null;}Bitmap cuttingBitmap = bitmap.Clone(rectangle, System.Drawing.Imaging.PixelFormat.DontCare);bitmapList.Add(cuttingBitmap);}if (bitmapList.Count > 0)return bitmapList;return null;}/// <summary>/// 图片上画矩形和标记文字/// </summary>/// <param name="bmp">图片bitmap</param>/// <param name="imgRectangle">矩形</param>/// <param name="lineColor">线条的颜色</param>/// <param name="lineWidth">线条</param>/// <param name="text">矩形的文本</param>/// <param name="fontSize">字体大小</param>/// <param name="ds">线条的线型</param>/// <returns></returns>public Bitmap DrawRectangleInPicture(Bitmap bmp, Rectangle imgRectangle, Color lineColor, int lineWidth, string text, int fontSize, DashStyle ds = DashStyle.Solid){if (bmp == null) return null;if (imgRectangle == null) return null;if (lineColor == null) return null;if (lineWidth == 0) return null;if (fontSize == 0) return null;if (string.IsNullOrEmpty(text)) return null;Point pos = imgRectangle.Location;Size size = imgRectangle.Size;Graphics g = Graphics.FromImage(bmp);Brush brush = new SolidBrush(lineColor);Pen pen = new Pen(brush, lineWidth);pen.DashStyle = ds;//画坐标的原点(用于测试)g.DrawEllipse(pen, new Rectangle(pos.X, pos.Y, 15, 15));//画矩形//int rectX = pos.X - (size.Width / 2);//int rectY = pos.Y - (size.Height / 2);//g.DrawRectangle(pen, rectX, rectY, size.Width, size.Height);g.DrawRectangle(pen, pos.X, pos.Y, size.Width, size.Height);Font myFont = new Font("宋体", fontSize, FontStyle.Regular);Brush bush = new SolidBrush(lineColor);//填充的颜色//字体位置的计算SizeF sizeF = g.MeasureString(text, myFont);//int fontPosX = (int)(pos.X - (sizeF.Width / 2));//int fontPosY = (int)(pos.Y + (sizeF.Height / 2) + (size.Height / 2));//g.DrawString(text, myFont, bush, fontPosX, fontPosY);int fontPosX = (int)(pos.X + (size.Width / 2) - (sizeF.Width / 2));int fontPosY = (int)(pos.Y + (size.Height / 2) - (sizeF.Height / 2));g.DrawString(text, myFont, bush, fontPosX, fontPosY);g.Dispose();return bmp;}}
}

源码地址:点击下载

结束

如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢

end

C# Rectangle基本用法和图片切割相关推荐

  1. 一个非常好用的图片切割工具(c# winform开发)

    本人业余时间开发了一个图片切割工具,非常好用,也很灵活! 特别对大型图片切割,更能体现出该软件的优势! 功能说明 可以设定切割的高度和宽度.切割线可以上下拖动,可以增加一个切割区域,可设定某个区域不参 ...

  2. .netcf 图片区域拷贝[图片切割]

    一.效果图 二.实现代码         const int SRCCOPY = 0x00CC0020; /// <summary>         /// 拷贝图片的某一个区域,生成一个 ...

  3. python智能图片识别系统(图片切割、图片识别、区别标识)

    目录 技术介绍 运行效果 关键代码 写在最后 技术介绍 你好! python flask图片识别系统使用到的技术有:图片背景切割.图片格式转换(pdf转png).图片模板匹配.图片区别标识. 运行效果 ...

  4. python3 opencv 基于二值化图像素投影的图片切割方法

    对于一些背景纯色,结构相对简单的图,可以利用传统的opencv图像处理进行分割.先来记录一下基于二值化图像素投影的图片切割方法的实现.比如下面这张图,可以利用这个算法进行切割.(源代码在最后面) 切割 ...

  5. 将一个图片切割成多个图片

    有种场景,我们想将一个图片切割成多个图片.比如我们在开发一个拼图的游戏,就首先要对图片进行切割.  以下是封装好的两个类,可以实现图片的切割.仅供参考和学习.  一个是ImagePiece类,此类保存 ...

  6. C#图片处理类(颜色透明化,图片切割,图片合并,图片旋转等)(转)

                              目录 1.背景透明化 2.指定颜色透明化 3.指定颜色替换成另一种颜色 4.图片按比例缩放 5.图片旋转 6.图片更改透明度 7.图片添加文字 8. ...

  7. WPF纯手工两步打造图片切割工具(一)

    一.功能说明 1.四种图片切割方式:缩放:指定宽高(可能变形).缩放:指定宽(高按比例).缩放:指定高(宽按比例).裁减:指定宽高. 2.批量图片切割. 3.目标存储区同名文件处理:直接覆盖.重新命名 ...

  8. Android中将一个图片切割成多个图片[转]

    有种场景,我们想将一个图片切割成多个图片.比如我们在开发一个拼图的游戏,就首先要对图片进行切割. 以下是封装好的两个类,可以实现图片的切割.仅供参考和学习. 一个是ImagePiece类,此类保存了一 ...

  9. WPF纯手工两步打造图片切割工具(二)

    上一节已经完成了功能需求和界面布局,这一节就说明一下编码. 本文分两部分: (一)界面布局及数据初始化 (二)编码实现 1. 既然要求支持批量处理,那么一次就应该允许选择多个文件,在上一节的最后已经说 ...

最新文章

  1. oracle归档模式教程,Oracle从归档模式变成非归档模式详细步骤
  2. spyder安装_windows10 Anaconda3安装教程
  3. laravel-admin 关闭debug模式导致异常信息到页面的排查
  4. python函数五要素_Python安装及关键要素
  5. 【计算机图形学课程】一.MFC基本绘图函数使用方法
  6. 数据库原理与应用(SQL Server)笔记 第四章 嵌套查询和其他查询子句
  7. k-means k均值聚类的弱点/缺点
  8. python 图形_Python图形数据
  9. python自动化_Python 实现Excel自动化办公上
  10. 【XSY2469】graph 分治 并查集
  11. 物化视图常用维护操作
  12. java 类型 转换 valueOf和parse...
  13. 第一批富起来的人,正在悄悄变“穷”
  14. 终于开通了,呵呵,以后跟大家一起讨论
  15. 【优化求解】基于matlab遗传算法求解道路流量优化问题【含Matlab源码 1480期】
  16. web平台安装程序 无效的uri_计算机二级Web(1):Web技术基础(上)
  17. postgresql 客户端连接方式差异造成的时间差异
  18. Matlab如何下载安装科研绘图工具Gramm并绘图
  19. Excel 从入门到精通免费视频教程-值得收藏
  20. 安卓手机如何投屏mac苹果电脑上面?

热门文章

  1. df.plot实现多种图绘制
  2. java 通过xml控制ui_3.1.1 使用XML布局文件控制UI界面
  3. $timeformat
  4. PYQT Pushbutton 右键菜单
  5. 弹性体模拟(弹性力学)
  6. 基于JSP的公寓租赁系统
  7. java weka 聚类_# weka 聚类的使用
  8. vs下载问题:请检查网络连接
  9. 带网络功能的多媒体播放器
  10. Oracle的greatest和least函数