0

Simple problem.

I have a form to which I add a panel and put 1 label with some text. When I look at the saved image, all I see is the panel. I have tried all the solutions I could find. Code below. I get the panel saved but the text box doesn't appear. If I can get that to work, then I can do all that I need.

What am I doing wrong?

            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            int width = printPanel.Width;
            int height = printPanel.Height;

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

            using (Bitmap flag = new Bitmap(width, height))
            {
                printPanel.DrawToBitmap(flag, bounds);

                if (Environment.UserName == "grimesr")
                {
                    string saveImage = Path.Combine(fileStore, "./p" + ".png");
                    flag.Save(saveImage);
                }
             }
GrandpaG
  • 11
  • 1
  • 3
  • Are the Label and TextBox CHILDREN of the Panel?...or are they "floating above" the panel and just look like they are children. When you use `printPanel.DrawToBitmap` you are telling the Panel to draw ITS contents; things that don't belong to the Panel will not be included. The way to tell if the other controls are part of the panel is to MOVE the panel on your Form. If the other controls don't move with the Panel then they are probably directly contained by the Form. – Idle_Mind Dec 06 '22 at 21:15
  • Positively in the panel. I move the panel and the label field moves with it. I am using MS Visual Studio 2019 .NET 4.8 if it makes any difference. – GrandpaG Dec 07 '22 at 03:12
  • What's up with your "bounds"? Normally you'd just make a bitmap the same same size as the panel and then draw at (0, 0). – Idle_Mind Dec 07 '22 at 03:27
  • What do you mean? By using the width, height of the panel I should be making the bitmap exactly the same size. Am I not correct? This makes the new bitmap (0, 0, panel width, panel height). From running in debug and pausing just before DrawToBitmap -> bounds = {X = 0 Y = 0 Width = 380 Height = 190} – GrandpaG Dec 07 '22 at 07:07
  • Also, when I look at the saved image, I get the same size as the panel and the same color as the panel. It is creating correly, just no children. – GrandpaG Dec 07 '22 at 07:13

2 Answers2

0

Really not sure where you're going wrong.

Here's a simple test:

private void button1_Click(object sender, EventArgs e)
{
    int width = printPanel.Width;
    int height = printPanel.Height;
    Rectangle bounds = new Rectangle(0, 0, width, height);
    Bitmap flag = new Bitmap(width, height);
    printPanel.DrawToBitmap(flag, bounds);
    pictureBox1.Image = flag;
}

It grabs the entire panel and puts the image into the picturebox to the right:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Thanks for the hints, even if they weren't directly related. The print button helped me figure this out. The button code worked as desired. However, putting the same code where I had it wasn't working. I then noticed a InvalidOperation error was being rasied. So looking at more in detail led me to see what the real issue was.

I must admit I left out 1 tiny piece of information that was critical. I was trying to do this in a thread that was feeding my label printer. Of course, trying to used a UI panel control in the thread threw an Invalid Operation. I moved the code out and all is well now. Cross thread operations are sometimes subtle and difficult to think about how they fail.

Problem solved for me.

GrandpaG
  • 11
  • 1
  • 3