1

My question is related to this one: How to get a screen capture of a .Net WinForms control programmatically?

I want to take a screenshot of a System.Windows.Forms.Control in C#. I'm using the DrawToBitmap method suggested in the question linked above and that works most of the time. However there are a few problems.

Problem 1:
I have two tabpages, let's call them A and B. The Control I want to take a screenshot of is in tabpage B. I want to take the screenshot when a button in tabpage A is pressed. This works most of the time, except when tabpage B hasn't been accessed yet: then the screenshot is just white. If I first access tabpage B, then go back to tabpage A and click the button to take the screenshot then it works fine. I'm guessing this is because of some loading or building of the control in the tab that hasn't been done yet, but I'm not sure what exactly (or it could be something else entirely). I've been trying to force that loading or building using ResumeLayout, PerformLayout, Show, Update, Invalidate but that doesn't work.
EDIT: Managed to solve this by using DrawToBitmap on the containing tabpage control instead of the Control itself and doing a Show on that tabpage.

Problem 2:
When I take a screenshot of a certain custom Control (a subclass of UserControl) there is a small rectangular white area in the screenshot (where there shouldn't be one obviously). The rectangular area isn't on one particular part of the control like a button or textbox so I'm not sure what's causing this. On other custom Controls (also subclasses of UserControl) it works fine, so that couldn't be the problem itself.
EDIT: Solved it, there was an empty control there that was being drawn on top of it. Setting Visible to false for that control solved it.

Community
  • 1
  • 1
user968698
  • 429
  • 3
  • 14
  • I've used `DrawToBitmap` with my custom controls in .net 2.0 and haven't had any extra white rectangles. Does your control have any transparent parts ? – Djole Sep 28 '11 at 10:11

2 Answers2

0

You can do with this code, the rest of the work and finding the correct coordinates is for the reader to do the homework.

int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • 2
    That will not work at all. That takes a screenshot of what's currently on the screen. What i want a screenshot of is in a tab that is not currently on the screen. Please take the effort of actually reading my post. – user968698 Sep 28 '11 at 09:17
  • 1
    @Luis If tab B is non-active in that moment screenshot will not be made correctly :( – Tadeusz Sep 28 '11 at 09:25
0

For problem 1: If "If I first access tabpage B, then go back to tabpage A and click the button to take the screenshot then it works fine" then add in formloadevent imitating of that actions:

tabControl.SelectedIndex = IndexOfTabB;
tabControl.SelectedIndex = IndexOfTabA;

That is trick but it will work.

For problem 2: Can you compare sizes of your control and screenshot of It and give results to us? If Control.Width is not equal wirdth of bitmap screen then It is a real problem.

Tadeusz
  • 6,453
  • 9
  • 40
  • 58
  • I managed to solve problem 1 using a different method (see my edited question), but thanks for your help anyway. Let me know if you have any idea about problem 2. – user968698 Sep 28 '11 at 10:04