我有以下方法加载空白模板图像,在其上绘制相关信息并将其保存到另一个文件。我想稍微改变一下以实现以下目标:
我不想保存它,只需打印出来。这是我现有的方法:
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);
}
}
希望你能提供帮助, 布雷特
答案 0 :(得分: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();
答案 1 :(得分: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 :(得分:2)
使用PrintPage事件;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.DrawImage(image, 0, 0);
}