首页WIN11问题WinForm(十二)画图

WinForm(十二)画图

时间2022-12-16 05:30:06发布分享专员分类WIN11问题浏览85

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

下面是在Form上画了一个小票,然后调用打印组件在虚拟打印机里打印出来的效果tinypdf虚拟打印机 。

usingQRCoder;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Drawing.Imaging;

namespaceWinFormsDemo12{publicpartialclassForm1: Form{publicForm1( ) {InitializeComponent;}voidDraw( Graphics graphics) {vary = 15; usingvarlogo = newBitmap(Directory.GetCurrentDirectory + "/aeon.png"); graphics.DrawImage(MakeGrayscale(logo), 60, y, 200, 80); graphics.DrawLine( newPen(Color.Black, 2), 10, y += 80, 310, y); graphics.DrawLine( newPen(Color.Black, 2), 10, y += 4, 310, y); varfont = newFont( "黑体", 10); varbrush = newSolidBrush(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); varno = "000000000001"; graphics.DrawString( $"流水号: {no}" , font, brush, 10, y += 20); graphics.DrawLine( newPen(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( newPen(Color.Black, 2), 10, y += 20, 310, y); graphics.DrawLine( newPen(Color.Black, 2), 10, y += 4, 310, y); varsumfont = newFont( "黑体", 12); graphics.DrawString( " 小计:82", sumfont, brush, 10, y += 5);

varqrCodeAsBitmapByteArr = PngByteQRCodeHelper.GetQRCode(no, QRCodeGenerator.ECCLevel.Q, 20, false); usingvarqrcode = Image.FromStream( newMemoryStream(qrCodeAsBitmapByteArr)); graphics.DrawImage(qrcode, 100, y += 50, 120, 120); }

publicstaticBitmap MakeGrayscale( Bitmap original) {varnewBitmap = newBitmap(original.Width, original.Height); varg = Graphics.FromImage(newBitmap); varcolorMatrix = newSystem.Drawing.Imaging.ColorMatrix( newfloat[][] {newfloat[] { .3f, .3f, .3f, 0, 0}, newfloat[] { .59f, .59f, .59f, 0, 0}, newfloat[] { .11f, .11f, .11f, 0, 0}, newfloat[] { 0, 0, 0, 1, 0}, newfloat[] { 0, 0, 0, 0, 1} });varattributes = newSystem.Drawing.Imaging.ImageAttributes; attributes.SetColorMatrix(colorMatrix);g.DrawImage(original, newRectangle( 0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); g.Dispose;returnnewBitmap; } privatevoidprintDocument1_PrintPage( objectsender, System.Drawing.Printing.PrintPageEventArgs e ) {if(e.Graphics != null) {Draw(e.Graphics);}}privatevoidbutton2_Click( objectsender, EventArgs e ) {printDocument1.Print;}privatevoidbutton1_Click( objectsender, EventArgs e ) {vargraphics = this.CreateGraphics; Draw(graphics);}}}

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

Graphics实现了IDisposable,用后请释放tinypdf虚拟打印机 。

窗体的浏览效果:

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

WinForm(十二)画图

爱资源吧版权声明:以上文中内容来自网络,如有侵权请联系删除,谢谢。

WinForm十二WinForm十二tinypdf虚拟打印机
万能打印工具:Air Printer Mac FCC文件揭示苹果神秘的“网络适配器”,运行iOS系统