博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
打印图片,自动调整宽高
阅读量:4589 次
发布时间:2019-06-09

本文共 5360 字,大约阅读时间需要 17 分钟。

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Printing;using System.Linq;using System.Text;namespace BIPS.Print{    public class PrintDocumentImage    {        public event Action EndPrint;        List
listimage; ///
/// /// ///
打印机名称 ///
打印纸张大小 ///
如果该页应以彩色打印,则为 true;反之,则为 false。默认值由打印机决定。 ///
图片 ///
份数 ///
双面打印设置 public void PrintStrat(string PrintName, string documentNo, string PageSize, bool color, List
img, int Copies) { PrintDocument printdoc = new PrintDocument();//Duplex printdoc.DocumentName = documentNo; printdoc.PrinterSettings.PrinterName = PrintName; printdoc.PrinterSettings.Copies = (short)Copies; printdoc.DefaultPageSettings.Color = color;//参数设置 printdoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); if (img.Count >= 2) { //printdoc.PrinterSettings.Duplex = Duplex.Horizontal; printdoc.PrinterSettings.Duplex = Duplex.Default; } foreach (PaperSize ps in printdoc.PrinterSettings.PaperSizes) { if (ps.Kind.ToString().Equals(PageSize)) { printdoc.PrinterSettings.DefaultPageSettings.PaperSize = ps; printdoc.DefaultPageSettings.PaperSize = ps; } } this.listimage = img; printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage); printdoc.EndPrint += new PrintEventHandler(printdoc_EndPrint); this.listimage = img; printdoc.Print(); } void printdoc_EndPrint(object sender, PrintEventArgs e) { if (EndPrint != null) { EndPrint("打印完成"); } } private void printdoc_PrintPage(object sender, PrintPageEventArgs e) { if (listimage.Count > 0) { //如果是base64字符串则将base64转成图片 Image inBitmap; if (listimage[0].Length > 500) { //字符串过程认为是base64string byte[] buffer = System.Convert.FromBase64String(listimage[0]); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer)) { inBitmap = Image.FromStream(ms); } } else { inBitmap = Image.FromFile(listimage[0]); } using (inBitmap) { //打印机绘图宽高 int stdBitmapWidth = e.PageBounds.Width; int stdBitmapHeight = e.PageBounds.Height; //如果宽度比高度大,则进行旋转 if (inBitmap.Width > inBitmap.Height) { inBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); } //计算 打印机绘图区域宽高比 double width_Height = stdBitmapWidth * 1.0 / stdBitmapHeight; //计算 原图宽高比 double sWidth_Height = inBitmap.Width * 1.0 / inBitmap.Height; //打印机绘制区域的 区域 int heigth = stdBitmapHeight; int width = stdBitmapWidth; int x = 0; int y = 0; //如果图片区域比 打印机绘图区域大则需要缩放 if (stdBitmapHeight < inBitmap.Height || stdBitmapWidth < inBitmap.Width) { //如果是高度很高的图片,则按图片比例调整绘制宽度 if (width_Height > sWidth_Height) { width = (int)(sWidth_Height * heigth); //调整到左右的中间。 x = (stdBitmapWidth - width) / 2; } //如果是宽度比较宽的图片,则按图片比例调整绘制高度 else if (width_Height < sWidth_Height) { heigth = (int)(width / sWidth_Height); //t调整到上下的中间。 y = (stdBitmapHeight - heigth) / 2; } } //如果图片宽高都比绘图区域小,则不需要进行缩放。直接绘制在打印机绘图区域中央。 else { width = inBitmap.Width; heigth = inBitmap.Height; x = (stdBitmapWidth - inBitmap.Width) / 2; y = (stdBitmapHeight - inBitmap.Height) / 2; } //绘制区域 Rectangle stdRec = new Rectangle(x, y, width, heigth); //将图片绘制到打印机,绘图区域。 e.Graphics.DrawImage(inBitmap, stdRec, new Rectangle(0, 0, inBitmap.Width, inBitmap.Height), GraphicsUnit.Pixel); listimage.RemoveAt(0); if (listimage.Count > 0) { e.HasMorePages = true; } else { e.HasMorePages = false; } } } } }}

 

转载于:https://www.cnblogs.com/LittleJin/p/9798199.html

你可能感兴趣的文章
bzoj3998[TJOI2015]弦论
查看>>
leetcode:Pascal's Triangle II【Python版】
查看>>
2019 HL SC day10
查看>>
[IE编程] 多页面基于IE内核浏览器的代码示例
查看>>
对不同型号开发板的认识及环境搭建
查看>>
web.xml配置详解之listener
查看>>
tarjan模板
查看>>
请让本题永远沉睡于此(东方化改题+给的标程)
查看>>
第二第三周暑期集训总结
查看>>
C#屏幕截图
查看>>
JQuery模仿a标签的点击事件
查看>>
github hexo 搭建博客
查看>>
JS调用百度地图API获取地理位置
查看>>
BZOJ 1103 [POI2007]大都市meg(树状数组+dfs序)
查看>>
BZOJ 4260 Codechef REBXOR(字典树)
查看>>
idea使用maven逆向mybitis的文件
查看>>
宿舍助手app——个人工作第二天
查看>>
线段树(hdu 2795)
查看>>
David Sankoff 介绍学习链接
查看>>
当连续进行多个请求,并且请求的url地址相同时。放弃前面的所有请求,只执行最后一次请求。...
查看>>