C# GDI+ 绘图

1 坐标系统

(1) 坐标原点:在窗体或控件的左上角,坐标为(0,0)

(2) 正方向:X轴正方向为水平向右,Y轴正方向为竖直向下

(3) 单位:在设置时,一般以像素为单位,像素(Pixel)是由图像(Picture)和元素(Element)组成,是用来计算数码影像的一种单位。

把影像放大数倍,会发现这些连续的色调其实是有许多色彩相近的小方点组成,这些小方点是构成影像的最小单位—像素。

图形的质量是有像素决定,像素越大,分辨率也越大。

2 命名空间 --- System.Drawing

(1) System.Drawing 提供了对GDI+基本图形功能的访问

(2)  System.Drawing 常用基本类及结构



说明


Bitmap


用于处理有像素数据定义的图像的对象。


Brush


定义用于填充图形形状的内部对象。


Font


定义特定的文本格式。


Graphics


封装一个GDI+绘图图画,无法继承此类。


Pen


用于绘制直线和曲线的对象,无法继承此类。


Region


指示由矩形和路径构成的图形形状的内部,无法继承此类。


Color


表示RGB颜色。


Point


定义二维平面中定义的点。


Rectangle


存储一组整数,共4个,表示一个矩形的位置和大小。


Size


存储一个有序整数对,通常为矩形的宽和高。

3 Graphics类

Graphics类封装了一个GDI+绘制界面,提供将对象绘制到显示界面的方法。使用GDI+创建图形图像时,需要先创建Graphics对象,即在哪里画图。

共有3种类型的绘图界面:

(1)   窗体和控件

(2)   打印机

(3)   内存的位图

创建图形对象的3中方法:

(1)控件类的OnPaint()方法参数PaintEventArgs获取Graphics对象

(2)窗体类或控件类中的CreateGraphics()方法获得Graphics对象

(3)从位图对象(Bitmap)产生一个Graphics对象

Graphics类的常用方法


名称


说明


Dispose


释放Graphics使用的所有资源。


DrawEllipse


绘制椭圆,有高度,宽度,一对坐标。


DrawArc


绘制弧形。


DrawLine


绘制一条直线,由2个点指定。


DrawPolygon


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


DrawRectangle


绘制矩形。


DrawPie


绘制一个扇形。


DrawCurse


绘制曲线,由参数Point数组指定。


FillEllipse


填充边框所定义的椭圆的内部。


FillRegion


填充Region的内部。


ScaleTransform


将制定的缩放操作应用于次Graphics。


TanslateTransform


平移更改坐标系统的原点。

4 绘图工具类


类名


说明


Pen


设置画笔的颜色,线条粗细和线条样式(实线和虚线)。


Brush


用于填充图形,设置笔刷的样式,颜色及线条的粗细。

5 Brush类的派生类


名称


说明


ImageBrush


图像绘制区域。


LinearGradientBrush


线性渐变绘制区域。


RadialGradientBrush


径向渐变绘制区域,焦点定义渐变的开始,椭圆定义渐变的终点。


SolidColorBrush


单色绘制区域。


VideoBrush


视频内容绘制区域。

6 案例  免费下载地址 http://download.csdn.net/detail/taoerit/8350869

7  代码

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;

namespace GDI绘图
{
    public partial class MainDialog : Form
    {
        public MainDialog()
        {
            InitializeComponent();
        }

        private void MainDialog_Load(object sender, EventArgs e)
        {

        }

        private void lineButton_Click(object sender, EventArgs e)
        {
            // 画直线
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            pen.Width = 2;
            Point startPoint = new Point(20,20);
            Point endPoint = new Point(70,20);
            gra.DrawLine(pen,startPoint,endPoint);

            pen.Dispose();
            gra.Dispose();
        }

        private void rectangleButton_Click(object sender, EventArgs e)
        {
            //画矩形
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            gra.DrawRectangle(pen, 20,50, 100,100);
            pen.Dispose();
            gra.Dispose();
        }
        private void cyliderButton_Click(object sender, EventArgs e)
        {
            //圆柱体,有许多个椭圆有底部逐渐叠起来的,最后填充颜色

            int height = this.ClientSize.Height - 150;
            int width = this.ClientSize.Width - 50;
            int vHeight = 200;
            int vWidth = 100;
            Graphics gra = this.CreateGraphics();
            gra.Clear(Color.White);
            Pen pen = new Pen(Color.Gray,2);
            SolidBrush brush = new SolidBrush(Color.Gainsboro);

            for (int i = height / 2; i > 0;i-- )
            {
                gra.DrawEllipse(pen,width/2,i,vHeight,vWidth);
            }

            gra.FillEllipse(brush,width/2,0,vHeight,vWidth);
        }

        private void fillRectangleButton_Click(object sender, EventArgs e)
        {
            //画矩形
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red,3);
            Brush brush = pen.Brush;
            Rectangle rect = new Rectangle(20,50,100,100);
            gra.FillRectangle(brush,rect);
            gra.Dispose();
        }

        private void drawEllispeButton_Click(object sender, EventArgs e)
        {
            Graphics gra = this.CreateGraphics();
            Rectangle rect = new Rectangle(0,0,200,100);
            LinearGradientBrush brush = new LinearGradientBrush(rect,Color.Orange,Color.Purple,90);
            gra.FillEllipse(brush,rect);
            gra.Dispose();
        }

        private void fontButton_Click(object sender, EventArgs e)
        {
            Graphics gra = this.CreateGraphics();
            Font font = new Font("隶书",24,FontStyle.Italic);
            Pen pen = new Pen(Color.Blue,3);
            gra.DrawString("Windows应用程序设计",font,pen.Brush,10,100);
        }

        private void ellispeButton_Click(object sender, EventArgs e)
        {
            // 画圆形
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            gra.DrawEllipse(pen, 0, 0, 200,100);
            pen.Dispose();
            gra.Dispose();
        }

        private void moveEllispeButton_Click(object sender, EventArgs e)
        {
            // 移动圆形
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            gra.TranslateTransform(10,10);// 改变起坐标(10,10)
            gra.DrawEllipse(pen, 0, 0, 200, 100);

            gra.Dispose();
        }

        private void scaleEllispeButton_Click(object sender, EventArgs e)
        {
            // 缩放圆形
            float xScale = 1.5F;
            float yScale = 2F;
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            gra.ScaleTransform(xScale, yScale);// X轴放大1.5倍, Y轴放大2倍
            gra.DrawEllipse(pen, 0, 0, 200, 100);
            gra.Dispose();
        }

        private void curveButton_Click(object sender, EventArgs e)
        {
            //绘制曲线
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Blue,3);
            Point oo1 = new Point(30,this.ClientSize.Height -100);
            Point oo2 = new Point(this.ClientSize.Width - 50 ,this.ClientSize.Height - 100);
            gra.DrawLine(pen,oo1,oo2);
            Point oo3 = new Point(30, 30);
            gra.DrawLine(pen, oo1, oo3);
            Font font = new System.Drawing.Font("宋体",12,FontStyle.Bold);
            gra.DrawString("X",font,pen.Brush,oo2);
            gra.DrawString("Y", font,pen.Brush,10,10);

            int x1 = 0, x2 = 0;
            double a = 0;
            double y1 = 0, y2 = this.ClientSize.Height - 100;
            for (x2 = 0; x2 < this.ClientSize.Width;x2++ )
            {
                a = 2 * Math.PI * x2 / (this.ClientSize.Width);
                y2 = Math.Sin(a);
                y2 = (1 - y2) *(this.ClientSize.Height-100)/2;
                gra.DrawLine(pen,x1 +30,(float)y1 ,x2+30,(float)y2);
                x1 = x2;
                y1 = y2;
            }
            gra.Dispose();
        }

        private void piechartButton_Click(object sender, EventArgs e)
        {
            //饼图
            Graphics gra = this.CreateGraphics();
            Pen pen = new Pen(Color.Blue, 3);
            Rectangle rect = new Rectangle(50,50,200,100);
            Brush brush = new SolidBrush(Color.Blue);
            gra.FillPie(pen.Brush,rect,0,60);
            gra.FillPie(brush,rect,60,150);
            brush = new SolidBrush(Color.Yellow);
            gra.FillPie(brush,rect,210,150);

        }

    }
}
时间: 2024-08-08 09:22:28

C# GDI+ 绘图的相关文章

GDI+ 绘图闪烁解决方法

闲着没事,准备做一个类似于TeeChart的自定义控件,结果第一步的绘图就把我给难倒了,虽然早就知道GDI绘图的闪烁问题很坑,但是却没有想到如此之坑,折腾了两天,才找到解决方法. 首先在窗体加载的时候,加入双缓存,说实话以前一直没觉得这个双缓存有什么用,不过这次总算是有了点儿效果. DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlSt

VS2013中使用GDI+绘图

VC范例,400多个例子源代码下载 http://download.csdn.net/detail/bigtree_mfc/7727977 VS2013中使用GDI+绘图和VC6.0不同,在VC6.0中能绘出的图像在VS2013中不会显示,原因就是在VS2013中需要添加初始化GDI+: 绘图 对话框视图类中:(绘图部分大同小异,) void **View::OnDraw(CDC *pDC){ //初始化部分 GdiplusStartupInput gdiplusStartupInput; UL

C#-gdi绘图,双缓冲绘图,Paint事件的触发---ShinePans

在使用gdi技术绘图时,有时会发现图形线条不够流畅,或者在改变窗体大小时会闪烁不断的现象.(Use DoubleBuffer to solve it!)                                                                                                                                                                              

MFC GDI绘图基础

一.关于GDI的基本概念 什么是GDI? Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Windows操作系统中,动态链接库C:/WINDOWS/system32/gdi32.dll(GDI Client DLL)中定义了GDI函数,实现与设备无关的包括屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面功能.在Visual C++6.0中的头文件C:/Program Files

Windows GDI绘图基础知识

一.Windows可以画直线.椭圆线(椭圆圆周上的曲线)和贝塞尔曲线.////////////7 个画线函式是:(1)画直线LineTo    BOOL LineTo(HDC hdc,int nXEnd,int nYEnd);结合MoveToEx函数使用BOOL MoveToEx(HDC hdc,int X,int Y,LPPOINT lpPoint);Point记录了旧的坐标点(先前的当前位置).///注意:GetCurrentPositionEx (hdc, &pt) ;获得当前位置. (2

MFC中的GDI绘图&lt;转&gt;

一.关于GDI的基本概念 什么是GDI? Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Windows操作系统中,动态链接库C:\WINDOWS\system32\gdi32.dll(GDI Client DLL)中定义了GDI函数,实现与设备无关的包括屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面功能.在Visual C++6.0中的头文件C:\Program Files

GDI绘图中的映射模式CDC::SetMapMode()

原文链接:http://blog.csdn.net/charlessimonyi/article/details/8264572 在GDI绘图前,一般要设置映射模式.映射模式是什么呢?它是逻辑长度单位与实际长度单位的一中转换关系,比如我要画一个长为800单位,宽为600单位的矩形,那么在不同的映射模式下,一个单位可能相当于一个像素,也可能相当于一毫米,也可能相当于一微米.也就是说在有的映射模式下,我们指定的800单位的长度相当于800像素,在有的映射模式下,我们指定的800单位的长度相当于800

【VS2013中使用GDI+绘图】

VS2013中使用GDI+绘图和VC6.0不同,在VC6.0中能绘出的图像在VS2013中不会显示,原因就是在VS2013中需要添加初始化GDI+: 绘图 对话框视图类中:(绘图部分大同小异,) void **View::OnDraw(CDC *pDC) { //初始化部分 GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartu

MFC中的GDI绘图(1)

一.关于GDI的基本概念 什么是GDI         Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Windows操作系统中,动态链接库C:\WINDOWS\system32\gdi32.dll(GDI Client DLL)中定义了GDI函数,实现与设备无关的包括屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面功能.在Visual C++6.0中的头文件C:\Progra