Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print image after it's been generated [c#]

I have the following method that loads in a blank template image, draws the relevant information on it and saves it to another file. I want to change this slightly to achieve the following:

  • load in the template image
  • draw the relevant information on it
  • print it

I don't want to save it, just print it out. Here's 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);
        }
    }

Hope you can help, Brett

like image 426
Brett Avatar asked Dec 16 '22 18:12

Brett


1 Answers

Just print it to the printer without saving

This is the most simple 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();
like image 77
Albin Sunnanbo Avatar answered Dec 29 '22 12:12

Albin Sunnanbo