Print image after it is created [C #]

I have the following method, which loads into an empty image of a template, draws relevant information on it and saves it in another file. I want to change this a bit to achieve the following results:

  • Uploading a template image
  • draw relevant information about him
  • print it

I do not want to save it, just print it. Here is my existing method:

public static void GenerateCard(string recipient, string nominee, string reason, out string filename) { // Get a bitmap. Bitmap bmp1 = new Bitmap("template.jpg"); Graphics graphicImage; // Wrapped in a using statement to automatically take care of IDisposable and cleanup using (graphicImage = Graphics.FromImage(bmp1)) { ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); // Create an Encoder object based on the GUID // for the Quality parameter category. Encoder myEncoder = Encoder.Quality; graphicImage.DrawString(recipient, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(480, 33)); graphicImage.DrawString(WordWrap(reason, 35), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(566, 53)); graphicImage.DrawString(nominee, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(492, 405)); graphicImage.DrawString(DateTime.Now.ToShortDateString(), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(490, 425)); EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; filename = recipient + " - " + DateTime.Now.ToShortDateString().Replace("/", "-") + ".jpg"; bmp1.Save(filename, jgpEncoder, myEncoderParameters); } } 

I hope you can help, Brett

+4
source share
3 answers

Just print it on the printer without saving

This is the simplest example I could come up with.

 Bitmap b = new Bitmap(100, 100); using (var g = Graphics.FromImage(b)) { g.DrawString("Hello", this.Font, Brushes.Black, new PointF(0, 0)); } PrintDocument pd = new PrintDocument(); pd.PrintPage += (object printSender, PrintPageEventArgs printE) => { printE.Graphics.DrawImageUnscaled(b, new Point(0, 0)); }; PrintDialog dialog = new PrintDialog(); dialog.ShowDialog(); pd.PrinterSettings = dialog.PrinterSettings; pd.Print(); 
+7
source

When you use the PrintDocument class, you can print without having to save the image.

 var pd = new PrintDocument(); pd.PrintPage += pd_PrintPage; pd.Print() 

And in the pd_PrintPage event handler:

 void pd_PrintPage(object sender, PrintPageEventArgs e) { Graphics gr = e.Graphics; //now you can draw on the gr object you received using some of the code you posted. } 

NOTE. Do not delete the Graphics object that you received in the event handler. This is done by the PrintDocument object itself ...

+3
source

Use PrintPage Event

  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(image, 0, 0); } 
+2
source

Source: https://habr.com/ru/post/1337116/


All Articles