zoukankan      html  css  js  c++  java
  • C#GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

    C#GDI+ 绘制线段(实线或虚线)、矩形、字符串、圆、椭圆

    绘制基本线条和图形 比较简单,直接看代码。

     1             Graphics graphics = e.Graphics;
     2 
     3             //绘制实线
     4             using (Pen pen = new Pen(Color.Black, 2))
     5             {
     6                 pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; //实线
     7                 graphics.DrawLine(pen,0,10,100,10);
     8             }
     9 
    10             //画出虚线
    11             using (Pen pen = new Pen(Color.Black, 2))
    12             {
    13                 pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
    14                 graphics.DrawLine(pen, 0, 15, 100, 15);
    15             }
    16 
    17             //画出矩形
    18             using (Pen pen = new Pen(Color.Black, 2))
    19             {
    20                  Rectangle rectangle = new Rectangle(2,30,100,50);
    21                     graphics.DrawRectangle(pen,rectangle);
    22             }
    23 
    24             //绘制字符串
    25             string str = "Hello 笨男孩!";
    26             graphics.DrawString(str, this.Font, Brushes.Black, 2, 100);
    27 
    28             //测量字符串的高度和宽度
    29             SizeF szfTitle = graphics.MeasureString(str, this.Font);
    30             graphics.DrawString(str, this.Font, Brushes.Black, 2, 200-(int)szfTitle.Height);
    31             graphics.DrawString("该字符串高度:"+szfTitle.Height+" 字符串宽度:"+szfTitle.Width, this.Font, Brushes.Black, 2, 200);
    32 
    33 
    34 
    35             //绘制圆
    36             using(Pen pen = new Pen(Color.Black,2))
    37             {
    38                 //
    39                 graphics.DrawEllipse(pen,2,230,100,100); //在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为100,高为100   此时就是一个圆
    40 
    41                 //椭圆
    42                 graphics.DrawEllipse(pen, 2, 400, 100, 50); //在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为100,高为50   此时就是一个椭圆
    43             }

    绘制效果

    源代码工程文件下载

  • 相关阅读:
    MyEclipse中的几种查找方法
    WebLogic初学笔记
    CountDownLatch源码分析
    linux--句柄相关
    linux命令--wc
    Spring源码解析(九)--再来说说三级缓存
    定位JVM内存泄漏常用命令和方法
    Mybatis整合Spring之MapperFactoryBean怎么拿到的SqlSessionFactory
    Mybatis3.3.0 Po类有LocalDateTime字段报错
    时间范围查询优化技巧
  • 原文地址:https://www.cnblogs.com/JiYF/p/9953828.html
Copyright © 2011-2022 走看看