打印PictureBox图像适合A4纸张尺寸? [英] print PictureBox image fit to A4 paper size?

查看:604
本文介绍了打印PictureBox图像适合A4纸张尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有一个项目,我的一个表单显示一个图片框,它有一个用于打印图片框图像的打印按钮,我想在A4纸张尺寸的图片框上打印图像而不关心图片框大小,我用谷歌搜索,发现不止一个解决方案尝试了所有这些,但所有打印图像的图片大小或小于A4大小

这是我使用的最后一个代码

I have a project in c#, one of my forms displays a picture box and it have a print button for printing picture box image, i want to print the image on picture box fit on A4 paper size without care about picture box size, i googled and found more than one solution tried all of them, but all print the image with the size of picture box or smaller than the A4 size
this is my last code that i used

        private void print_bt_Click(object sender, EventArgs e)
        {            System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
            myPrintDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1170);
            PrintDialog myPrinDialog1 = new PrintDialog();

            myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

            myPrinDialog1.Document = myPrintDocument1;
            if (myPrinDialog1.ShowDialog() == DialogResult.OK)
            {

                myPrintDocument1.Print();

            }
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
        Image i = show_pic_box.Image;
        float newWidth = i.Width * 100 / i.HorizontalResolution;
        float newHeight = i.Height * 100 / i.VerticalResolution;

        float widthFactor = newWidth / e.MarginBounds.Width;
        float heightFactor = newHeight / e.MarginBounds.Height;

        if(widthFactor>1 | heightFactor > 1)
        {
            if(widthFactor > heightFactor)
            {
                newWidth = newWidth / widthFactor;
                newHeight = newHeight / widthFactor;
            }
            else
            {
                newWidth = newWidth / heightFactor;
                newHeight = newHeight / heightFactor;
            }
        }
        e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);
}



有人能给我一个好的解决方案吗?


can anyone gives me a good solution please?

推荐答案

使用基于矩形的DrawImage重载: https://msdn.microsoft.com/en-us/ library / ms142040(v = vs.110).aspx [ ^ ]您可以输入要打印的矩形大小,并对其进行适当的拉伸/压缩。



请注意,如果原始图像很小,A4看起来不太好!
Use the Rectangle based DrawImage overload: https://msdn.microsoft.com/en-us/library/ms142040(v=vs.110).aspx[^] You feed it the size of the rectangle to print into, and it stretches / compresses it appropriately.

Do note that if the original image is small, it won't look good at A4!


这篇关于打印PictureBox图像适合A4纸张尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆