首页 > 其他分享 >WinForm(十二)画图

WinForm(十二)画图

时间:2023-02-07 21:23:32浏览次数:45  
标签:10 十二 画图 brush graphics var new font WinForm

  在.NET中,画图主要是通过Graphics类实现的,这个类主要通过两类方法完成画图,一类是DrawXXX,画各种线条图形;另一类是FillXXX,用各种形状,填充各种图形。Graphics是画板,Draw各个方法是各种盏笔(不过在调用Draw方法时,参数需要一个Pen对象),Fill的各个方法就是种种刷子(确实Fill的方法参数也需要一个Brush对象)。首先要熟悉各个Draw和Fill方法,以及他们的参数,那么剩下的事就是对坐票了,画什么,在那里画,怎么用Draw和Fill了。
  下面是在Form上画了一个小票,然后调用打印组件在虚拟打印机里打印出来的效果。

using QRCoder;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace WinFormsDemo12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void Draw(Graphics graphics)
        {
            var y = 15;
            using var logo = new Bitmap(Directory.GetCurrentDirectory() + "/aeon.png");
            graphics.DrawImage(MakeGrayscale(logo), 60, y, 200, 80);
            graphics.DrawLine(new Pen(Color.Black, 2), 10, y += 80, 310, y);
            graphics.DrawLine(new Pen(Color.Black, 2), 10, y += 4, 310, y);
            var font = new Font("黑体", 10);
            var brush = new SolidBrush(Color.Black);
            graphics.DrawString("分店:012", font, brush, 10, y += 1);
            graphics.DrawString("店员:张三", font, brush, 160, y);
            graphics.DrawString($"时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", font, brush, 10, y += 20);
            var no = "000000000001";
            graphics.DrawString($"流水号:{no}", font, brush, 10, y += 20);
            graphics.DrawLine(new Pen(Color.Black, 2), 10, y += 25, 310, y);
            graphics.DrawString("名称       数量  单价  金额", font, brush, 10, y += 5);
            graphics.DrawString("西红柿     500g  26.00 15.00", font, brush, 10, y += 20);
            graphics.DrawString("西葫芦    1000g  23.00 23.00", font, brush, 10, y += 20);
            graphics.DrawString("茄子       500g  50.00 25.00", font, brush, 10, y += 20);
            graphics.DrawString("豆角       500g  38.00 19.00", font, brush, 10, y += 20);
            graphics.DrawLine(new Pen(Color.Black, 2), 10, y += 20, 310, y);
            graphics.DrawLine(new Pen(Color.Black, 2), 10, y += 4, 310, y);
            var sumfont = new Font("黑体", 12);
            graphics.DrawString("              小计:82", sumfont, brush, 10, y += 5);

            var qrCodeAsBitmapByteArr = PngByteQRCodeHelper.GetQRCode(no, QRCodeGenerator.ECCLevel.Q, 20, false);
            using var qrcode = Image.FromStream(new MemoryStream(qrCodeAsBitmapByteArr));
            graphics.DrawImage(qrcode, 100, y += 50, 120, 120);
        }

        public static Bitmap MakeGrayscale(Bitmap original)
        {
            var newBitmap = new Bitmap(original.Width, original.Height);
            var g = Graphics.FromImage(newBitmap);
            var colorMatrix = new System.Drawing.Imaging.ColorMatrix(
               new float[][]
              {
                  new float[] {.3f, .3f, .3f, 0, 0},
                  new float[] {.59f, .59f, .59f, 0, 0},
                  new float[] {.11f, .11f, .11f, 0, 0},
                  new float[] {0, 0, 0, 1, 0},
                  new float[] {0, 0, 0, 0, 1}
              });
            var attributes = new System.Drawing.Imaging.ImageAttributes();
            attributes.SetColorMatrix(colorMatrix);
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
            g.Dispose();
            return newBitmap;
        }   
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (e.Graphics != null)
            {
                Draw(e.Graphics);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            var graphics = this.CreateGraphics();
            Draw(graphics);
        }
    }
}

  MakeGrayscale方法是把彩色logo转黑白的一个算法,关注画图部分请忽略。

  Graphics实现了IDisposable,用后请释放。

窗体的浏览效果:

 

 虚拟打印机输出的PDF效果图:

 

 

  想要更快更方便的了解相关知识,可以关注微信公众号

 

 

标签:10,十二,画图,brush,graphics,var,new,font,WinForm
From: https://www.cnblogs.com/ljknlb/p/17099836.html

相关文章

  • WinForm(十二)画图
    在.NET中,画图主要是通过Graphics类实现的,这个类主要通过两类方法完成画图,一类是DrawXXX,画各种线条图形;另一类是FillXXX,用各种形状,填充各种图形。Graphics是画板,Draw各......
  • 实战:第十二章:txt文件转xml文件
    开发不就这么点事吗,有个啥好bb的controller@RequestMapping("/DataDistributionController")@RestControllerpublicclassDataDistributionController{......
  • 《区块链基础知识25讲》-第十二讲-确保账户安全
    区块链使用非对称加密技术是为了确认所有者跟资产的对应关系,确保只有合法所有者才能获取他的资产用户使用公钥来确认给某账户转移资产,只有拥有私钥的人才能获取这些资产加密......
  • OpenCV-Python快速入门(十二):轮廓拟合
    OpenCV-Python快速入门(十二):轮廓拟合​​前言​​​​前提条件​​​​实验环境​​​​轮廓拟合​​​​矩形包围框(cv2.boundingRect())​​​​最小包围矩形框(cv2.minAreaRe......
  • 设计文档中画图的类型
       ......
  • 机器学习-白板推导-系列(十二)笔记:变分推断
    文章目录​​0笔记说明​​​​1背景介绍​​​​1.1频率派​​​​1.2贝叶斯派​​​​2公式推导​​​​3符号修正​​​​4SGVI​​0笔记说明。注意:本笔记主要是......
  • vue.js客服系统实时聊天项目开发(二十二)vue项目中router.js路由介绍
    vue项目的路由就相当于我们在网址url上输入的地址,访问的具体网址就是路由拿到项目先看看路由文件,就能知道具体的访问地址了例如下面的router.jsimportVuefrom'vue'......
  • winform窗体应用管理员登录验证
    首先创建一个管理员类,声明字段并封装生成构造方法如图:    第二添加DBhelper类的封装 如下:namespaceWvidin{//数据库相关操作验证登录p......
  • DVWA靶场实战(十二)——XSS(Stored)
    DVWA靶场实战(十二)五、XSS(Stored):1.漏洞原理:XSS的Stored被称作存储型XSS漏洞,漏洞的原理为语句被保存到服务器上,显示到HTML页面中,经常出现在用户评论的页面,攻击者将XS......
  • springcloud:gateway聚合swagger 下篇(十二)
    0.引言上一章我们之前讲解了在​​单个服务中部署swagger​​,但每次都需要在不同的端口中访问不同服务的swagger-ui。那么本期我们就来讲解一下,如何从一个统一的入口访问不......