tft每日頭條

 > 生活

 > winform零基礎教程

winform零基礎教程

生活 更新时间:2024-11-29 21:40:07

在.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,用後請釋放。

窗體的浏覽效果:

winform零基礎教程(WinForm十二畫圖)1

虛拟打印機輸出的PDF效果圖:

winform零基礎教程(WinForm十二畫圖)2

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved