用 C picturebox 控件画图

用 C picturebox 控件画图今天介绍一下 C 用 picturebox 画坐标系编译环境 VS20101 首先建立一个 Windows 窗体应用程序如下图 1 所示建好后 就会出现一个窗体设计界面 可以在属性栏更改其名字 图标 Text 如下图 2 所示 2 添加控件 picturebox 和 button 如图 3 所示 可根据需要更改名字 3 双击 绘图 按钮添加事件响应函数 在此之前 首先要明白

一、19/7/18版本

图1
建好后,就会出现一个窗体设计界面,可以在属性栏更改其名字,图标,Text……如下图2所示:
图2
2.添加控件 “picturebox” 和 “button” 如图3所示(可根据需要更改名字):
图3
3.双击 “绘图” 按钮添加事件响应函数;
在此之前,首先要明白如何从绘图坐标系转换为我们常用的坐标系,如下图3所示:
黑色的是绘图坐标系,红色的是我们常用的坐标系,简单画下图就可得到图中所示公式
图3
本来想画个写轮眼,可最后成了下图4的样子(笑哭):


















图4
要画好看一点要仔细研究点在哪里,这里就简单介绍一下在 picturebox 中怎样画线,画椭圆,“绘图按钮”点击函数中有:

private void button1_Click(object sender, EventArgs e) { Graphics g1 = pictureBox1.CreateGraphics();//画布 g1.Clear(Color.White); Pen p = new Pen(Color.Red, 2);//画笔 Pen p1 = new Pen(Color.Black, 2); Pen p2 = new Pen(Color.Brown, 5); Pen p3 = new Pen(Color.Black, 20); Font f = new Font("宋体", 10);//字体 int a = pictureBox1.Size.Height; int b = pictureBox1.Size.Width; g1.DrawLine(p1, 0, a, 0, 0);//画线 g1.DrawLine(p1, 0, a, b, a); g1.DrawLine(p1, 0, 0, 5, 10); g1.DrawLine(p1, b, a, b - 10, a - 5); g1.DrawString("O", f, Brushes.Black, 3, a - 20); g1.DrawString("正北X", f, Brushes.Black, 3, 3); g1.DrawString("正东Y", f, Brushes.Black, b - 50, a - 20); Point1[] Pt=new Point1[20]; Point1[] Pt1 = new Point1[20]; Pt[0] = new Point1("0", 400, 200); Pt[1] = new Point1("1", 250, 250); Pt[2] = new Point1("2", 200, 400); Pt[3] = new Point1("3", 150, 250); Pt[4] = new Point1("4", 0, 200); Pt[5] = new Point1("5", 150, 150); Pt[6] = new Point1("6", 200, 0); Pt[7] = new Point1("7", 250, 150); Pt[9] = new Point1("8",200,a-200); int i = 0; for (i = 0; i < 8; i++) { Pt1[i] = new Point1(Pt[i].name, Pt[i].y, a - Pt[i].x); } g1.FillEllipse(Brushes.Black, Pt[9].x-200, Pt[9].y-200, 400, 400); g1.FillEllipse(Brushes.Red, Pt[9].x - 170, Pt[9].y - 170, 340, 340); g1.FillEllipse(Brushes.Brown, Pt[9].x - 120, Pt[9].y - 120, 240, 240); g1.FillEllipse(Brushes.Red, Pt[9].x - 105, Pt[9].y - 105, 210, 210); g1.FillEllipse(Brushes.Black, Pt[9].x - 80, Pt[9].y - 80, 160, 160); g1.FillEllipse(Brushes.Red, Pt[9].x - 65, Pt[9].y - 65, 130, 130); g1.FillEllipse(Brushes.Black, Pt[9].x - 25, Pt[9].y - 25, 50, 50); i = 0; while (Pt1[i + 1] != null) { g1.DrawLine(p2, Pt1[i].x, Pt1[i].y, Pt1[i + 1].x, Pt1[i + 1].y); i++; } g1.DrawLine(p2, Pt1[0].x, Pt1[0].y, Pt1[i].x, Pt1[i].y); i = 1; while (i<=7) { g1.FillEllipse(Brushes.Black, Pt1[i].x - 25, Pt1[i].y - 25, 60, 60); g1.FillEllipse(Brushes.Red, Pt1[i].x - 20, Pt1[i].y - 20, 40, 40); //g1.DrawString(Pt1[i].name, f, Brushes.Black, Pt1[i].x - 5, Pt1[i].y + 5); i=i+2; } g1.DrawLine(p3, Pt1[0].x, Pt1[0].y, 200, a-280); g1.DrawLine(p3, Pt1[2].x, Pt1[2].y, 280, a - 200); g1.DrawLine(p3, Pt1[4].x, Pt1[4].y, 200, a - 120); g1.DrawLine(p3, Pt1[6].x, Pt1[6].y, 120, a - 200); g1.Dispose(); } 

4.双击“清空画布”按钮,添加响应事件:

private void button2_Click(object sender, EventArgs e) { Graphics g = pictureBox1.CreateGraphics(); g.Clear(Color.White); } 

点击运行,OK!

二、20/6/13版本

 private void Draft_Paint(object sender, PaintEventArgs e) { 
    } private void pB_Paint(object sender, PaintEventArgs e) { 
    } 

Graphics 的属性和方法有很多,可以看看:

MSDN:https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics?view=netcore-3.1

本来想截个长图放这里的,可是那实在是太长辽,朋友们可以自己去读一读。这里就简单介绍几个常用的绘图方法吧

1.预备知识

1.1 Color

public struct Color : IEquatable<System.Drawing.Color> 

系统自带了好多颜色:

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.color?view=netcore-3.1

1.2 GraphicsPath

public sealed class GraphicsPath : MarshalByRefObject, ICloneable, IDisposable 

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.drawing2d.graphicspath?view=netcore-3.1

1.3 Single

public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable 

2.一些方法简介

1.清除整个绘图面并以指定背景色填充。 public void Clear (System.Drawing.Color color); 2.释放此 Graphics 使用的所有资源。 public void Dispose ();//前面这俩应该是绘图必用的 3.绘制一段弧线,它表示由一对坐标、宽度和高度指定的椭圆部分。 DrawArc //有四个重载,吐槽一下,和我的脑回路很不同,很费解,可以看看我之前写的一篇博客(链接见下) 4.绘制由 4 个 Point 结构定义的贝塞尔样条。 DrawBezier(Pen, Point, Point, Point, Point);//3重载,挺高大上的,目前没用过 5.绘制由 Point 结构的数组定义的闭合基数样条。 DrawClosedCurve(Pen, Point[], Single, FillMode) 6.绘制经过一组指定的 Point 结构的基数样条。 DrawCurve(Pen, Point[]) 7.绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆。 DrawEllipse(Pen, Rectangle) 8.在指定坐标处绘制由指定的 Icon 表示的图像。 DrawIcon(Icon, Int32, Int32) 9.在指定位置并且按原始大小绘制指定的 Image。 DrawImage 10.绘制一条连接由坐标对指定的两个点的线条。 DrawLine(Pen, PointF, PointF) 11.绘制一系列连接一组 Point 结构的线段。 DrawLines(Pen, PointF[]) 12.绘制 GraphicsPath。 public void DrawPath (System.Drawing.Pen pen, System.Drawing.Drawing2D.GraphicsPath path); 13.绘制一个扇形,该形状由一个坐标对、宽度、高度以及两条射线所指定的椭圆定义。 DrawPie(Pen, Single, Single, Single, Single, Single, Single) 14.绘制由一组 Point 结构定义的多边形。 DrawPolygon(Pen, PointF[]) 15.绘制由坐标对、宽度和高度指定的矩形。 DrawRectangle(Pen, Single, Single, Single, Single) 16.绘制一系列由 Rectangle 结构指定的矩形。 DrawRectangles(Pen, Rectangle[]) 17.在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。 DrawString(String, Font, Brush, Single, Single, StringFormat) 18.填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定。 FillEllipse(Brush, Single, Single, Single, Single) 19.填充由一对坐标、一个宽度、一个高度以及两条射线指定的椭圆所定义的扇形区的内部。 FillPie(Brush, Single, Single, Single, Single, Single, Single) 20.填充 Point 结构指定的点数组所定义的多边形的内部。 FillPolygon(Brush, PointF[]) 21.填充由一对坐标、一个宽度和一个高度指定的矩形的内部。 FillRectangle(Brush, Rectangle) 22.填充 Region 的内部。 public void FillRegion (System.Drawing.Brush brush, System.Drawing.Region region); 23.测量用指定的 Font 绘制的指定字符串。 MeasureString(String, Font) 

DrawArc :https://blog.csdn.net/Gou_Hailong/article/details/

3.使用示例

将这些代码贴到Paint 函数中

 Graphics g = e.Graphics; g.Clear(Color.White);//1 PointF[] pt = new PointF[4]; pt[0] = new PointF(50, 50); pt[1] = new PointF(50, 100); pt[2] = new PointF(70, 110); pt[3] = new PointF(100, 100); // create pen. Pen p = new Pen(Color.Blue, 2); g.DrawBezier(p, pt[0], pt[1], pt[2], pt[3]);//4 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60;} g.DrawClosedCurve(p, pt);//5 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.DrawCurve(p, pt);//6 g.DrawEllipse(p, 100, 100, 60, 100);//7 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.DrawLine(p, pt[1], pt[3]);//10 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.DrawPie(p, pt[0].X, pt[0].Y, 60, 50, 60, 250);//13 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.DrawPolygon(p, pt);//14 p.Color = Color.Yellow; for (int i = 0; i < 4; i++) { 
    pt[i].Y -= 60 * 5; pt[i].X += 60; } g.DrawRectangle(p, pt[3].X, pt[3].Y, 100, 50);//15 for (int i = 0; i < 4; i++) { 
    pt[i].X += 60; } Font f = new Font("宋体", 15); Brush b = Brushes.Cyan; g.DrawString("Stay Hungry", f, b, pt[2]);//17 b = Brushes.DarkOrange; for (int i = 0; i < 4; i++) { 
    pt[i].X += 60; } g.FillEllipse(b, pt[1].X, pt[1].Y, 20, 20);//18 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.FillPie(b, pt[3].X, pt[3].Y, 60, 50, 60, 250);//19 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.FillPolygon(b, pt);//20 for (int i = 0; i < 4; i++) { 
    pt[i].Y += 60; } g.FillRectangle(b, pt[3].X, pt[3].Y, 100, 50);//21 g.Dispose();//2 

4.问题

参考

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics?view=netcore-3.1

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215233.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午2:22
下一篇 2026年3月18日 下午2:23


相关推荐

  • [SUS]SAP ERP传入SUS的采购订单查询不到的问题原因及解决方案

    [SUS]SAP ERP传入SUS的采购订单查询不到的问题原因及解决方案

    2021年9月6日
    73
  • pycharm安装教程2020.3_如何下载安装eclipse

    pycharm安装教程2020.3_如何下载安装eclipse简介PyCharm是一款PythonIDE,其带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如,调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制等等。此外,该IDE提供了一些高级功能,以用于支持Django框架下的专业Web开发。安装1.进入Pycharm官网的下载地址:链接:http://www.jetbrains.com/pycharm/download/#section=windows.2.professional表示专业

    2022年8月27日
    8
  • Pytorch优化器全总结(一)SGD、ASGD、Rprop、Adagrad

    Pytorch优化器全总结(一)SGD、ASGD、Rprop、Adagrad这是一个系列 以 Pytorch 为例 介绍所有主流的优化器 如果都搞明白了 对优化器算法的掌握也就差不多了 作为系列的第一篇文章 本文介绍 Pytorch 中的 SGD ASGD Rprop Adagrad 其中主要介绍 SGD 和 Adagrad 因为这四个优化器出现的比较早 都存在一些硬伤 而作为现在主流优化器的基础又跳不过 所以作为开端吧

    2026年3月18日
    3
  • 中标麒麟系统u盘安装_U盘启动中标麒麟V6双系统安装教程

    中标麒麟系统u盘安装_U盘启动中标麒麟V6双系统安装教程U 盘启动中标麒麟 V6 双系统安装教程本教程是双系统教程 一般是安装 XP win7 的机器需要安装中标麒麟的朋友使用 教程内容都是在网上找到相关资料结合自己经验编写 以供需要的朋友参考 一 准备工作 1 U 盘一个 2G 以上 2 下载中标麒麟 V6ISO3 下载 UltraISO 二 制作启动 U 盘 1 打开中标麒麟 V6ISO rar 和 UltraISO 都可以打开 推荐用 UltraISO 打开 image0012 提

    2026年3月26日
    2
  • mysql锁表原因及如何处理_mysql备份数据库命令

    mysql锁表原因及如何处理_mysql备份数据库命令解决mysql锁表的终极方法案例一mysql>showprocesslist;参看sql语句,一般少的话mysql>killthread_id;就可以解决了,kill掉第一个锁表的进程,依然没有改善。既然不改善,就想办法将所有锁表的进程kill掉吧,简单的脚本如下:复制代码代码如下:#!/bin/bashmysql-uroot-e”showprocesslist”|grep-i”L…

    2022年8月23日
    7
  • 快速排序基本思路(通俗易懂+例子)「建议收藏」

    快速排序基本思路(通俗易懂+例子)「建议收藏」快速排序今天看到大神写的一篇快速排序的博客,肃然起敬,觉得原来快速排序这么简单下面进行简单的试试快速排序的基本思想是1、先从数列中取出一个数作为基准数2、分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边3、再对左右区间重复第二步,直到各区间只有一个数概括来说为挖坑填数+分治法下面举例来进行说明,主要有三个参数,i为区间的开始地址,j为区间

    2022年6月15日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号