C#(029):GDI+图像编程(C# (029): GDI + image programming)

一、Graphics

GDI+是GDI(Windows Graphics Device Interface)的后继者,它是.NET
Framework为操作图形提供的应用程序编程接口,主要用在窗体上绘制各种图形图像,可以用于绘制各种数据图像、数学仿真等。

Graphics类是GDI+的核心,它提供将对象绘制到显式设备的方法。Graphics类封装了绘制直线、曲线、圆形、图像和文本的方法,是一切GDI+操作的基础类。在绘图之前,必须在指定的窗体上创建一个Graphics对象,才能调用Graphics类的方法画图。

1、创建Graphics类对象

1、Paint事件
在窗体或控件的Paint事件中创建,将其作为PaintEventArgs的一部分。在为控件创建绘制代码时,通常会使用此方法。

例如,在Paint事件中创建Graphics对象:

    public partial class Form1 : Form
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
    
            Graphics g = e.Graphics; // e.ClipRectangle;//剪切区域,即被遮挡的部分
            g.DrawLine(Pens.Wheat, 0, 0, 2, 2);
        }
    }

2、 CreateGraphics方法
调用窗体或控件的CreateGraphics方法可以获取对Graphics对象的引用,该对象表示控件或窗体的绘图画面。如果在已存在的窗体或控件身上绘图,应该使用此方法

例如,在窗体的Load事件中,通过CreateGraphics方法创建Graphics对象

    private void Form1_Load(object sender, EventArgs e)    //窗体的Load事件
    {
        Graphics g;    //声明一个Graphics对象
        g = this.CreateGraphics();
    }

3 、Graphics.FromImage方法
由从Image继承的任何对象创建Graphics对象,调用Graphics.FromImage方法即可,该方法在需要更改已存在的图像时十分有用,例:

    private void Form1_Load(object sender, EventArgs e)
    {
        Bitmap mbit = new Bitmap(@"C:\test.bmp");
        Graphics g = Graphics.FromImage(mbit);
    }

2、Graphics类的属性

  • Clip 获取或设置 Region,它限定此 Graphics 的绘图区域。
  • ClipBounds 获取一个 RectangleF 结构,该结构限定此 Graphics 的剪辑区域。
  • VisibleClipBounds 获取此 Graphics 的可见剪辑区域的边框。
  • CompositingQuality 获取或设置绘制到此 Graphics 的合成图像的呈现质量。
  • SmoothingMode 获取或设置此 Graphics 的呈现质量。
  • DpiX 获取此 Graphics 的水平分辨率。
  • DpiY 获取此 Graphics 的垂直分辨率。
  • PageScale 获取或设置此 Graphics 的世界单位和页单位之间的比例。
  • PageUnit 获取或设置用于此 Graphics 中的页坐标的度量单位。

3、Graphics类的常用方法

  • 画线条
    DrawLine 绘制一条连接由坐标对象指定的两个点的线条
    DrawLines 绘制一列连接一组Point结构的线段
    DrawBezier 绘制由四个Point结构定义的贝塞尔样条
    DrawBeziers 从Point结构的数组绘制一系列贝塞尔样条
    DrawCurve 绘制经过一组指定的Point结构的基数样条
  • 画可填充线条
    DrawEllipse 绘制一个由一对坐标、宽度和高度指定的椭圆
    DrawPath 绘制GraphicsPath对象
    DrawPie 绘制一个扇形,该扇形由一个坐标对象,宽度和高度,以及两条射线所指定的椭圆指定
    DrawPolygon 绘制由一组Point结构定义的多边形
    DrawRectangle 绘制由坐标对。宽度和高度指定的矩形
    DrawRectangles 绘制一系列由Rectangle结构指定的矩形
    DrawArc 绘制一段弧线,它表示由一对坐标、宽高指定的椭圆部分
  • 填充区域
    FillEllipse 填充边框所定义的椭圆内部,该边框由一对坐标、一个高度和一个宽度指定
    FillPath 填充GraphicsPath对象的内部
    FillPie 填充扇形内部
    FillPolygon 填充多边形内部
    FillRectangle 填充由一对坐标、一个宽度和一个高度指定的矩形内部
    FillRectangles 填充由Rectangle结构指定的一些列矩阵的内部
    FillRegion 填充Region对象的内部
  • 画字符串、图画、图标
    DrawString 在指定位置并且用指定的Brush和Font对象绘制指定的文本字符串
    DrawIcon 在指定坐标处绘制由指定的Icon对象表示的图像
    DrawImage 在指定的位置并且按原始大小绘制指定的Image对象
  • 其他
    MeasureString(String, Font) 测量用指定的 Font 绘制的指定字符串。
    FromImage 从指定的Image对象创建行的Graphics对象
    Save 保存此Graphics对象的当前状态,并且GraphicsState对象标识保存的状态
    Clear 清除整个绘图面并以指定背景色填充
    Dispose 释放由此Graphics对象使用的所有资源

二、坐标系统

GDI+定义了三种坐标系统,并提供了三种坐标转换的方法Graphics.TransformPoints()。

  • 全局坐标系统。
  • 页面(Page)坐标系统:左上角为原点,横向x轴向右为正方向,纵向y轴向下为正方向。单位为像素。这是默认的坐标系统。
  • 设备坐标系统:可以指定特定测量单位的页面(Page)坐标系统。如果单位为像素,和页面(Page)坐标系统相同。

1、点结构:Point和PointF

点结构有两个成员:X,Y,表示点的x轴和y轴的坐标。其常用构造函数如下:

    Point p1=new Point(int X,int Y);//X,Y为整数
    PointF p2=new PointF(float X,floa Y);//X,Y为浮点数

2、尺寸结构Size和SizeF

Size和SizeF用来表示尺寸大小,有两个成员:Width和Height。常用构造函数如下:

    public Size(int width,int height);
    public SizeF(float width,float height);

3、矩形结构Rectangle和RectangleF

结构Rectangle和RectangleF用来表示一个矩形,常用属性如下:

  • Top、Left,Bottom, Right:Rectangle结构坐标
  • Width、Height:获取或设置此Rectangle结构的宽、高度。
  • Size:获取或设置此Rectangle的大小。
  • X、Y:获取或设置此Rectangle结构左上角的x,Y坐标。

其常用构造函数为:

    //参数为矩形左上角坐标的点结构location和代表矩形宽和高的Size结构size
    Rectangle(Point location,Size size);//参数也可为PointF和SizeF
    //参数为矩形左上角x和y坐标,宽,高
    Rectangle(int X,int Y,int width,int height);//X和Y也可为float

4、路径类GraphicsPath

GraphicsPath 类表示一系列相互连接的直线和曲线。 此类不能被继承。

    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    
    GraphicsPath gp = new GraphicsPath();
    
    gp.AddLine(10, 10, 10, 50);
    gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
    gp.AddLine(30, 70, 60, 70);
    //闭合图形
    gp.CloseFigure();
    
    //GraphicsPath类本身没有提供显示创建路径的功能,所以需要把路径对象传给Graphics类的一个方法来显示
    g.DrawPath(Pens.Black, gp);
    g.FillPath(Brushes.Orange, gp);//给路径填充橘黄色,
    gp.Dispose();
    g.Dispose();

5、区域类Region类

Region类表示由矩形或由路径构成图形形状的内部

1、从GraphicsPath路径对象中创建区域,

    GraphicsPath gp = new GraphicsPath();
    
    gp.AddLine(10, 10, 10, 50);
    gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
    gp.AddLine(30, 70, 60, 70);
    //闭合图形
    gp.CloseFigure();
    
    Region reg = new Region(gp);   //从GraphicsPath对象创建Region对象
    //填充区域
    g.FillRegion(Brushes.Green, reg);

2.从各类图形中创建:

    Region reg = new Region(new Rectangle(10, 10, 80, 20));

3.从一个区域中创建另一个区域

    Region r1 = new Region(new Rectangle(10, 10, 80, 20));
    RegionData r1Data = r1.GetRegionData();//区域数据
    Region r2 = new Region(r1Data);  //构造函数不接受区域对象本身,可以接受区域数据数组。

4.区域的并集和交集

示例:

    Graphics g = e.Graphics;
    Rectangle rect1 = new Rectangle(50, 10, 50, 130);
    Rectangle rect2 = new Rectangle(10, 50, 130, 50);
    //设置Region为第一个矩形
    Region reg = new Region(rect1);
    //用Rect2提取Region中的交集
    reg.Intersect(rect2);
    
    g.FillRegion(Brushes.Orange, reg);
    g.DrawRectangle(Pens.Black, rect1);
    g.DrawRectangle(Pens.Black, rect2);

三、颜色

1、Color结构

Color结构表示颜色,结构中包含一个无符号32位数代表颜色。任何一种颜色可以用ARGB颜色(alpha、红、绿、蓝)表示。Color结构通过表态属性公开了140个命名颜色。。常用方法如下:

  • public static Color FromArgb(int alpha,int rr,int gg,int bb);
    从四个分量(透明度、红色、绿色和蓝色)值创建Color结构。每个分量的值仅限于8位(小于256)。alpha值表示透明度,=0为完全透明,=255为完全不透明
  • public static Color FromArgb(int rr,int gg,int bb);
    从指定的8位颜色值(红色、绿色和蓝色)创建Color结构。透明度值默认为255(完全不透明)。每个分量的值仅限于8位(小于256)。红色为(255,0,0),绿色为(0,255,0),蓝色为(0,0,255)。
  • public static Color FromArgb(int alpha,Color color);
    从指定的Color结构创建新Color结构,使用新指定的透明度值alpha。alpha值仅限于8位。

创建实例

    //创建Color实例的参数为 r,g,b; //参数为红,绿,蓝三种原色,透明度为默认,即完全不透明.
    Color cl = Color.FromArgb(200, 200, 200);
    
    //调用静态FromName方法
    Color cl = Color.FromName("PowderBlue");  //字符串参数必须包含一个KnownColor枚举值的名称.

使用颜色:

    Color red=Color.Red;
    red.GetBrightness();
    red.GetHue();
    red.GetSaturation();

2、ColorTranslator类

  • FromHtml(String) 将 HTML 颜色表示形式翻译成 GDI+ Color 结构。
    Color myColor = ColorTranslator.FromHtml(“Blue”);

    Color myColor = ColorTranslator.FromHtml(“H00000”);

  • ToHtml(Color) 将指定的 Color 结构翻译成 HTML 字符串颜色表示形式。
    string htmlColor = ColorTranslator.ToHtml( Color.Red);

    string htmlColor = ColorTranslator.ToHtml( Color.FromArgb(255,255,255));

四、画笔Pen类

画笔,定义用于绘制直线和曲线的对象。

定义画笔:

    Pen pen1 = new Pen(Color, Single); //参数为颜色Color,单精度浮点数Sigle;宽度默认为1;
    Pen pen2 = Pen(Brush, Single);  // 使用指定的 Pen 和 Brush 初始化 Width 类的新实例。

使用画笔:

    //画实线
    Pen pen = new Pen(Color.Red, 3);
    //确定线的首位位置
    Point point1 = new Point(10, 50);
    Point point2 = new Point(250, 50);
    Graphics g = this.CreateGraphics();
    g.DrawLine(pen, point1, point2);

五、画刷Brush

画刷:定义用于填充图形形状(如矩形、椭圆、饼形、多边形和封闭路径)的内部的对象。只是一个抽象基类。

    Rectangle rect1 = new Rectangle(20, 80, 250, 100);
    
    // (实心实心刷)
    SolidBrush sbrush = new SolidBrush(Color.DarkOrchid);
    
    //(纹理刷)
    TextureBrush textureBrush = new TextureBrush(new Bitmap(@"e:/123.jpg"));
    
    //(梯度、线性渐进刷)
    LinearGradientBrush lbrush = new LinearGradientBrush(rect1, Color.DarkOrange, Color.Aquamarine, 45);
    
    //路径渐进画刷
    PathGradientBrush pathbrush = new PathGradientBrush(graphicsPath);
    
    //(阴影刷)
    HatchBrush hbrush = new HatchBrush(HatchStyle.DiagonalCross, Color.DarkOrange, Color.Aquamarine);
    
    e.Graphics.FillRectangle(sbrush, rect1);         // (实心刷)
    e.Graphics.FillRectangle(textureBrush, rect1);       //(纹理刷)
    e.Graphics.FillRectangle(lbrush, rect1);            //(梯度刷)
    e.Graphics.FillRectangle(hbrush, rect1);             //(阴影刷)

六、字体 Font

Font类有两个构造函数:

第一个是new Font(字体名称,字号),例如,label1.Font=new Font(“黑体”,9)。

第二个是new Font(字体名称,字号,字体风格),其中第三个参数是枚举类型。

定义字体:

    //例如修改标签控件字体为斜体:
    label1.Font=new Font("黑体",9,label1.Font.Style|FontStyle.Italic);
    
    //修改标签控件字体不为斜体:
    label1.Font=new Font("黑体",9,label1.Font.Style&~FontStyle.Italic);

字体用法

    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    
    // Draw string to screen.
    e.Graphics.DrawString("Sample Text", drawFont, drawBrush,  new PointF(150.0F, 150.0F));

七、图像

图像Image类、打印Printing相关

————————

一、Graphics

GDI + is the successor of GDI (Windows graphics device interface) NET
The application programming interface provided by the framework for operating graphics is mainly used to draw various graphics and images on the form, and can be used to draw various data images, mathematical simulation, etc.

Graphics class is the core of GDI +, which provides methods to draw objects to explicit devices. Graphics class encapsulates the methods of drawing lines, curves, circles, images and text. It is the basic class of all GDI + operations. Before drawing, you must create a graphics object on the specified form before calling the method of graphics class to draw.

1、创建Graphics类对象

1. Paint event
Create an event as part of the paintpaintevents control on the form. This method is usually used when creating drawing code for a control.

For example, to create a graphics object in a paint event:

    public partial class Form1 : Form
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
    
            Graphics g = e.Graphics; // e.ClipRectangle;//剪切区域,即被遮挡的部分
            g.DrawLine(Pens.Wheat, 0, 0, 2, 2);
        }
    }

2. CreateGraphics method
Call the CreateGraphics method of the form or control to obtain a reference to the graphics object, which represents the drawing screen of the control or form. You should use this method if you are drawing on an existing form or control

For example, in the load event of the form, create a graphics object through the CreateGraphics method

    private void Form1_Load(object sender, EventArgs e)    //窗体的Load事件
    {
        Graphics g;    //声明一个Graphics对象
        g = this.CreateGraphics();
    }

3 、Graphics. Fromimage method
Create a graphics object from any object inherited from image and call graphics Fromimage method, which is very useful when you need to change an existing image, for example:

    private void Form1_Load(object sender, EventArgs e)
    {
        Bitmap mbit = new Bitmap(@"C:\test.bmp");
        Graphics g = Graphics.FromImage(mbit);
    }

2、Graphics类的属性

  • Clip 获取或设置 Region,它限定此 Graphics 的绘图区域。
  • ClipBounds 获取一个 RectangleF 结构,该结构限定此 Graphics 的剪辑区域。
  • VisibleClipBounds 获取此 Graphics 的可见剪辑区域的边框。
  • CompositingQuality 获取或设置绘制到此 Graphics 的合成图像的呈现质量。
  • SmoothingMode 获取或设置此 Graphics 的呈现质量。
  • DpiX 获取此 Graphics 的水平分辨率。
  • DpiY 获取此 Graphics 的垂直分辨率。
  • Pagescale gets or sets the ratio between the world units and page units of this graphics.
  • Pageunit gets or sets the unit of measure used for page coordinates in this graphics.

3、Graphics类的常用方法

  • Draw lines
    DrawLine draws a line connecting two points specified by the coordinate object
    Drawlines draws a list of line segments connecting a set of point structures
    Drawbezier draws Bezier splines defined by four point structures
    DrawBeziers draws a series of Bezier splines from an array of point structures
    Drawcurve draws cardinal splines that pass through a set of specified point structures
  • Draw fillable lines
    Drawellipse draws an ellipse specified by a pair of coordinates, width, and height
    Drawpath draws a GraphicsPath object
    Drawpie draws a sector specified by a coordinate object, width and height, and an ellipse specified by two rays
    Drawpolygon draws polygons defined by a set of point structures
    Drawrectangle is drawn by coordinate pairs. Rectangle with width and height specified
    Drawrectangles draws a series of rectangles specified by the rectangle structure
    Drawarc draws an arc that represents the part of the ellipse specified by a pair of coordinates, width and height
  • Filled area
    Fillellipse fills the interior of the ellipse defined by the border, which is specified by a pair of coordinates, a height and a width
    Fillpath fills the interior of the GraphicsPath object
    Fillpie fills the inside of the fan
    FillPolygon fills the interior of the polygon
    FillRectangle fills the interior of a rectangle specified by a pair of coordinates, a width, and a height
    Fillrectangles fills the interior of some column matrices specified by the rectangle structure
    Fillregion fills the interior of the region object
  • Draw string, picture, Icon
    Drawstring draws the specified text string at the specified position and with the specified brush and font objects
    Drawicon draws the image represented by the specified icon object at the specified coordinates
    DrawImage draws the specified image object at the specified location and at the original size
  • 其他
    MeasureString(String, Font) 测量用指定的 Font 绘制的指定字符串。
    FromImage 从指定的Image对象创建行的Graphics对象
    Save 保存此Graphics对象的当前状态,并且GraphicsState对象标识保存的状态
    Clear 清除整个绘图面并以指定背景色填充
    Dispose 释放由此Graphics对象使用的所有资源

2、 Coordinate system

GDI + defines three coordinate systems and provides three coordinate conversion methods TransformPoints()。

  • Global coordinate system.
  • Page coordinate system: the upper left corner is the origin, the transverse x-axis is the right direction, and the longitudinal Y-axis is the downward direction. In pixels. This is the default coordinate system.
  • Equipment coordinate system: you can specify the page coordinate system of a specific unit of measurement. If the unit is pixels, it is the same as the page coordinate system.

1、点结构:Point和PointF

The point structure has two members: X and y, which represent the x-axis and y-axis coordinates of the point. Its common constructors are as follows:

    Point p1=new Point(int X,int Y);//X,Y为整数
    PointF p2=new PointF(float X,floa Y);//X,Y为浮点数

2、尺寸结构Size和SizeF

Size and sizef are used to represent size. They have two members: width and height. Common constructors are as follows:

    public Size(int width,int height);
    public SizeF(float width,float height);

3、矩形结构Rectangle和RectangleF

The structures rectangle and rectangle f are used to represent a rectangle. The common attributes are as follows:

  • Top、Left,Bottom, Right:Rectangle结构坐标
  • Width、Height:获取或设置此Rectangle结构的宽、高度。
  • Size:获取或设置此Rectangle的大小。
  • 10. Y: gets or sets the X and Y coordinates of the upper left corner of this rectangle structure.

Its common constructors are:

    //参数为矩形左上角坐标的点结构location和代表矩形宽和高的Size结构size
    Rectangle(Point location,Size size);//参数也可为PointF和SizeF
    //参数为矩形左上角x和y坐标,宽,高
    Rectangle(int X,int Y,int width,int height);//X和Y也可为float

4、路径类GraphicsPath

The GraphicsPath class represents a series of interconnected lines and curves. This class cannot be inherited.

    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    
    GraphicsPath gp = new GraphicsPath();
    
    gp.AddLine(10, 10, 10, 50);
    gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
    gp.AddLine(30, 70, 60, 70);
    //闭合图形
    gp.CloseFigure();
    
    //GraphicsPath类本身没有提供显示创建路径的功能,所以需要把路径对象传给Graphics类的一个方法来显示
    g.DrawPath(Pens.Black, gp);
    g.FillPath(Brushes.Orange, gp);//给路径填充橘黄色,
    gp.Dispose();
    g.Dispose();

5、区域类Region类

The region class represents the interior of a graphic shape composed of a rectangle or a path

1. Create an area from the GraphicsPath path object,

    GraphicsPath gp = new GraphicsPath();
    
    gp.AddLine(10, 10, 10, 50);
    gp.AddBezier(10, 50, 10, 55, 25, 70, 30, 70);
    gp.AddLine(30, 70, 60, 70);
    //闭合图形
    gp.CloseFigure();
    
    Region reg = new Region(gp);   //从GraphicsPath对象创建Region对象
    //填充区域
    g.FillRegion(Brushes.Green, reg);

2. Create from various graphics:

    Region reg = new Region(new Rectangle(10, 10, 80, 20));

3. Create another region from one region

    Region r1 = new Region(new Rectangle(10, 10, 80, 20));
    RegionData r1Data = r1.GetRegionData();//区域数据
    Region r2 = new Region(r1Data);  //构造函数不接受区域对象本身,可以接受区域数据数组。

4. Union and intersection of regions

Example:

    Graphics g = e.Graphics;
    Rectangle rect1 = new Rectangle(50, 10, 50, 130);
    Rectangle rect2 = new Rectangle(10, 50, 130, 50);
    //设置Region为第一个矩形
    Region reg = new Region(rect1);
    //用Rect2提取Region中的交集
    reg.Intersect(rect2);
    
    g.FillRegion(Brushes.Orange, reg);
    g.DrawRectangle(Pens.Black, rect1);
    g.DrawRectangle(Pens.Black, rect2);

3、 Colour

1、Color结构

The color structure represents the color, and the structure contains an unsigned 32-digit number to represent the color. Any color can be represented by ARGB color (alpha, red, green, blue). The color structure exposes 140 named colors through the statement attribute.. Common methods are as follows:

  • public static Color FromArgb(int alpha,int rr,int gg,int bb);
    Creates a color structure from the values of the four components (transparency, red, green, and blue). The value of each component is limited to 8 bits (less than 256). Alpha value indicates transparency, = 0 is completely transparent, = 255 is completely opaque
  • public static Color FromArgb(int rr,int gg,int bb);
    Creates a color structure from the specified 8-bit color values (red, green, and blue). The transparency value defaults to 255 (completely opaque). The value of each component is limited to 8 bits (less than 256). Red is (255,0,0), green is (0255,0), and blue is (0,0255).
  • public static Color FromArgb(int alpha,Color color);
    从指定的Color结构创建新Color结构,使用新指定的透明度值alpha。alpha值仅限于8位。

Create instance

    //创建Color实例的参数为 r,g,b; //参数为红,绿,蓝三种原色,透明度为默认,即完全不透明.
    Color cl = Color.FromArgb(200, 200, 200);
    
    //调用静态FromName方法
    Color cl = Color.FromName("PowderBlue");  //字符串参数必须包含一个KnownColor枚举值的名称.

Use color:

    Color red=Color.Red;
    red.GetBrightness();
    red.GetHue();
    red.GetSaturation();

2、ColorTranslator类

  • FromHtml(String) 将 HTML 颜色表示形式翻译成 GDI+ Color 结构。
    Color myColor = ColorTranslator.FromHtml(“Blue”);

    Color myColor = ColorTranslator.FromHtml(“H00000”);

  • ToHtml(Color) 将指定的 Color 结构翻译成 HTML 字符串颜色表示形式。
    string htmlColor = ColorTranslator.ToHtml( Color.Red);

    string htmlColor = ColorTranslator.ToHtml( Color.FromArgb(255,255,255));

4、 Pen class

Brush that defines the objects used to draw lines and curves.

Define brush:

    Pen pen1 = new Pen(Color, Single); //参数为颜色Color,单精度浮点数Sigle;宽度默认为1;
    Pen pen2 = Pen(Brush, Single);  // 使用指定的 Pen 和 Brush 初始化 Width 类的新实例。

Use brush:

    //画实线
    Pen pen = new Pen(Color.Red, 3);
    //确定线的首位位置
    Point point1 = new Point(10, 50);
    Point point2 = new Point(250, 50);
    Graphics g = this.CreateGraphics();
    g.DrawLine(pen, point1, point2);

五、画刷Brush

Brush: defines an object used to fill the interior of graphic shapes such as rectangles, ellipses, pancakes, polygons, and closed paths. Just an abstract base class.

    Rectangle rect1 = new Rectangle(20, 80, 250, 100);
    
    // (实心实心刷)
    SolidBrush sbrush = new SolidBrush(Color.DarkOrchid);
    
    //(纹理刷)
    TextureBrush textureBrush = new TextureBrush(new Bitmap(@"e:/123.jpg"));
    
    //(梯度、线性渐进刷)
    LinearGradientBrush lbrush = new LinearGradientBrush(rect1, Color.DarkOrange, Color.Aquamarine, 45);
    
    //路径渐进画刷
    PathGradientBrush pathbrush = new PathGradientBrush(graphicsPath);
    
    //(阴影刷)
    HatchBrush hbrush = new HatchBrush(HatchStyle.DiagonalCross, Color.DarkOrange, Color.Aquamarine);
    
    e.Graphics.FillRectangle(sbrush, rect1);         // (实心刷)
    e.Graphics.FillRectangle(textureBrush, rect1);       //(纹理刷)
    e.Graphics.FillRectangle(lbrush, rect1);            //(梯度刷)
    e.Graphics.FillRectangle(hbrush, rect1);             //(阴影刷)

6、 Font

The font class has two constructors:

The first is new font (font name, font size), for example, label1 Font = new font (“bold”, 9).

The second is new font (font name, font size and font style), and the third parameter is enumeration type.

Define font:

    //例如修改标签控件字体为斜体:
    label1.Font=new Font("黑体",9,label1.Font.Style|FontStyle.Italic);
    
    //修改标签控件字体不为斜体:
    label1.Font=new Font("黑体",9,label1.Font.Style&~FontStyle.Italic);

Font usage

    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    
    // Draw string to screen.
    e.Graphics.DrawString("Sample Text", drawFont, drawBrush,  new PointF(150.0F, 150.0F));

7、 Image

see

Related to image class and printing