zoukankan      html  css  js  c++  java
  • GDI+的基本使用

    Graphics g = e.Graphics; // 创建画板
    Pen p = new Pen(Color.Blue, 1); // 画笔颜色及宽度
    
    g.DrawLine(p, 10, 10, 110, 110); // 绘制直线
    g.DrawLine(p, 210, 10, 110, 110);
    g.DrawRectangle(p, 10, 10, 100, 100); // 绘制矩形
    g.DrawRectangle(p, 110, 10, 100, 100);
    g.DrawEllipse(p, 10, 10, 200, 100); // 根据外接矩形绘制椭圆
    
    g.Dispose();
    p.Dispose();

    Result:

    Pen的属性主要有: Color(颜色),DashCap(短划线终点形状),DashStyle(虚线样式),EndCap(线尾形状), StartCap(线头形状),Width(粗细)等。

    Graphics g = e.Graphics; // 创建画板
    Pen p = new Pen(Color.Blue, 5); // 画笔颜色及宽度
    
    p.DashStyle = DashStyle.Dot; // 定义虚线的样式为点
    g.DrawLine(p, 10, 10, 200, 10);
    
    p.DashPattern = new float[] { 2, 1 }; // 设置短划线和空白部分的数组
    g.DrawLine(p, 10, 20, 200, 20);
    
    p.DashStyle = DashStyle.Solid; // 恢复实线
    p.EndCap = LineCap.ArrowAnchor; // 实现箭头,只对非封闭线有效
    g.DrawLine(p, 10, 30, 200, 30);
    
    g.Dispose();
    p.Dispose();

    Result:

    下面简单实现一下Brush的使用,

    Graphics g = this.CreateGraphics();
    Rectangle rect = new Rectangle(10, 10, 50, 50);//定义矩形,参数为起点横纵坐标以及其长和宽
    
    SolidBrush b1 = new SolidBrush(Color.Blue);//定义单色画刷          
    g.FillRectangle(b1, rect); //填充矩形
    
    g.DrawString("字符串", new Font("宋体", 10), b1, new PointF(90, 10)); // 字符串
    
    TextureBrush b2 = new TextureBrush(Image.FromFile(@"D:.jpg")); // 使用图片填充
    rect.Location = new Point(10, 70); // 更改矩形的起点坐标
    rect.Width = 200; // 更改矩形的宽
    rect.Height = 200; // 更改矩形的高
    g.FillRectangle(b2, rect);
    
    rect.Location = new Point(230, 10);
    LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.Yellow, Color.Red, LinearGradientMode.Horizontal); // 用渐变色填充
    g.FillRectangle(b3, rect);

    Result:

    下面实现对坐标轴的变换,默认坐标轴原点为左上角,正X轴向右,正Y轴向下。

    using (Graphics g = this.CreateGraphics())
    {
        Pen p = new Pen(Color.OrangeRed, 1);
    
        //转变坐标轴角度
        for (float i = 1; i < 90; i += 0.1f)
        {
            g.RotateTransform(i); // 每旋转0.1度画一条线
            g.DrawLine(p, 0, 0, i, 0);
        }
        g.ResetTransform();
    
        g.TranslateTransform(100, 100); // 平移坐标轴
        g.DrawLine(p, 0, 0, 100, 0);
        g.ResetTransform();
    
        g.TranslateTransform(100, 200); // 先平移到指定坐标,然后进行度旋转
        for (int i = 0; i < 8; i++)
        {
            g.RotateTransform(45);
            g.DrawLine(p, 0, 0, 100, 0);
        }
    }

    Result:

    GDI+ 图形设备接口普拉斯(Graphic Device Interface Plus)属于非托管资源,应手动释放。

  • 相关阅读:
    dotnet 通过 WMI 获取系统安装的驱动
    dotnet 通过 WMI 获取设备厂商
    dotnet 通过 WMI 获取设备厂商
    dotnet 通过 HttpClient 下载文件同时报告进度的方法
    dotnet 通过 HttpClient 下载文件同时报告进度的方法
    PHP highlight_string() 函数
    PHP highlight_file() 函数
    PHP get_browser() 函数
    PHP exit() 函数
    PHP eval() 函数
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/9962214.html
Copyright © 2011-2022 走看看