C#GDI 绘图 各种方法代码C#GDI 绘图 各种方法代码C#GDI 绘图 各种方法代码
代码片段和文件信息
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.Drawing.Drawing2D;
using System.Drawing.Text;
namespace GDI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//(1)从窗口的指定句柄创建新的 Graphics对象。
// public static Graphics FromHwnd(IntPtr hwnd)
//(2) 从指定的 Image 创建新的 Graphics 对象。
// public static Graphics FromImage(Image image)
//(3) 从设备上下文的指定句柄创建新的 Graphics 对象。
// public static Graphics FromHdc(IntPtr hdc)
// public static Graphics FromHdc(IntPtr hdcIntPtr hdevice)
// (4) 为控件创建 Graphics 对象。
// public Graphics CreateGraphics()
private void button1_Click(object sender EventArgs e)
{
//从窗体的句柄创建 Graphics 对象
Graphics g = Graphics.FromHwnd(this.Handle);
g.Clear(Color.Black);
//创建用于绘制图像使用的画笔
Pen myPen = new Pen(Color.Red);
//画矩形
g.DrawRectangle(myPen 20 20 200 100);
//画直线
g.DrawLine(myPen 20 250 200 250);
//画椭圆
g.DrawEllipse(myPen new Rectangle(250 20 150 100));
myPen.Dispose();
g.Dispose();
}
private void Form1_Load(object sender EventArgs e)
{
}
private void button2_Click(object sender EventArgs e)
{
//创建Graphics对象
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
//创建画笔,第一个参数为颜色,第二个参数为画笔宽度,可选参数,默认为一
Pen p = new Pen(Color.Black 2);
//绘制矩形
g.DrawRectangle(p 50 50 200 100);
//重新设置画笔颜色
p.Color = Color.Green;
//重新设置画笔宽度
p.Width = 10;
//绘制椭圆
g.DrawEllipse(p 50 200 200 100);
//释放对象
p.Dispose();
g.Dispose();
}
private void button3_Click(object sender EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
//默认为实线
Pen p = new Pen(Color.Red 3);
g.DrawRectangle(p 50 50 200 100);
//设置画笔线形
p.Dashstyle = Dashstyle.DashDotDot;
g.DrawRectangle(p 50 200 200 100);
//使用自定义线形
float[] dashArray = {
5.0f//线长5像素
2.0f//间断2像素
15.0f//线长15像素
8.0f//间断4像素
};
p.DashPattern = dashArray;
p.Color = Color.Black;
g.DrawLine(p 280 100 500 100);
//释放资源
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)