C# 打印机
2021-03-22 15:23
标签:microsoft mil process ntb obj mes setting size 设置 程序控制打印机打印存在问题,即不按默认参数打印。 一开始按如下代码打印,会弹出点击确认的对话框,为了不弹窗,采用PrintDocument类打印 PrintDocument打印代码如下 其中:DefaultPageSettings参数不用代码设置,有的如页边距无法设置(只读的),此时只要在设置-打印机-设置打印机的首选项参数即可 C# 打印机 标签:microsoft mil process ntb obj mes setting size 设置 原文地址:https://www.cnblogs.com/llstart-new0201/p/9486246.html //ProcessStartInfo info = new ProcessStartInfo(@"D:\Demo\picWorldCup\Test01.png");
////info.Arguments = string.Format("-noquery -landscape -printer \"{0}\" \"{1}\"",
//// "Canon MG3600 series Printer", @"D:\Demo\picWorldCup\Test01.png");
//info.Verb = "Print";
//info.CreateNoWindow = true;
//info.WindowStyle = ProcessWindowStyle.Hidden;
//Process.Start(info);
class Program
{
static void Main(string[] args)
{
//bool b = true;
//ProcessStartInfo info = new ProcessStartInfo(@"D:\Demo\picWorldCup\Test01.png");
////info.Arguments = string.Format("-noquery -landscape -printer \"{0}\" \"{1}\"",
//// "Canon MG3600 series Printer", @"D:\Demo\picWorldCup\Test01.png");
//info.Verb = "Print";
//info.CreateNoWindow = true;
//info.WindowStyle = ProcessWindowStyle.Hidden;
//Process.Start(info);
//string printWin = "打印图片";
//string printBtn = "取消";
//Stopwatch sw = new Stopwatch();
//sw.Start();
//while(b)
//{
// if(sw.ElapsedMilliseconds>200)
// {
// sw.Stop();
// b = false;
// }
//}
//ApiUtility.OnRunClick(printWin, printBtn);
string printFile = args[0];
//string printerName = args[1];
int width;
int height;
if (!int.TryParse(args[1], out width))
{
width = 355;
}
if (!int.TryParse(args[2], out height))
{
height = 500;
}
//Console.WriteLine("Print size:" + width + " " + height);
PrintDocument pd = new PrintDocument();
pd.PrintController = new StandardPrintController();
//Margins margin = new Margins(0, 0, 0, 0);
//pd.DefaultPageSettings.Margins = margin;
//pd.DefaultPageSettings.Color = false;
//pd.DefaultPageSettings.PaperSize = new PaperSize("L", 44, 63);
//pd.DefaultPageSettings.Margins = margin;
//pd.PrinterSettings.PrinterName = printerName;
pd.PrintPage += (o, e) =>
{
Image img = Image.FromFile(printFile);
int imgWidth = (int)(img.Height * 0.7f);
e.Graphics.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imgWidth, img.Height), GraphicsUnit.Pixel);//通用
//Console.WriteLine("image size:" + imgWidth + " " + img.Height);
//e.Graphics.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
//Console.WriteLine("W:{0} H:{1}", img.Width, img.Height);
};
try
{
pd.Print();
Console.WriteLine("success");
}
catch (Exception ex)
{
Console.WriteLine("failed:" + ex.Message);
}
//btnPrint_Click();
Console.ReadKey();
}
public static void btnPrint_Click()
{
PrintDocument pd = new PrintDocument();
pd.PrintController = new StandardPrintController();
//pd.OriginAtMargins = true;
//Margins margin = new Margins(0, 0, 0, 0);
//pd.DefaultPageSettings.Color = false;
//pd.DefaultPageSettings.PaperSize = new PaperSize("L", 89, 127);
//pd.PrinterSettings.PrinterName = "Canon MG3600 series Printer";
//pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
//pd.DefaultPageSettings.Margins = margin;
Console.WriteLine("Margin:" + pd.DefaultPageSettings.Margins);
Console.WriteLine("Bound:" + pd.DefaultPageSettings.Bounds);
Console.WriteLine("xHardMargin:" + pd.DefaultPageSettings.HardMarginX);
Console.WriteLine("yHardMargin:" + pd.DefaultPageSettings.HardMarginY);
pd.PrintPage += PrintPage;
try
{
pd.Print();
}
catch(Exception ex)
{
Console.WriteLine("Print exception:" + ex.Message);
}
}
private static void PrintPage(object o, PrintPageEventArgs e)
{
Image img = Image.FromFile(@"D:\Demo\picWorldCup\005.png");
//int width = int.Parse((img.Height * 0.7f).ToString());
int width = (int)(img.Height * 0.7f);
//e.Graphics.DrawImage(img, new Rectangle(0, 0, 70, 100), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);
//e.Graphics.DrawImage(img, new Rectangle(0,0, 350, 500),new Rectangle(0,0, width, img.Height),GraphicsUnit.Pixel);
//e.Graphics.DrawImage(img, new Rectangle(0, 0, 350, 500), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);//手机拍照
//e.Graphics.DrawImage(img, new Rectangle(0, 0, 355, 500), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);//AR拍照
e.Graphics.DrawImage(img, new Rectangle(0, 0, 355, 500), new Rectangle(0, 0, width, img.Height), GraphicsUnit.Pixel);//通用
//e.Graphics.DrawImage(img, new Rectangle(0,0, 89, 127),new Rectangle(0,0, width, img.Height),GraphicsUnit.Millimeter);
//e.Graphics.DrawImage(img, 0,0,width,img.Height);
Console.WriteLine("W:{0} H:{1}", img.Width, img.Height);
}
}