• C# GDI+与图形编程


    GDI+与图形编程

    GDI+的基本概念

    GDI+的常用对象,包括Graphics、Font、Brush、Pen等对象的创建和使用

    常用图形的绘制

    Color结构、Point结构和Rectangle结构

    1.GDI+的概念

          GDI+是GDI(Graphics Device Interface,图形设备接口)的改进产品。

    2.GDI+的绘图命名空间

          用户所使有的GDI+函数都保存在System.Drawing.d11程序集中。其中包括System.Drawing、System.Drawing.Text、System.Drawing.Printing、System.Drawing.Imaging、System.Drawing.Drawing2D和System.Drawing.Design等命名空间。   

    Graphics对象

    (1)利用窗体或控件的Paint事件的参数PaintEventArgs创建Graphics对象。

    (2)使用窗体或控件的CreateGraphics方法

           Graphics  g=this.CreateGraphics();

    (3)使用Image的派生类创建Graphics对象。使用Image的任何派生类均可以生成相应的Graphics对象,这种方法一般适用于在C#中对图像进行处理的场合。

                  Bitmap b=new Bitmap("Mybmp.bmp");

    Graphics g=Graphics.FromImage(b);

    Pen对象

    Pen类的构造函数有四种,使用方法如下。

    (1)创建某一颜色的Pen对象:public Pen(Color)

    (2)创建某一刷子样式的Pen对象:public Pen(Brush)

    (3)创建某—刷子样式并具有相应宽度的Pen对象:public Pen(Brush,float)

    (4)创建某一颜色和相应宽度的Pen对象:public Pen(Color,float)

    Pen对象的常用属性

    (1)Alignment属性:用来获取或设置此Pen对象的对齐方式。

    (2)Color属性:用来获取或设置此Pen对象的颜色。

    (3)Width属性:用来获取或设置此Pen对象的宽度。

    (4)DashStyle属性:用来获取或设置通过此Pen对象绘制的虚线的样式。

    (5)DashCap属性:用来指定虚线两端风格,是一个DashCap枚举型的值。

    (6)StartCap属性:用来获取或设置通过此Pen对象绘制的直线起点的帽样式。

    (7)EndCap属性:用来获取或设置通过此Pen对象绘制的直线终点的帽样式。

    (8)PenType属性:用来获取用此Pen对象绘制的直线的样式。 

    Font对象


     

    Brush对象

    1.SolidBrush画刷

       SolidBrush类用来定义单一颜色的Brush,其构造函数如下。

       public SolidBrush(Color.Color)

    2.HatchBrush画刷

          HatchBrush类的构造函数有两种,分别如下:  

    [格式1]:public HatchBrush(HatchStyle, Color);

    [格式2]:public HatchBrush(HatchStyle, Color, Color); HatchBrush画刷具有三个属性,分别如下:

    (1)BackgroundColor属性:获取此HatchBrush 对象的背景色。

    (2)ForegroundColor属性:获取此HatchBrush 对象的前景色。

    (3)HatchStyle属性:获取此HatchBrush 对象的阴影样式。

    3.LinearGradientBrush画刷

    LinearGradientBrush类的构造函数有多种格式,最常用的格式如下。

        public LinearGradientBrush(Point1, Point2, Color1, Color2);

    常用图形的绘制方法

    1.画直线

          [格式1]:public void DrawLine(Pen pen,int x1,int y1,int x2,int y2);

          [格式2]:public void DrawLine(Pen pen,Point pt1,Point pt2);

    2.画椭圆

    [格式1]:public void DrawEllipse( Pen pen, Rectangle rect);

    [格式2]:public void DrawEllipse(Pen pen,int x,int y,int width, int height);

    3.绘制圆弧

    [格式1]:public void DrawArc(Pen pen,Rectangle rect,float startAngle,float sweepAngle);

    [格式2]:public void DrawArc(Pen pen,int x,int y,int width,int height,int startAngle,int sweepAngle);

    4.画扇形图

    使用Graphics对象的DrawPie方法可以绘制扇形图,所谓扇形图其实就是把一段圆弧的两个端点与圆心相连。DrawPie方法的格式与DrawArc方法基本一致。

    5.画矩形

    [格式1]: public void DrawRectangle( Pen pen, Rectangle rect);

    [格式2]:public void DrawRectangle(Pen pen,int x,int y,int width,int height);

    6.画Bezier曲线

    [格式1]:public void DrawBezier(Pen pen,Point pt1,Point pt2,Point pt3,Point pt4);

    [格式2]:public void DrawBezier(Pen pen,float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4);

    7.画多边形

    [格式1]:public void DrawPolygon(Pen pen,  Point[] points);

    [格式2]:public void DrawPolygon(Pen pen, PointF[] points);

    8.绘制闭合曲线

    [格式1]:public void DrawClosedCurve(Pen pen,Point[] points);

    [格式2]:public void DrawClosedCurve(Pen pen,Point[] points,float tension,FillMode fillmode);

    9.绘制非闭合曲线

    [格式]:public void DrawCurve( Pen pen,Point[] points);

    10.绘制路径

    [格式]:public void DrawPath(Pen pen,GraphicsPath path);

    11.填充椭圆

    [格式1]:public void FillEllipse(Brush brush, Rectangle rect);

    [格式2]:public void DrawEllipse(Brush brush,int x,int y,int width, int height);

    12.填充矩形

    [格式1]: public void FillRectangle( Brush brush, Rectangle rect);

    [格式2]:public void FillRectangle(Brush brush,int x,int y,int width,int height);

    13.填充饼图

    [格式1]:public void FillPie(Brush brush,Rectangle rect,float startAngle,float sweepAngle)

    [格式2]:public void FillPie(Brush brush,int x,int y,int width,int height,int startAngle,int sweepAngle);

  • 相关阅读:
    linux的别名(alias/unalias)
    asp.net <%%> <%#%><%=%><%@%><%$%>用法与区别
    SQL获取刚插入的记录的自动增长列ID的值
    包和继承
    面对对象编程(封装)
    面对对象编程(上)
    数组(下)
    java数组-如何在一堆数据中使用数组!
    Request和Response学习笔记5
    Request和Response学习笔记4
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2656967.html
Copyright © 2020-2023  润新知