简体   繁体   中英

Print Fit to Paper C#

About

I'm using WinForms. In my Form I have a open and print button. The Open button opens tif images into a picturebox. The print button prints these pictures from the picture box. I work with big image documents, for example width and length: (3000, 3600). So i scaled these tif image documents to fit to the regular printing paper size (8.5 x 11). The reason i did this is so the letters on the tif image wont be blurry using the method below.

Issue

The good news is it scaled nicely meaning its not blurry. The bad news is it scaled down to much. See Figure A.2

Test

Test i increased and decreased the * 100 the weird thing is it does not increase the size but it decreases the size

 float newWidth = i.Width * 100 / i.HorizontalResolution; float newHeight = i.Height * 100 / i.VerticalResolution; 


Code

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        { //pageViewer = picturebox
            Image i = pageViewer.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);
        }


How its suppose to print

在此处输入图片说明

How it is currently printing Figure A.2 在此处输入图片说明

Your image already contains a margin, so when you use the e.MarginBounds property, you are effectively doubling your margins. To fix, use the PageBounds property instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM