winfrom 打印图片,打印A4纸大小,自适应_winform 照片打印 自适应边框-程序员宅基地

技术标签: c#  c++  java  winfrom  

           private int printNum = 0;//多页打印
    private string imageFile = "";//单个图片文件
    private ArrayList fileList = new ArrayList();//多个图片文件
    private void button2_Click_1(object sender, EventArgs e)
    {
        PrintPreview();
    }



    public void PrintPreview()
    {
        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 = false;

        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)
    {

        FileStream fsa = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open);
        byte[] bytes = new byte[fsa.Length];
        fsa.Read(bytes, 0, bytes.Length);
        fsa.Close();
        MemoryStream ms = new MemoryStream(bytes);
        Bitmap image = new Bitmap(ms);
        //pictureBox1.Image = image;

        //图片抗锯齿
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Stream fs = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open, FileAccess.Read);
        //System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
        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, y, width, height);
        //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, 800, 1123);
            //   System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
            System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(x, y, width, 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;
        }
        printNum = 0;
        e.HasMorePages = false;
        image.Dispose();
        fs.Dispose();
        fs.Close();
       




    }
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43044132/article/details/126260912

智能推荐

在Windows平台上安装MRTG流量监控软件_mrtg 下载-程序员宅基地

文章浏览阅读188次。打开MRTG软件包中的"MRTG.cfg"文件,该文件是MRTG的主配置文件。打开MRTG软件包中的"MRTG.cfg"文件,该文件是MRTG的主配置文件。确保将命令中的"C:\MRTG"替换为你的MRTG安装目录和配置文件路径,"community"替换为你的SNMP团体字符串,"device_ip"替换为目标设备的IP地址。确保将命令中的"C:\MRTG"替换为你的MRTG安装目录和配置文件路径,"community"替换为你的SNMP团体字符串,"device_ip"替换为目标设备的IP地址。_mrtg 下载

kaggle简单使用教程(代码查找.下载、项目建立.运行、参加比赛)_kaggle在线写代码-程序员宅基地

文章浏览阅读1w次,点赞7次,收藏35次。Kaggle机器学习竞赛、托管数据库、编写和分享代码_kaggle在线写代码

network.service - LSB: Bring up/down networking_network.service - lsb: bring up/down networking lo-程序员宅基地

文章浏览阅读3.1k次,点赞11次,收藏14次。CentOS7突然连接不了网络,使用systemctl status network后报如下错误network.service - LSB: Bring up/down networkingLoaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)Active: failed (Result: exit-code)【解决方案】停止NetworkManager并取消开机启动chkconfig NetworkMan_network.service - lsb: bring up/down networking loaded: loaded (/etc/rc.d/in

GitHub上10个有趣的开源小游戏(附加在线演示)_github开源小游戏-程序员宅基地

文章浏览阅读4.9w次,点赞312次,收藏1.3k次。前言GitHub作为程序员们的开源宝库,有着很多非常好的项目。对于初学者来说,游戏有着一种特殊的魅力。今天统计了GitHub上比较有趣的10个开源小游戏,其中有许多可以称之为经典。笔者是一名90后,《贪吃蛇》、《坦克大战》、《超级马里奥》和《太空侵略者》作为儿时的玩伴,陪伴笔者度过了很多时光,给笔者带来了非常多的回忆。1、Pacman(吃豆人游戏)项目演示地址: https://passe..._github开源小游戏

Java数据结构和算法(十二)——2-3-4树,java面试题,java高级笔试题_树查找 java 笔试题-程序员宅基地

文章浏览阅读210次。写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家。扫码加微信好友进【程序员面试学习交流群】,免费领取。也欢迎各位一起在群里探讨技术。通过前面的介绍,我们知道在二叉树中,每个节点只有一个数据项,最多有两个子节点。如果允许每个节点可以有更多的数据项和更多的子节点,就是多叉树。本篇博客我们将介绍的——2-3-4树,它是一种多叉树,..._树查找 java 笔试题

随便推点

3D单目(mono 3D)目标检测算法综述_mono3d-程序员宅基地

文章浏览阅读1.1w次,点赞12次,收藏105次。layout: posttitle: 3D单目(mono 3D)目标检测算法综述date: 2021-01-22 22:08:39.000000000 +09:00categories: [算法篇]tags: [CV, 3D, 综述]欢迎访问个人博客:https://johneyzheng.top/前言算法调研(相对完善)2D升3D问题表达形式(Representation transformation): BEV, Pseudo-Lidar关键点&&形状通过2._mono3d

YOLO V8车辆行人识别_yolov8 无法识别路边行人-程序员宅基地

文章浏览阅读537次。与其他目标检测方法相比,YOLO V8拥有更高的速度和更好的准确率,它能够在实时视频流中运行,并且可以同时检测多个目标。我们可以使用Keras或TensorFlow等框架来实现模型,并使用OpenCV来进行车辆检测。我们将图片缩放为128x128像素的大小,并使用预训练的模型来进行车辆检测。在车辆识别中,我们需要训练一个针对车辆的模型。首先,我们需要准备一组包含车辆的图片,并标注车辆的位置和大小。我们可以使用OpenCV来读取图片,并使用YOLO V8模型来进行车辆检测。_yolov8 无法识别路边行人

jpa mysql分页_Spring Boot之JPA分页-程序员宅基地

文章浏览阅读141次。JPA分页​当请求的数据总量很大时,这时候前端往往都会要求后端将数据分页返回。本文介绍SpringBoot下后端数据层使用JPA+MySQL时,如何分页返回数据(除了当前页面的数据,往往还要返回总页数这项数据)。一、从头到尾自己实现分页:Controller层:使用@RequestParam绑定page和pageSize参数,调用ServiceService层:接收page、pageSize参数,..._jpa mysql limit 分页

win10打印图片中间空白以及选择打印机预览重启_win10更新后打印图片中间空白-程序员宅基地

文章浏览阅读7.6k次。当月10号左右大量windows10系统发现打印照片时只能打印出头和尾,如下还有没开始打印,一选择打印机电脑就重启,是因为微软发布的新补丁不兼容,卸载最近更新的补丁即可(不同系统版本补丁编号是不一样的,看最近日期就行了)打开控制面板-卸载程序查看已安装的更新按时间排序双击卸载最新的补丁重启即可..._win10更新后打印图片中间空白

【加密】SHA256加盐加密_sha256随机盐加密-程序员宅基地

文章浏览阅读2.4k次。SaltUtil 类 private final String algorithmName = "SHA-256"; private final int hashIterations = 10000; private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator(); //默认16位……//方法中调用// 生成salt model.se_sha256随机盐加密

cordys 启动流程_cordys服务重启-程序员宅基地

文章浏览阅读763次。启动操作js ://--by wallvar startReq=BPMStartXml.XMLDocument;cordys.setNodeText(startReq,".//*[local-name()='BusinessID']",pursh_id.getValue());cordys.setNodeText(startReq,".//*[local-name()_cordys服务重启