Print with Win 8.1 without opening the print preview [closed]

0

I would like to know if anyone has had the opportunity to investigate how to print images by selecting them from the hard drive and without printing the image, using c #.

    
asked by Lina Manjarrés Ortiz 16.02.2016 в 22:38
source

1 answer

1

In Windows 8 onwards, there are two types of applications that can be executed:

  • ModernUI applications that are designed to be published in the Windows Store and from where they will be installed.
  • Let's say traditional applications, which do not need to be published in the Store and can be executed simply with a double click.
  • If this is the second case, then you must use the PrintDocument class. This class is in charge of sending the content that you indicate (text, images, etc.) to the printer that you indicate.

    There is a dialog box to select a printer and another to configure parameters that you can invoke optionally from your application, but it is not mandatory ...

    The following is a sample code to perform the task ...

        using System.Drawing;
        using System.Drawing.Printing;
    
        namespace PrinterTest
        {
          class Program
          {
               static string imageToPrint = @"c:\temp\foto.png";
    
               static void Main(string[] args)
               {
                  PrintDocument prn = new PrintDocument();
                  prn.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; //Debes indicar a qué impresora se enviará el documento.
                  //Con Printdocument se genera el contenido página a página, para esto se dispara un evento PrintPage
                  prn.PrintPage+=prn_PrintPage; //Acá indicamos el Event Handler que se invocará para generar el contenido de cada página.
                  prn.Print();
               }
    
              private static void prn_PrintPage(object sender, PrintPageEventArgs e)
              {
                  Image img = Image.FromFile(imageToPrint);
                  //El método DrawImage envía una imagen a impresión.
                  e.Graphics.DrawImage(img, 10,10,img.Width,img.Height);
                  //Si esta es la última página, HasMorePages es False.
                  e.HasMorePages = false;
              }
            }
         }
    

    It is still necessary to verify issues of scaling the image, if for example you want it to occupy the whole page, but that is not so complex anymore.

    In the case of ModernUI applications, it seems to me that it is not feasible to do it according to the following information.

    Printing from Windows app directly without bringing Print Dialog

    I hope the information helps you.

        
    answered by 17.02.2016 в 00:29