I
I
iremezoff2012-04-16 11:53:46
.NET
iremezoff, 2012-04-16 11:53:46

DrawImage in PrintDocument - inserting an image for printing, scaling problems

The problem is the following. When a print event occurs, we process the parameters of the PrintPageEventHandler delegate, where the Graphics object is stored in one of the variables (the PrintPage event).
I'm trying to insert any image on the canvas, and then actually view it with the standard preview control. And this is where the problems begin. The image is inserted, but everything would be fine, but it is scaled with some factor and as a result the inserted image becomes blurry both during preview and when printing, the original itself is lost.
I tried to change the resolution of the inserted image (SetResolution) to what is stored in the Graphics object obtained from the PrintPageEventHandler delegate parameter (I had 600dpi). I tried to change PageScale to 1 (although this is the default value). I tried to change the units of measurement to inches (inch) and points (point).
I tried a lot of things, but I still can't get a normal image. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rasa, 2012-04-18
@rasa

I recently solved this issue, and did not understand how best to do it. While the draft version works like this
(although it still cuts the edges a little). I copied part of the code:

 InitializeComponent();
    PrintDocument pd = new PrintDocument();
                string printerU = "PDF Printer";
                pd.PrinterSettings.PrinterName = printerU;
                PageHeight = pd.PrinterSettings.DefaultPageSettings.PaperSize.Height;
                PageWidth = pd.PrinterSettings.DefaultPageSettings.PaperSize.Width;
                pd.PrintPage += new PrintPageEventHandler(PrintImage);

                if (pd.PrinterSettings.IsValid)
                {
                    pd.Print();
                }
                else
                {
                    // Printer is invalid
                }
                pd.Dispose();
            }            
        }

        private void PrintImage(Object sender, PrintPageEventArgs e)
        {
            System.Drawing.Image img =
                          System.Drawing.Image.FromFile("123.bmp");
            e.Graphics.DrawImage(img, 0, 0, PageWidth, PageHeight);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question