honar abubakr

honar abubakr

  • NA
  • 73
  • 108.4k

Printing PictureBox image to fit on A4 paper size?

Mar 25 2015 9:36 AM
hello, i'm working on a small project using c# windows forms, in my project i have a form with two buttons and a picturebox the first button is for scan an image from the scanner using WIA program and display it on a picturebox the another button is form printing the pictureBox image on an A4 paper (fit A4 without margins), i have a problem with printing button when i print the it does not fit the A4 paper i googled for the problem and tried more than one article but still i have a problem, any one can help please? and this is the last code that i used         private void print_bt_Click(object sender, EventArgs e)
        {
            try
            {
                string p_height = "1100";// height of A4 paper size
                double Height = Convert.ToDouble(p_height);
                double width = 0;
                double aspectRatio = 0;
                aspectRatio = (double)PictureBox1.Image.Width / (double)PictureBox1.Image.Height;
                width = Height * aspectRatio;

                    int wi = 0;
                    wi = Convert.ToInt32(width);
                    int hi = 0;
                    hi = Convert.ToInt32(Height);
                    Bitmap New_Bitmap = new Bitmap(PictureBox1.Image, wi, hi);
                    PictureBox1.Image = New_Bitmap;

                if (myPrinDialog.ShowDialog() == DialogResult.OK)
                {
                    prntdoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
                    prntdoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                    prntdoc.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error" + ex.Message);
            }
        }
        private void prntdoc_PrintPage1(object sender, PrintPageEventArgs e)
        {
            e.PageSettings.Margins = new Margins(0, 0, 0, 0);
            e.Graphics.DrawImage(PictureBox1.Image,0, 0);
        }