面板上的 DrawToBitmap 为空白 [英] DrawToBitmap on Panel is blank

查看:21
本文介绍了面板上的 DrawToBitmap 为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我编写了一个类,它存储了一些测试结果信息,然后是一个向用户显示该信息的控件.我想在这个类上放置一个打印功能,以全页大小绘制控件并打印它.然而它总是空白.代码将面板视为控件,因为它可能是其他类型的值.我想一定是我遗漏了一些简单的东西.

So I've written a class that has a stores some test results info and then a control that displays that info to the user. I want to put a print function on this class to draw the control at a full page size and print it. However it always comes out blank. The code see the panel as a control because it could be some other type of value. I figure there must be something simple I'm missing.

void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Size oldSize = printData.Size;

        printData.Size = new System.Drawing.Size(e.MarginBounds.Width, e.MarginBounds.Height);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Size.Width, printData.Size.Height);

        InvertZOrderOfControls(printData.Controls);
        printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, printData.Size.Width, printData.Size.Height));
        InvertZOrderOfControls(printData.Controls);

        e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
        bitmap.Save(@"C:\Users\jdudley\Documents\File.bmp");
        printData.Size = oldSize;
    }

遵循 这个线程反转了控件的 Z 顺序,但它没有改变任何东西.添加了保存调用以进行调试.看起来它实际上是在没有任何控件的情况下渲染面板的背景颜色.

Following this advice of this thread inverted the Z-Order of the controls but it didn't change anything. The save call was added for debugging. It looks like it's actually rendering the background color of the panel without any of the controls.

这是在打印的上下文中,但我对打印没有任何问题.我的错误在于创建位图.我添加的保存行证明了这一点,因为它创建了一个空白位图文件.

This is in the context of printing but I have not issue with printing what so ever. My error is in creating the bitmap. The save line I added proves this because it creates a blank bitmap file.

推荐答案

把你的整个活动改成这个

Change your entire event to this

    void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
        printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
        e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
    }

编辑

这是我的整个项目.我创建了一个名为 printData 的面板并添加了两个按钮,我将一个事件附加到了 button1.

This is my whole Project. I created a panel named printData and I added two buttons, I attached an event to button1.

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    PrintDocument printDocument = new PrintDocument();
    public Form1()
    {
        InitializeComponent();
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    }

    void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(printData.Width, printData.Height);
        printData.DrawToBitmap(bitmap, new System.Drawing.Rectangle(new Point(0, 0), printData.Size));
        e.Graphics.DrawImage(bitmap, e.MarginBounds.Location);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        pd.Print();
    }
}
}

你必须试试这个,看看它是否有效,否则我今晚睡不着!!

You have to try this and see if it works, or else I wont be able to sleep tonight!!

这篇关于面板上的 DrawToBitmap 为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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