[英]C# Unmanaged code not working
This is supposed to be very simple. 这应该很简单。 I basically copied the code from MSDN for printing a form in my C# winforms application. 基本上,我从MSDN复制了用于在C#winforms应用程序中打印表单的代码。 All I get when i click on "Print" is a blank sheet of paper but no error messages. 当我单击“打印”时,我得到的只是一张空白纸,但没有错误消息。
This is the link. 这是链接。 https://msdn.microsoft.com/en-us/library/aa287529%28v=vs.71%29.aspx https://msdn.microsoft.com/en-us/library/aa287529%28v=vs.71%29.aspx
The code is: 代码是:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private 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);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}
All I need to do is print the current form. 我需要做的就是打印当前表格。 I just get a blank sheet of paper. 我刚得到一张空白纸。 I think its because of the unmanaged code(calling the dll). 我认为这是由于非托管代码(调用dll)所致。 How do I fix this? 我该如何解决?
I tested the sample in .NET 4.5.2 and it works great. 我在.NET 4.5.2中测试了该示例,并且效果很好。
I just get a blank sheet of paper. 我刚得到一张空白纸。 I think its because of the unmanaged code(calling the dll). 我认为这是由于非托管代码(调用dll)所致。 How do I fix this? 我该如何解决?
You will get a blank sheet of paper if you don't hook up the subscriber for PrintDocument.PrintPage
event. 如果不为PrintDocument.PrintPage
事件的订阅者挂机,则将得到一张空白的纸。
private void printDocument1_PrintPage(Object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
The page you linked to is from 2003, likely not going to work anymore. 您链接到的页面来自2003年,可能不再起作用。
Here is a more recent MSDN page: How to: Print a Windows Form 这是最新的MSDN页面: 如何:打印Windows窗体
You only require permissions to access the printer. 您只需要访问打印机的权限。
And the full code: 以及完整的代码:
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.