Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , i am working on window application and i need print on my image full on paper without cut and loss in C#, i was use print document class and maintain aspect ratio too but still not able to do it, i was try all here, if any one know and help me then it's great , look at my code if any problem then tell me what did i wrong ,

C#
if (string.IsNullOrWhiteSpace(textBox1.Text)) return; // Prevents execution of below statements if filename is not selected.

                PrintDocument pd = new PrintDocument();
              
                PrintController printController = new StandardPrintController();
                pd.PrintController = printController;

                pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                pd.PrintPage += (sndr, args) =>
                {
                    System.Drawing.Image i = System.Drawing.Image.FromFile(textBox1.Text);
                    System.Drawing.Rectangle m = args.MarginBounds;

                    //Logic below maintains Aspect Ratio 
                    if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
                    {
                        m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
                    }
                    else
                    {
                        m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
                    }

                    //Calculating optimal orientation.
                    pd.DefaultPageSettings.Landscape = m.Height > m.Width;

                    // Putting image in center of page.
                    m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
                    m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
                    args.Graphics.DrawImage(i, m);
                };
                pd.Print();
Posted
Updated 16-Mar-15 0:20am
v2
Comments
Sinisa Hajnal 17-Mar-15 3:35am    
What IS your problem? You described what you want, but not what you get as a result of your code...is the print result cropped, distorted? Not printed (empty pages)? What?

You have too complicated condition for aspect ratio: having if i.Width > i.Height is enough.
Member 11202411 17-Mar-15 8:16am    
Everything is fine with above my code but when i print any image on printer , " on printed image both left and right hand side very little white space ( white color) occurred i.e. image not print full paper, but i want image print full paper without cut or loss. i.e. original image print full paper on the printer.
Sinisa Hajnal 17-Mar-15 8:49am    
You cannot print absolutely without margins, at least not universally. You could probably adjust for your own printer, but first time you get to another printer (maybe even of the same type) you'll get either cropped image or white borders.

You can change margins/paddings in printer properties, set them to zero or even negative.
Member 11202411 17-Mar-15 9:04am    
please if you tell me how then it's great help for me, i was put my code already?
Member 11202411 17-Mar-15 9:28am    
Please look at my above code , i already set paper and printer margin, then what can i do now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900