none
Print image with print document without dialog: image printed blank RRS feed

  • Вопрос

  • Hi Guys: I have slected a printer & print size; i tried to print using Print Document class but the image printed in blank.

    Could you please tell me what is my mistake?

    private void BtnPrint_Click(object sender, RoutedEventArgs e)
            {
                if (cbxprintersizes.SelectedIndex == -1 || cbxprinters.SelectedIndex == -1)
                {
                    MessageBox.Show("Please select a printer & print size");
                    return;
                }
    
                MessageBox.Show(cbxprinters.SelectedItem.ToString()+ cbxprintersizes.SelectedItem.ToString());
              
                var printDoc = new PrintDocument();
               
                string outputPath = getrandomfile2("C:\\Users\\Pictures\\images");
                printDoc.DocumentName = outputPath;
                printDoc.PrinterSettings.PrinterName = cbxprinters.SelectedItem.ToString();
                
                printDoc.Print();
    
                
            }
    
    
    
    //Combobox to select print size
    private void Cbxprinters_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                cbxprintersizes.Items.Clear();
                var printDoc = new PrintDocument();
                printDoc.PrinterSettings.PrinterName = cbxprinters.SelectedItem.ToString();
                
    
                foreach (PaperSize paperSize in printDoc.PrinterSettings.PaperSizes)
                {
                                    cbxprintersizes.Items.Add(paperSize.PaperName);
                }
            }
     

    19 августа 2019 г. 13:40

Ответы

  • Hi,

    try to resize image:

      private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
               System.Drawing.Image myimage = System.Drawing.Image.FromFile(@"D:\image\night.jpg");
                Bitmap b = new Bitmap(myimage);
                System.Drawing.Image i = resizeImage(b, new System.Drawing.Size(1000, 600));
                e.Graphics.DrawImage(i, 20, 120);
            }
            private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, System.Drawing.Size size)
            {  
                int sourceWidth = imgToResize.Width;      
                int sourceHeight = imgToResize.Height;
                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;
                nPercentW = ((float)size.Width / (float)sourceWidth);
    
                nPercentH = ((float)size.Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;
        
                int destWidth = (int)(sourceWidth * nPercent);
     
                int destHeight = (int)(sourceHeight * nPercent);
    
                System.Drawing.Bitmap b = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage((System.Drawing.Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
                g.Dispose();
                return (System.Drawing.Image)b;
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Помечено в качестве ответа FF_DAPI 23 августа 2019 г. 13:45
    20 августа 2019 г. 8:37

Все ответы

  • its a wpf application & a button click to send a random image to the printer from a fodler  to print images using driver. 
    19 августа 2019 г. 17:44
  • Hi,

    Your code is missing the printDocument1_PrintPage event,see my code:

       public partial class MainWindow : Window
        {
            PrintDocument printDoc = new PrintDocument();
    
            public MainWindow()
            {
                InitializeComponent();
                printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            }
    
            private void BtnPrint_Click(object sender, RoutedEventArgs e)
            {
    
                //string outputPath = getrandomfile2("C:\\Users\\Pictures\\images");
                //printDoc.DocumentName = outputPath;
                //printDoc.PrinterSettings.PrinterName = cbxprinters.SelectedItem.ToString();
    
                printDoc.Print();               
            }
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
               System.Drawing.Image myimage = System.Drawing.Image.FromFile("C:\\Users\\Pictures\\imag");
                e.Graphics.DrawImage(myimage, 20, 20);
            }
        }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    20 августа 2019 г. 6:01
  • Hi Akex: thnaks i can print now but the printing size is not good.

    What i need to do print the below sizes.? do i need to resize the image before submitting to the printer driver?

    4x6    1266x1836 pixel

    Thanks,

    K


    • Изменено FF_DAPI 21 августа 2019 г. 8:13
    20 августа 2019 г. 7:19
  • Hi,

    Do you want to set the size of the picture?use the following code:

     e.Graphics.DrawImage(myimage, 20, 120,new System.Drawing.RectangleF(0,0, 1266,1236), System.Drawing.GraphicsUnit.Pixel);

    or paper size

    printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    20 августа 2019 г. 7:54
  • After uisng the below code still the printed image wasn't good.

    By any change can i use the driver paper size? 

      private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                string outputPath = getrandomfile2("C:\\Users\\krangaraj.KONICAFR\\Pictures\\images");
                System.Drawing.Image myimage = System.Drawing.Image.FromFile(outputPath);
                e.Graphics.DrawImage(myimage, 20, 120, new System.Drawing.RectangleF(0, 0, 1266, 1236), System.Drawing.GraphicsUnit.Pixel);
            }

    Code to get the driver paper sizes:

     foreach (PaperSize paperSize in printDoc.PrinterSettings.PaperSizes)
                {
                    //Console.WriteLine($"PaperName:{paperSize.PaperName}, PaperSize: {paperSize.Height},{paperSize.Width}");
                    cbxprintersizes.Items.Add(paperSize.PaperName);
                }
     

    thanks,

    • Изменено FF_DAPI 21 августа 2019 г. 8:13
    20 августа 2019 г. 8:08
  • Hi,

    try to resize image:

      private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
               System.Drawing.Image myimage = System.Drawing.Image.FromFile(@"D:\image\night.jpg");
                Bitmap b = new Bitmap(myimage);
                System.Drawing.Image i = resizeImage(b, new System.Drawing.Size(1000, 600));
                e.Graphics.DrawImage(i, 20, 120);
            }
            private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, System.Drawing.Size size)
            {  
                int sourceWidth = imgToResize.Width;      
                int sourceHeight = imgToResize.Height;
                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;
                nPercentW = ((float)size.Width / (float)sourceWidth);
    
                nPercentH = ((float)size.Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;
        
                int destWidth = (int)(sourceWidth * nPercent);
     
                int destHeight = (int)(sourceHeight * nPercent);
    
                System.Drawing.Bitmap b = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage((System.Drawing.Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
                g.Dispose();
                return (System.Drawing.Image)b;
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Помечено в качестве ответа FF_DAPI 23 августа 2019 г. 13:45
    20 августа 2019 г. 8:37
  • Hi Alex: tried your solution, still no good. 

    Why do we need to resize the image with good pixel & then draw image with another size? 

     System.Drawing.Image i = resizeImage(b, new System.Drawing.Size(1266, 1236));
                e.Graphics.DrawImage(i, 20, 120);

    20 августа 2019 г. 8:48
  • Hi,

    It may be because the picture is out of paper size,you try to narrow your picture.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    20 августа 2019 г. 8:56
  • yes i'm folliwng as per your suggestiin, by debuggung i see different size sent to the printer.

    For 4x6 landscape image size is  1836x1266 ans set in the code. But the sent image size is different.

    i'l lost here & thanks for your help. 

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                string outputPath = getrandomfile2("C:\\Users\\Pictures\\images");
                System.Drawing.Image myimage = System.Drawing.Image.FromFile("C:\\Users\\Pictures\\images\\BimbiMela.jpg");
                //e.Graphics.DrawImage(myimage, 20, 120, new System.Drawing.RectangleF(0, 0, 1266, 1236), System.Drawing.GraphicsUnit.Pixel);
                Bitmap b = new Bitmap(myimage);
                System.Drawing.Image i = resizeImage(b, new System.Drawing.Size(1836, 1266));
                e.Graphics.DrawImage(i, 20, 120);
            }

    debug:


    • Изменено FF_DAPI 20 августа 2019 г. 9:14 edited
    20 августа 2019 г. 9:12
  • Hi,

    You can try to set the size of the paper.

    printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom",2000,2000);

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    21 августа 2019 г. 9:25
  • Sorry Alex, same issue.

     private void BtnPrint_Click(object sender, RoutedEventArgs e)
            {
                if (cbxprintersizes.SelectedIndex == -1 || cbxprinters.SelectedIndex == -1)
                {
                    MessageBox.Show("Please select a printer & print size");
                    return;
                }
    
                MessageBox.Show(cbxprinters.SelectedItem.ToString()+ cbxprintersizes.SelectedItem.ToString());
              
                printDoc.PrinterSettings.PrinterName = cbxprinters.SelectedItem.ToString();
                printDoc.DefaultPageSettings.PaperSize = new PaperSize("Custom", 1836, 1266);
             
    
                printDoc.Print();
    
                
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                string outputPath = getrandomfile2("C:\\Users\\Pictures\\images");
                System.Drawing.Image myimage = System.Drawing.Image.FromFile("C:\\Users\\krangaraj.KONICAFR\\Pictures\\images\\BimbiMela.jpg");
                //e.Graphics.DrawImage(myimage, 20, 120, new System.Drawing.RectangleF(0, 0, 1266, 1236), System.Drawing.GraphicsUnit.Pixel);
                Bitmap b = new Bitmap(myimage);
                System.Drawing.Image i = resizeImage(b, new System.Drawing.Size(1836, 1266));
                e.Graphics.DrawImage(i, 20, 120);
            }

    21 августа 2019 г. 11:52
  •      e.Graphics.DrawImage(myimage, e.PageBounds);
    prepare the rigth size of the image and use the the above code it works. thanks
    23 августа 2019 г. 13:45