Home > Software design >  Why am I missing controls with bit
Why am I missing controls with bit

Time:12-08

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);
                }
             }

CodePudding user response:

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

CodePudding user response:

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.

  • Related