C#非托管代码不工作
这应该很简单。我基本上是从MSDN复制代码,以便在我的C#winforms应用程序中打印表单。当我点击“打印”时,我得到的只是一张空白纸,但没有错误信息 这是链接。 代码是:C#非托管代码不工作,c#,C#,这应该很简单。我基本上是从MSDN复制代码,以便在我的C#winforms应用程序中打印表单。当我点击“打印”时,我得到的只是一张空白纸,但没有错误信息 这是链接。 代码是: [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeig
[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();
}
我需要做的就是打印当前表单。我刚得到一张白纸。我认为这是因为非托管代码(调用dll)。如何修复此问题?您链接到的页面是2003年的,可能不再有效 以下是最近的MSDN页面: 您只需要权限即可访问打印机 完整代码:
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());
}
}
你链接到的页面是2003年的,可能不再有用了 以下是最近的MSDN页面: 您只需要权限即可访问打印机 完整代码:
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());
}
}
我在.NET4.5.2中测试了这个示例,效果非常好 我刚得到一张白纸。我认为这是因为非托管代码(调用dll)。我该如何解决这个问题 如果不为
PrintDocument.PrintPage
事件连接订阅服务器,您将得到一张空白纸
private void printDocument1_PrintPage(Object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
我在.NET 4.5.2中测试了该示例,效果非常好 我刚得到一张白纸。我认为这是因为非托管代码(调用dll)。我该如何解决这个问题 如果不为
PrintDocument.PrintPage
事件连接订阅服务器,您将得到一张空白纸
private void printDocument1_PrintPage(Object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
太好了!谢谢MDeSchaepmeester!工作正常。我会在
图形
对象周围使用块。未正确处理GDI内容已经让我不止一次陷入麻烦。@Chris我只逐字记录了MSDN页面中的代码,作为防止可能的链接损坏的对策。@MDeSchaepmeester-Oh,来自MSDN?那太糟糕了。。!超级的!谢谢MDeSchaepmeester!工作正常。我会在图形
对象周围使用块。未正确处理GDI内容已经让我不止一次陷入麻烦。@Chris我只逐字记录了MSDN页面中的代码,作为防止可能的链接损坏的对策。@MDeSchaepmeester-Oh,来自MSDN?那太糟糕了。。!