C# Graphic 绘制圆、三角形、椭圆、图片

您所在的位置:网站首页 c语言画个圆 C# Graphic 绘制圆、三角形、椭圆、图片

C# Graphic 绘制圆、三角形、椭圆、图片

2024-05-21 13:37| 来源: 网络整理| 查看: 265

在form和panel上可以绘制图形,线段,圆,文字,图形等等。 绘制代码必须放在OnPaint()函数里面,因为窗体刷新的时候,都会调用该函数,重新刷新所绘的图。 示例代码在Panel上绘制图形来简单的描述下绘线和绘图原理。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Reflection; namespace TestGraphic { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void panel1_Paint(object sender, PaintEventArgs e) { Graphics gc = e.Graphics; /// 设置绘图的颜色 Brush greenBrush = new SolidBrush(Color.Green); int radius = 30; // 绘制圆,(0, 0)为左上角的坐标,radius为直径 gc.FillEllipse(greenBrush, 0, 0, radius, radius); Brush yellowBush = new SolidBrush(Color.Yellow); // 绘制椭圆,其实圆时椭圆的特殊的一种,即两个定点重合, (50, 60)为左上角的坐标, // 70位椭圆的宽度,100位椭圆的高度 gc.FillEllipse(yellowBush, 50, 60, 70, 100); // 绘制三角形,指定红色和线宽5。三个顶点为(150,160) (200, 210) (280, 180),绘制三条连线。 Pen pen = new Pen(Color.Red, 5); gc.DrawLine(pen, 150, 160, 200, 210); gc.DrawLine(pen, 200, 210, 280, 180); gc.DrawLine(pen, 150, 160, 280, 180); /// 绘制矩形,(50,300)左上角坐标,110位宽度, 80为高度。 gc.DrawRectangle(pen, 50, 300, 110, 80); Brush blueBrush = new SolidBrush(Color.Blue); /// 绘制文本 gc.DrawString("Graphic绘制图形的例子", new Font("宋体", 20, FontStyle.Italic), blueBrush, new PointF(300, 400)); /// 绘制图片 TestGraphic.Properties.Resources.niang为图片在资源中的名称,可以先将图片设置为Panel的背景图, /// 获得图片的名称,然后将Panel的背景图清空。(400,20)是图片左上角坐标,300,300是图片将要显示的宽度和高度, /// 并不是图片本身的宽度和高度。 Image image = global::TestGraphic.Properties.Resources.niang; gc.DrawImage(image, new Rectangle(400, 20, 300, 300)); } } }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3