欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > WinForm中使用Graphics画元素

WinForm中使用Graphics画元素

2024/10/24 1:56:05 来源:https://blog.csdn.net/weixin_45114627/article/details/140845118  浏览:    关键词:WinForm中使用Graphics画元素

前言

有时候我们需要在一个图像上显示一些文字,或者画一些标志,这就想我们平时截图也需要做一些描述信息。在C#中我们可以Graphics这个对象来绘制自己所需要描述的信息,当然在WPF中的它的设计思路又不一样了,在WPf中考虑使用的矩形控件等元素进行标注。他的前台界面设计更加丰富。

注意:在这里仅是演示在WinForm中,通过PictureBox控件来画出一些元素。

一、效果展示

二、实现代码

1、主要调用方法

官方文档:Graphics 类 (System.Drawing) | Microsoft Learn

DrawLine(Pen, Point, Point)

绘制一条连接两个 Point 结构的线。

DrawPolygon(Pen, PointF[])

绘制由一组 PointF 结构定义的多边形。

DrawString(String, Font, Brush, PointF)

在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。

DrawRectangle(Pen, Int32, Int32, Int32, Int32)

绘制由坐标对、宽度和高度指定的矩形。

2、画线段
       //线private void button2_Click(object sender, EventArgs e){Graphics g = Graphics.FromImage(pictureBox1.Image);SolidBrush mybrush = new SolidBrush(Color.DarkKhaki);Pen pen = new Pen(mybrush, 10);g.DrawLine(pen, 200, 450, 950, 350);pictureBox1.Refresh();}
3、画多边形
 //多边形private void button4_Click(object sender, EventArgs e){Graphics g = Graphics.FromImage(pictureBox1.Image);SolidBrush mybrush = new SolidBrush(Color.DodgerBlue);Pen pen = new Pen(mybrush, 10);g.DrawPolygon(pen,new Point[] {new Point(250, 1550),new Point(1050, 1400) ,new Point(1300,1800), new Point(1500, 1200) });pictureBox1.Refresh();}
4、画矩形
 private void btn_append_Click(object sender, EventArgs e){Graphics g = Graphics.FromImage(pictureBox1.Image);SolidBrush mybrush = new SolidBrush(Color.DarkGreen);Pen pen = new Pen(mybrush,10);g.DrawRectangle(pen, new Rectangle(new Point(100, 200), new Size(200, 500)));pictureBox1.Refresh();}
5、写文字
 //文字private void button3_Click(object sender, EventArgs e){Graphics g = Graphics.FromImage(pictureBox1.Image);SolidBrush mybrush = new SolidBrush(Color.Red);Font myfont = new Font("黑体", 150);g.DrawString(tb_info.Text, myfont, mybrush, new Rectangle(x + 200, y + 50, w + 500, h + 50));pictureBox1.Refresh();}
6、保存结果图
   private void btn_save_Click(object sender, EventArgs e){saveFileDialog1.Filter= "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp";if (saveFileDialog1.ShowDialog()==DialogResult.OK){pictureBox1.Image.Save(saveFileDialog1.FileName);}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com