private System.Drawing.Printing.PrintDocument printDocument1; private void button1_Click(object sender, System.EventArgs e) { SendKeys.Send("%{PRTSC}"); Application.DoEvents();//Processes all Windows messages currently in the message queue this.printDocument1.Print(); }
下面的范例通过点击button来调用打印事件。
首先在类中定义一个System.Drawing.Printing.PrintDocument对象,在button1_click方法中调用SendKeys.Send("%{PRTSC}"),相当于您同时按alt+printscreen键,将当前窗口的图像复制到clipboard,然后调用System.Drawing.Printing.PrintDocument对象的print方法,进行打印。然后在PrintPage方法中将clipboard中的图像打印出来。
private System.Drawing.Printing.PrintDocument printDocument1;
private void button1_Click(object sender, System.EventArgs e)
{
SendKeys.Send("%{PRTSC}");
Application.DoEvents();//Processes all Windows messages currently in the message queue
this.printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
IDataObject iData = Clipboard.GetDataObject ();
Image img = (Image)iData.GetData (DataFormats.Bitmap);
e.Graphics.DrawImage(img,0,0);
}
- 微软全球技术中心 开发技术支持
网上一找一大堆啊
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());
}
}
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); 错误:D:\My Documents\Visual Studio Projects\WindowsApplication1\Form1.cs(348): “System.Drawing.Graphics”并不包含对“CopyFromScreen”的定义
如:
1.printDocument1是没有赋值的,到this.printDocument1.Print();这里报空值错误 ;
2.//然后在PrintPage方法中将clipboard中的图像打印出来可是printDocument1_PrintPage()这个方法并没有调用到呀?
***********
4 楼mywindyboy(华剑香)回复于 2002-08-07 16:12:25 得分 0 还差一点,printDocument1_PrintPage怎么和this.printDocument1.Print();建立联系呢?
Top5 楼mywindyboy(华剑香)回复于 2002-08-07 16:30:27 得分 0 哦,知道了***********
我的问题也相似
兴趣的加QQ群:47110748
我的QQ是:531345405
大家要支持哦 !
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
IDataObject iData = Clipboard.GetDataObject ();
Image img = (Image)iData.GetData (DataFormats.Bitmap);
e.Graphics.DrawImage(img,0,0);
}
这个就ok吧???
//下例使用 CopyFromScreen 画窗体
Bitmap memorybitmap;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
memorybitmap = new Bitmap(this.Size.Width, this.Size.Height,g);
g = Graphics.FromImage(memorybitmap);
g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
memorybitmap.Save(@"c:\b.bmp");//保存为图片
}
//加入一个Printdocument对象,直接打印这个图片
private System.Drawing.Printing.PrintDocument printDocument1;
private void Form_水印_Load(object sender, EventArgs e)
{
printDocument1=new System.Drawing.Printing.PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
} void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memorybitmap, new Point(0, 0));
} Bitmap memorybitmap;
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
memorybitmap = new Bitmap(this.Size.Width, this.Size.Height,g);// g);
//Graphics mg = Graphics.FromImage(memorybitmap);
g = Graphics.FromImage(memorybitmap);
g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
//memorybitmap.Save(@"c:\b.bmp");
printDocument1.Print();
}
“System.Drawing.Graphics”并不包含对“CopyFromScreen”的定义
“System.Drawing.Graphics”并不包含对“CopyFromScreen”的定义怎么解决???????????