2011-01-26 66 views
4

我有以下方法,加载在一个空白模板图像,绘制相关信息,并将其保存到另一个文件。我想稍微改变这种实现如下:打印图像后生成[c#]

模板图像中
  • 负载
  • 画上了相关信息,则
  • 打印它

我不想保存,只是打印出来。这里是我现有的方法:

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); 
     } 
    } 

希望能对你有所帮助, 布雷特

回答

7

它只是打印到打印机,但不保存

这是最简单的例子,我可以拿出。

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(); 
3

当您使用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. 
} 

注意:请勿将Graphics对象,你在事件处理程序接收。这是由PrintDocument的对象本身做...

2

使用PrintPage事件;

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