c#通过api直接控制打印机也可以将图片呈现到窗体,通过打印窗体的方式打印图片.
以下代码来自MSDN:如何:打印 Windows 窗体
作为开发过程的一部分,通常会希望打印 Windows 窗体的副本。下面的代码示例演示如何使用 CopyFromScreen 方法来打印当前窗体的副本。using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;public class Form1 :
Form
{
private Button printButton = new Button();
private PrintDocument printDocument1 = new PrintDocument(); public Form1()
{
printButton.Text = "Print Form";
printButton.Click += new EventHandler(printButton_Click);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
this.Controls.Add(printButton);
} void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
} Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
} private void printDocument1_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
} public static void Main()
{
Application.Run(new Form1());
}
}
以下代码来自MSDN:如何:打印 Windows 窗体
作为开发过程的一部分,通常会希望打印 Windows 窗体的副本。下面的代码示例演示如何使用 CopyFromScreen 方法来打印当前窗体的副本。using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;public class Form1 :
Form
{
private Button printButton = new Button();
private PrintDocument printDocument1 = new PrintDocument(); public Form1()
{
printButton.Text = "Print Form";
printButton.Click += new EventHandler(printButton_Click);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
this.Controls.Add(printButton);
} void printButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
} Bitmap memoryImage;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
} private void printDocument1_PrintPage(System.Object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
} public static void Main()
{
Application.Run(new Form1());
}
}
System.Drawing.Printing.PrintPageEventArgs e)
{
Image memoryImage = Image.FromFile(filePath); //文件路径
e.Graphics.DrawImage(memoryImage, 0, 0);
}
可以直接在这里边画图片。
都可以加上打印预览