zoukankan      html  css  js  c++  java
  • c#使用GDI进行简单的绘图

    https://www.2cto.com/database/201805/749421.html

    https://zhidao.baidu.com/question/107832895.html

    private void button2_Click(object sender, EventArgs e)
          {
              Bitmap image = new Bitmap(200, 200);
              Graphics g = Graphics.FromImage(image);
              //使绘图质量最高,即消除锯齿
              g.SmoothingMode = SmoothingMode.AntiAlias;
              g.InterpolationMode = InterpolationMode.HighQualityBicubic;
              g.CompositingQuality = CompositingQuality.HighQuality;
              Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔 
              g.DrawString("字符串", new Font("宋体", 10),  new SolidBrush(Color.Blue), new PointF(10, 10));
              g.DrawLine(p, 0, 0, 200, 200);//在画板上画直线
              g.DrawRectangle(p, 0, 0, 200, 200);//在画板上画矩形
              g.DrawEllipse(p, 0, 0, 200, 200);//在画板上画椭圆
              //保存图片
              image.Save(@"c:1.jpg");
              #region 显示
              //pictureBox1.Image = (Image)image;
              //pictureBox1.Image.Save(@"c:1.jpg");
              #endregion
          }
     
     
     
    你在绘制字符串之前可以使用Graphics的MeasureString方法测量它的尺寸再绘制一个相应大小的矩形就行了,如下
    Graphics g = this.CreateGraphics();
    Font font = new Font("宋体", 9f);
    PointF pointF = new PointF(10, 10);
    SizeF sizeF = g.MeasureString("Hello World!", font);
    g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
    g.DrawString("Hello World!", font, Brushes.Black, pointF);
    g.Dispose();
    font.Dispose();
     
     
    //自己整合后写的,画正方形,中间写字
     
     Bitmap image = new Bitmap(100, 50);
                                        Graphics g = Graphics.FromImage(image);
                                        Font font = new Font("黑体", 26.25f, FontStyle.Bold);
                                        SizeF sizeF = g.MeasureString(lxtmStr, font);
                                        //sizeF.Width, sizeF.Height
                                        image = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
                                        g = Graphics.FromImage(image);
                                        //使绘图质量最高,即消除锯齿
                                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                                        PointF pointF = new PointF(0, 0);

                                        if ((dsShoptemp.Tables[0].Rows[0]["STEP"].ObjToInt() % 2) == 1)
                                        {
                                            g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
                                            g.DrawString(lxtmStr, font, Brushes.Black, pointF);
                                        }
                                        else
                                        {
                                            g.FillRectangle(Brushes.Black, new RectangleF(pointF, sizeF));
                                            g.DrawString(lxtmStr, font, Brushes.White, pointF);
                                        }
                                        item["路线条码"] = image;
  • 相关阅读:
    (转载)你好,C++(25)函数调用和它背后的故事5.1.2 函数调用机制
    (转载)你好,C++(24)好大一个箱子!5.1.1 函数的声明和定义
    (转载)你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作
    (转载)你好,C++(22) 排排坐,吃果果——4.3.3 for循环:某个范围内…每个都…
    【NOI2002T4】荒岛野人-扩展欧几里得
    【POJ1743】Musical Theme-后缀数组+二分答案
    【POJ1743】Musical Theme-后缀数组+二分答案
    【NOI2014T1】起床困难综合症-贪心+位运算
    【NOI2014T1】起床困难综合症-贪心+位运算
    【APIO2012T1】派遣-贪心+左偏树
  • 原文地址:https://www.cnblogs.com/LuoEast/p/11991052.html
Copyright © 2011-2022 走看看