简体   繁体   中英

Winform Textbox on top of image not printing

I have a Winform application that creates signs. Everything works and looks fine except when I print. I have an image with textboxes placed on top of them. They are visible on my computer, but not when I print. I am assuming that somehow when I print the image is getting "Brought-to-front."

Below is my Print Function:

private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintImage);
        pd.Print();      
    }

    void PrintImage(object o, PrintPageEventArgs e)
    {
        int x = SystemInformation.WorkingArea.X;
        int y = SystemInformation.WorkingArea.Y;
        int width = this.Width;
        int height = this.Height; 

        Rectangle bounds = new Rectangle(x, y, width, height); 

        Bitmap img = new Bitmap(width, height); 

        this.DrawToBitmap(img, bounds);
        Point p = new Point(100, 100);
        e.Graphics.DrawImage(img, p);    
    }

I don't know for sure that anything in the print function is a cause, but I can't think of anything else.

I don't know the answer to the question of why the TextBox contents are not being rendered, but I can tell you that you are doing it "wrong".

What you should be doing is rendering the text in your paint handler and using a single, "in-place" TextBox for allowing the user to edit text at some location on the form which is moved into position and made visible just-in-time for editing.

It requires that your "document" consist of lists of objects (like "text blocks") which you can render and which you can detect the bounds of for when the user tries to manipulate them. This is very similar to how a "paint" program works.

This is going to be a complete departure from what you are doing now. It's always a lot more work to do things "correctly". I don't want to tell you to redo your application. If this is a learning experience and not a commercial product, it's OK to hack your way through it, using what you're familiar with. But maybe next time you might try another approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM