简体   繁体   中英

Save an Image/Bitmap from hidden panels

I have a TabControl with four tabs, and every Tab contains a panel. I want save all panels with one click, but i can only save the panel in the front.

Do anyone know another way ?

Bitmap eins = new Bitmap(p1.Width, p1.Height);
eins.Save(".string."+1+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

I don't see where you are filling anything into the Bitmap .

This should work for all your Panels , no matter which TabPage they are on:

foreach (Panel px in new Panel[] { p1, p2, p3, p4 } )
    using (Bitmap bmp = new Bitmap(pX.ClientSize.Width, pX.ClientSize.Height))
    {
        pX.DrawToBitmap(bmp, pX.ClientRectangle);
        bmp.Save(somefolder + pX.Name + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }

In fact it will even work if the Panels are actually invisible and not just in one of the hidden TabPages !

Of course you must make sure that the Panels have unique and filename compliant Names ..

Update : Now that I know that you have Controls inside the Panel and not just graphics you draw and maybe a BackgroundImage , I can understand the problems you had..

Unfortunately TabPages have a nasty way of hiding embedded controls.

So I wrote a little helper routine that doesn't cause more than a blink of the main form going inactive and back..:

void SaveHiddencontrol(Control ctl, string fileName)
{
    Control originalParent = ctl.Parent;

    Form fff = new Form();
    fff.Opacity = 0;
    ctl.Parent = fff;
    fff.Show();
    System.Drawing.Imaging.ImageFormat fmt = System.Drawing.Imaging.ImageFormat.Jpeg;
    if (fileName.ToLower().EndsWith(".png")) fmt = System.Drawing.Imaging.ImageFormat.Png;
    using (Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height))
    {
        ctl.DrawToBitmap(bmp, ctl.ClientRectangle);
        bmp.Save(fileName, fmt);
    }
    ctl.Parent = originalParent;
    fff.Close();
}

Here is the result:

在此处输入图片说明

Note how the Form itself is completely transparent and yet lets the Panel do its DrawToBitmap just fine, including an image, an ellipse and two controls and even independent of the Form.Size ..!

Update2 And here is a function that dosn't blink at all, at least if the panel is indeed not visible:

void SaveHiddencontrol(Control ctl, string fileName)
{
    Control originalParent = ctl.Parent;
    int oldLeft = ctl.Left;
    ctl.Left = 22222;  // way outside
    ctl.Parent = this;
    System.Drawing.Imaging.ImageFormat fmt = System.Drawing.Imaging.ImageFormat.Jpeg;
    if (fileName.ToLower().EndsWith(".png")) fmt = System.Drawing.Imaging.ImageFormat.Png;
    using (Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height))
    {
        ctl.DrawToBitmap(bmp, ctl.ClientRectangle);
        bmp.Save(fileName, fmt);
    }
    ctl.Parent = originalParent;
    ctl.Left = oldLeft;
}

This simply moves the Panel onto the main Form but way to the right , so that it won't show. Then saves and it moves it back. Of course you should check if the Panel is indeed on a hidden TabPage or else it will blink; in that case the original routine will do..

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