C# 打印机相关配置

编程入门 行业动态 更新时间:2024-10-11 23:18:28

C# <a href=https://www.elefans.com/category/jswz/34/1768547.html style=打印机相关配置"/>

C# 打印机相关配置

在C#中设置打印机纸张大小,如此简单

using System.Drawing.Printing.PrintDocumentusing System.Drawing.Printing.PrinterSettings.PaperSizeCollectionusing System.Drawing.Printing.PaperSizeusing System.Drawing.Printing.PageSettings/需要以上引用PrintDocument pd = new PrintDocument();
PaperSize p=null;
foreach(PaperSize ps in pd.PrinterSettings.PaperSizes)
{   if(ps.PaperName.Equals("A3"))p = ps;
}
pd.DefaultPageSettings.PaperSize = p;
pd.Print();

打印区域设定:
///写在打印printpage 事件里

PrintDocument pd = new PrintDocument();pd.PrintPage += (sender, args) =>{Image i = Image.FromFile("C://tesimage.PNG");Point p = new Point(100, 100);args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height);};pd.Print();

横打,竖打

PrintDocument.DefaultPageSettings.Landscape = true;//横打
PrintDocument.DefaultPageSettings.Landscape = false;//纵打

打印控制类

 class PrintDirectClass{private int printNum = 0;//多页打印private Image imageFile = null;//单个图片文件private ArrayList fileList = new ArrayList();//多个图片文件public void PrintPreview(Image ImageIn){imageFile = ImageIn; PrintDocument docToPrint = new PrintDocument();docToPrint.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_BeginPrint);docToPrint.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_EndPrint);docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.docToPrint_PrintPage);docToPrint.DefaultPageSettings.Landscape = true;//横向打印//PrintDialog printDialog = new PrintDialog();//printDialog.AllowSomePages = true;//printDialog.ShowHelp = true;//printDialog.Document = docToPrint;//if (printDialog.ShowDialog() == DialogResult.OK)//{docToPrint.Print();//}}private void docToPrint_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e){if (fileList.Count == 0)fileList.Add(imageFile);}private void docToPrint_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e){}private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){//图片抗锯齿e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//Stream fs = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open, FileAccess.Read);System.Drawing.Image image =(Image)fileList[printNum];int x = e.MarginBounds.X;int y = e.MarginBounds.Y;int width = image.Width;int height = image.Height;if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)){width = e.MarginBounds.Width;height = image.Height * e.MarginBounds.Width / image.Width;}else{height = e.MarginBounds.Height;width = image.Width * e.MarginBounds.Height / image.Height;}//DrawImage参数根据打印机和图片大小自行调整System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x/2, y/2, width-x/2, height-y/2);//System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);if (image.Height < 310){e.Graphics.DrawImage(image, 0, 30, image.Width + 20, image.Height);//    System.Drawing.Rectangle destRect1 = new System.Drawing.Rectangle(0, 30, image.Width, image.Height);//    e.Graphics.DrawImage(image, destRect1, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);}else{//e.Graphics.DrawImage(image, 0, 0, image.Width + 20, image.Height);//e.Graphics.DrawImage(image, 0, 0, e.MarginBounds.Width, e.MarginBounds.Height);e.Graphics.DrawImage(image, destRect);//    System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);//    e.Graphics.DrawImage(image, destRect2, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);}if (printNum < fileList.Count - 1){printNum++;e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件return;}e.HasMorePages = false;}}

更多推荐

C# 打印机相关配置

本文发布于:2024-03-23 23:20:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:打印机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!