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,用后请释放。

窗体的浏览效果:

853728281a94dbcade4d5ddfd33ba9d1.png

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

222e1b4c0975ce2f7b22dd1512d2c410.png

技术群:添加小编微信并备注进群

小编微信:mm1552923   

公众号:dotNet编程大全    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值