2

I have a serious problem here. Iam programming a tool in which the user can design a Control Cubicle for a switchgear. The Cubicle is drawn with Panels and Pictureboxes, which is working nice and looks good.

Now I want to make an Export function which exports the designed Cubicle into an pdf file.

So far so good, the function works - on my computer only! I use CopyFromScreen to get a Screenshot of the Panel in which the cubicle is shown, save that into a file and put it into a pdf file ( I also tried to get a picture of the panel using DrawToBitmap, but this isnt working properly as it is drawing some Pictureboxes over others). On my computer it captures the panel correctly and shows the correct picture in the pdf, however on every other computer it takes a picture of what is behind the Form. This is quite confusing as I have no idea why it should do that and why it is working on my system. So far I tried a few things to force the window to be on top but nothing will work, everybody gets pictures of the desktop or the window that is behind.

Code:

void takeScreenshot()
    {
        this.TopMost = true;
        this.BringToFront();
        this.Focus();
        Application.DoEvents();

        System.Drawing.Rectangle bounds = Mainpanel.Bounds;
        bounds.Width = bounds.Width - 6;
        bounds.Height = bounds.Height - 4;
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(Mainpanel.PointToScreen(new Point()).X + 3, Mainpanel.PointToScreen(new Point()).Y + 2, 0, 0, bounds.Size);
            }
            bitmap.Save(Application.StartupPath + "\\data\\programdata\\temppic.bmp");
        }
        this.TopMost = false;
    }

Iam actually quite desperate, without the export the program is useless.

Does anyone have an idea how to solve this?

Jonathan

flautzr
  • 387
  • 2
  • 8
  • `Application.DoEvents();` is evil! http://www.codinghorror.com/blog/2004/12/is-doevents-evil.html – Andre Sep 04 '13 at 09:09
  • Try removing `BringToFront` and `Focus`. this may help or may not. not sure – Sriram Sakthivel Sep 04 '13 at 09:12
  • To draw you should try a different approach. Use a panel and draw everything yourself, with the OnPaint handler or the Paint event. After that you can draw everything on a bitmap and easily export the bitmap. – Andre Sep 04 '13 at 09:25
  • i know that would have been the best way to do .. but i use a lot of different boxes, movable by drag&drop and it was just the easier way .. .well now i know i should have done it with onPaint but its too late .. – flautzr Sep 04 '13 at 09:32
  • i had it without bringtofront and focus in the first place .. I added them after the program behaved like it does now – flautzr Sep 04 '13 at 09:33
  • 1
    This is likely to happen on older machines that run XP. You will need to ensure that your window is painted, call this.Update(); BringToFront() is also pretty likely to fail although that's less likely to be the root problem. Microsoft.VisualBasic.Interaction.AppActivate() carries a bigger hammer. – Hans Passant Sep 04 '13 at 09:34

2 Answers2

0

Use this code.

void takeScreenshot()
    {
        Application.DoEvents();
        System.Drawing.Rectangle bounds = Mainpanel.Bounds;
        bounds.Width = bounds.Width - 6;
        bounds.Height = bounds.Height - 4;

        using (Bitmap bitmap = new Bitmap(Mainpanel.Width, Mainpanel.Height))
        {
            Mainpanel.DrawToBitmap(bitmap, new Rectangle(3, 2, bounds.Width, bounds.Height));
            bitmap.Save(Application.StartupPath + "\\data\\programdata\\temppic.bmp");
        }
    }
0

you can just fade the form out and then take your picture then fade it in again like so :

    private void captur_Click(System.Object sender, System.EventArgs e)
{
    Opacity = 0;
    // put the code you used to capture the screen
    Opacity = 1;
}