打印在C#Windows窗体 [英] Print Windows form in c#

查看:601
本文介绍了打印在C#Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此代码打印表单:

 私人无效btnPrint_Click(对象发件人,EventArgs五)
{
图形G1 = this.CreateGraphics();
图像MYIMAGE =新位图(this.ClientRectangle.Width,this.ClientRectangle.Height,G1);
图形G2 = Graphics.FromImage(MYIMAGE);
IntPtr的DC1 = g1.GetHdc();
IntPtr的DC2 = g2.GetHdc();
的BitBlt(DC2,0,0,this.ClientRectangle.Width,this.ClientRectangle.Height,DC1,0,0,13369376);
g1.ReleaseHdc(DC1);
g2.ReleaseHdc(DC2);
MyImage.Save(@C:\PrintPage.jpg,ImageFormat.Jpeg);
的FileStream FILESTREAM =新的FileStream(@C:\PrintPage.jpg,FileMode.Open,FileAccess.Read);
StartPrint(FILESTREAM,图像);
fileStream.Close();
如果(System.IO.File.Exists(@C:\PrintPage.jpg))
{
System.IO.File.Delete(@C:\PrintPage .JPG);
}
}



但给我一个错误的位置:MyImage.Save



错误:




ExternalException是未处理:在发生一般性错误GDI +。




有人可以给我这个问题的修复程序,并解释了,为什么我'收到此错误?



在此先感谢!


解决方案

 私人无效btn_print_Click(对象发件人,EventArgs五)
{
CaptureScreen();
printDocument1.Print();
printDocument1.PrintPage + =新PrintPageEventHandler(printDocument1_PrintPage);
}

位图memoryImage;

私人无效CaptureScreen()
{
图形myGraphics = this.CreateGraphics();
大小S = this.Size;
memoryImage =新位图(s.Width,s.Height,myGraphics);
图形memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X,this.Location.Y,0,0,S);
}


私人无效printDocument1_PrintPage(对象发件人,System.Drawing.Printing.PrintPageEventArgs E)
{

}

私人无效printDocument1_PrintPage_1(对象发件人,PrintPageEventArgs E)
{
e.Graphics.DrawImage(memoryImage,0,0);
}



从工具箱中,形成拖动添加print_document。执行此代码,它会正常工作。



http://csharpprobsandsoln.blogspot.in/2013/04/print-windows-form-in-c.html


I am trying to print a form using this code:

private void btnPrint_Click(object sender, EventArgs e)
    {
        Graphics g1 = this.CreateGraphics();
        Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
        Graphics g2 = Graphics.FromImage(MyImage);
        IntPtr dc1 = g1.GetHdc();
        IntPtr dc2 = g2.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        g1.ReleaseHdc(dc1);
        g2.ReleaseHdc(dc2);
        MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
        FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
        StartPrint(fileStream, "Image");
        fileStream.Close();
        if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
        {
            System.IO.File.Delete(@"c:\PrintPage.jpg");
        }
    }

But is gives me an error at: MyImage.Save.

The error:

ExternalException was Unhandled: A generic error occurred in GDI+.

Can someone give me a fix for this problem,and explain, why I'am getting this error?

Thanks in Advance!

解决方案

    private void btn_print_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    }

    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(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

    }

    private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

Add a print_document by dragging it from the toolbox to form. Execute this code, it will work fine.

http://csharpprobsandsoln.blogspot.in/2013/04/print-windows-form-in-c.html

这篇关于打印在C#Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆