简体   繁体   English

从隐藏的面板保存图像/位图

[英]Save an Image/Bitmap from hidden panels

I have a TabControl with four tabs, and every Tab contains a panel. 我有一个带有四个选项卡的TabControl,每个选项卡都包含一个面板。 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 . 我看不到要在Bitmap中填充任何内容的位置。

This should work for all your Panels , no matter which TabPage they are on: 无论TabPage是哪个TabPage ,这对您的所有Panels均适用:

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 ! 实际上,即使Panels 实际上是 不可见的 ,而不仅仅是在隐藏的TabPages之一中,它甚至也可以工作!

Of course you must make sure that the Panels have unique and filename compliant Names .. 当然,您必须确保Panels具有唯一且符合文件名的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.. 更新 :现在,我知道您在Panel Controls ,而不仅仅是您绘制的图形,还有BackgroundImage ,我可以理解您遇到的问题。

Unfortunately TabPages have a nasty way of hiding embedded controls. 不幸的是, TabPages具有隐藏嵌入式控件的讨厌方法。

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 ..! 注意如何Form本身是完全透明的 ,但让Panel做了DrawToBitmap就好了,包括图像,椭圆形和两个控制,甚至独立Form.Size ..!

Update2 And here is a function that dosn't blink at all, at least if the panel is indeed not visible: Update2这是一个根本不闪烁的函数,至少在面板确实不可见的情况下:

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. 这只是将Panel移动到主Form而是way to the right ,因此它不会显示。 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; 当然,您应该检查Panel是否确实在隐藏的TabPage ,否则它将闪烁。 in that case the original routine will do.. 在这种情况下,原始例程将可以执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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