您的位置:首页 > 教育 > 培训 > C# 图形图像技术(通过Graphics绘制图像)

C# 图形图像技术(通过Graphics绘制图像)

2025/4/17 11:43:57 来源:https://blog.csdn.net/weixin_51169222/article/details/140825220  浏览:    关键词:C# 图形图像技术(通过Graphics绘制图像)

文章目录

  • 创建Graphics对象
  • 画笔与画刷
    • 画笔
    • 画刷
      • SolidBrush类
      • HatchBrush类
      • LinerGradientBrush类
  • 基本图形绘制
    • 矩形
    • 椭圆
    • 圆弧
    • 扇形

创建Graphics对象

private  void Form1_Load(object sender,Eventargs e)
{Graphics ghs = this.CreateGraphics();
}

画笔与画刷

画笔

构造函数

public Pen(Color color,float width);
//color:设置Pen的颜色。
//width:设置Pen的宽度。
Pen p=new Pen(Color.Blue,2);//实例化一个Pen类,并设置其颜色为蓝色,宽度为2

画刷

SolidBrush类

构造函数

public SolidBrush(Color color);
//color:设置画刷的颜色。

HatchBrush类

HatchBrush类提供一种特定样式的图形,用来实现填满整个封闭区域的绘图效果。

构造函数

public HatchBrush(HatchStyle hatchstyle,Color foreColer);
//hatchstyle:表示此HatchBrush绘制图案。
//foreColer:表示此HatchBrush绘制的线条颜色。

例 绘制阶梯

 private void button1_Click(object sender, EventArgs e){Graphics ghs=this.CreateGraphics();for (int i = 0; i < 6; i++) { HatchStyle style = (HatchStyle)(5+i);HatchBrush hatchBrush = new HatchBrush(style,Color.White);Rectangle rectangle = new Rectangle(10,50*i,50*i,50);ghs.FillRectangle(hatchBrush, rectangle);}}

LinerGradientBrush类

LinerGradientBrush类提供一种渐变色彩的特效,填满图形的内部区域。

构造函数

public LinerGradientBrush(Point p1,Point p2,Color color1,Color color2)
//p1:表示线性渐变的起始点
//p2:表示线性渐变的结束点
//color1:表示线性渐变的开始色彩
//color2:表示线性渐变的结束色彩

 private void button2_Click(object sender, EventArgs e){Point p1= new Point(150,150);Point p2= new Point(300,300);LinearGradientBrush lgb = new LinearGradientBrush(p1, p2, Color.Blue, Color.Green);Graphics ghs = this.CreateGraphics();lgb.WrapMode=WrapMode.TileFlipX;ghs.FillRectangle(lgb,15,50,300,300);}

基本图形绘制

矩形

构造函数

public void DrawRectangle(Pen pen,int x,int y,int width,int height);
//pen:Pen对象,用于指定矩形的颜色、宽度、和样式
//x:矩形左上角的X坐标
//y:矩形左上角的Y坐标
//width:矩形的宽度
//height:矩形的高度

 private void button3_Click(object sender, EventArgs e){Graphics graphics = this.CreateGraphics();Pen myPen=new Pen(Color.Black,8);graphics.DrawRectangle(myPen, 15, 50, 300, 300);}

椭圆

构造函数

public void DrawEllipse(Pen pen,int x,int y,int width,int height);
//pen:Pen对象,用于指定曲线的颜色、宽度、和样式
//x:椭圆边框左上角的X坐标
//y:椭圆边框左上角的Y坐标
//width:椭圆边框的宽度
//height:椭圆边框的高度

private void button4_Click(object sender, EventArgs e)
{Graphics graphics = this.CreateGraphics();Pen myPen = new Pen(Color.Black, 3);graphics.DrawEllipse(myPen, 15, 50, 300, 300);
}

圆弧

构造函数

public void DrawArc(Pen pen,Rectangle rect,float startAngle,float sweepAngle);
//pen:Pen对象,用于指定曲线的颜色、宽度、和样式
//rect:Rectangle结构,用于定义弧的边界
//startAngle:从X轴到弧线的起始点,沿顺时针方向度量的角(以度为单位)
//sweepAngle:从startAngle参数到弧线的结束点,startAngle

 private void button5_Click(object sender, EventArgs e){Graphics graphics = this.CreateGraphics();Pen myPen = new Pen(Color.Black, 3);Rectangle rectangle=new Rectangle(15,50,100,60);graphics.DrawArc(myPen, rectangle, 150, 200);}

扇形

构造函数

public void DrawPie(Pen pen,float x,float y,float width,float height,float startAngle,float); sweepAngle);
//pen:Pen对象,用于指定曲线的颜色、宽度、和样式
//x:边框左上角的X坐标,该边框用于定义扇形所属的椭圆
//y:边框左上角的Y坐标,该边框用于定义扇形所属的椭圆
//width:边框的宽度
//height:边框的高度
//startAngle:从X轴到扇形的起始点,沿顺时针方向度量的角(以度为单位)
//sweepAngle:从startAngle参数到扇形的第二条边,startAngle

版权声明:

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

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