需求:直接上图,PrinterSettings只能改变纸张大小,打印质量,无法更改打印纸类型
爱普生打印机打印照片已经设置了最高质量,打印图片仍不清晰,需要修改打印纸类型,使用PrintDialog调出对话框,修改打印纸类型后,关掉软件还需要每次人工进行调,所以需要打印照片自动选择“高质量普通纸”,打印文字使用“普通纸”
需求视频
安装包Vanara.PInvoke.Kernel32
将打印机设置保存为文件
PrintDialog printDialog = new PrintDialog();
//显示打印对话框,更改打印纸类型
printDialog.ShowDialog();
PrinterSettings printSetting = printDialog.PrinterSettings;
var devmode= printSetting.GetHdevmode();
var fileName="高质量打印纸.bin";
//将devmode序列化保存为文件,方便下次调用加载高质量打印纸
SaveHdevmode(devmode,fileName);
每次打印之前,加载上图保存的打印配置文件即可
PrintDialog printDialog = new PrintDialog();
PrinterSettings printSetting = printDialog.PrinterSettings;
IntPtr hDevMode = IntPtr.Zero;
IntPtr pDevMode = IntPtr.Zero;
hDevMode=printSetting.GetHdevmode();
//在句柄上锁定
pDevMode= Vanara.PInvoke.Kernel32.GlobalLock(hDevMode);
var source = File.ReadAllBytes("高质量打印纸.bin");
Marshal.Copy(source, 0, pDevMode, source.Length);
//解锁
Vanara.PInvoke.Kernel32.GlobalUnlock(hDevMode);
printSetting.SetHdevmode(hDevMode);
printSetting.DefaultPageSettings.SetHdevmode(hDevMode);
//释放内存
Vanara.PInvoke.Kernel32.GlobalFree(hDevMode);
打印测试
PrintDocument pd = new PrintDocument();
pd.PrintController = new StandardPrintController();
pd.PrinterSettings = printSetting;
pd.DefaultPageSettings = pageSetting;
pd.PrintPage += PicturePrintDocument;
pd.Print();
附bin文件下载,下载链接在最顶部