在它之后打印图像';已生成[c#]

在它之后打印图像';已生成[c#],c#,image,file,printing,C#,Image,File,Printing,我使用以下方法加载一个空白模板图像,在其上绘制相关信息并将其保存到另一个文件中。我想对此稍作修改,以实现以下目标: 加载模板图像 在上面画出相关的信息 打印出来 我不想保存它,只是把它打印出来。以下是我现有的方法: public static void GenerateCard(string recipient, string nominee, string reason, out string filename) { // Get a bitmap.

我使用以下方法加载一个空白模板图像,在其上绘制相关信息并将其保存到另一个文件中。我想对此稍作修改,以实现以下目标:

  • 加载模板图像
  • 在上面画出相关的信息
  • 打印出来
我不想保存它,只是把它打印出来。以下是我现有的方法:

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);
        }
    }
希望你能帮忙,
Brett

使用PrintDocument类时,无需保存图像即可打印

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

pd.Print()
在pd_PrintPage事件处理程序中:

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.
}

注意:不要处理在eventhandler中收到的图形对象。这是由PrintDocument对象本身完成的…

只需将其打印到打印机而不保存即可

这是我能想到的最简单的例子

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();
利用事件

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