0

My first post here so here goes,

i have a UserControl with about 30 controls (labels, textboxes etc). Now i would like to make a "screenshot" of it. So i used the "DrawToBitmap" method from the UserControl.

Here some samplecode

//this is the UserControl with the about 30 controls    
var sampleusercontrol = new SampleUserControl();

var bmp = new Bitmap(sampleusercontrol.Width, sampleusercontrol.Height);
sampleusercontrol.DrawToBitmap(bmp, sampleusercontrol.Bounds);

if i run this code, it returns me a black image all the time. I have no clue why. Please help!

Edit:

Forgot to say that the UserControl is a WinForms UserControl

TaW
  • 53,122
  • 8
  • 69
  • 111
Domi Light
  • 11
  • 4
  • I have had the same problem. For me, the solution was to do - sampleusercontrol.Arrange(0, 0, sampleusercontrol.Width, sampleusercontrol.height) before I created the bitmap. I have no idea why this worked, but it did... – Ralt Jun 25 '15 at 13:44
  • [`DrawToBitmap`](http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,4441da5479e7a6f8) simply sending `WM_PRINT` message. `Arrange` is wpf function, why not to [do it in the right way](http://stackoverflow.com/a/24478299/1997232) ? – Sinatr Jun 25 '15 at 14:07
  • Sorry i forgot to say that the usercontrol i am talking about, is a winforms usercontrol – Domi Light Jun 25 '15 at 14:19
  • 2
    Bounds is simply the wrong rectangle, use sampleusercontrol.DisplayRectangle instead. – Hans Passant Jun 25 '15 at 17:09
  • 1
    To add to Hans' comment: `Bounds` is 'located' at the control's `Location`, so unless it sits at the screen origin you will miss much or all of the image. Depending on whether the control is scrolled or not you may go for `ClientRectangle` for the visible portion or `DisplayRectangle` for the full content including portions that are not visible atm. – TaW Jun 25 '15 at 17:32

3 Answers3

1

This had been working for me for years until it started returning a black image last week in production. Interestingly, it was just after we applied a new windows patch. I was able to find this posting about the issue:

KB3057839 Has Broken Windows Forms Control.DrawToBitmap() When Called from Application Launched From Windows Service

You didn't mention how that winforms control was being created, but in the first case it was from a winforms app launched by a service. There is also a case on the Telerik site where the control was instantiated server-side by IIS to create a PDF file:

http://www.telerik.com/forums/export-to-pdf-597e04c01b39

Try checking for the patch KB3057839 and rolling it back for a short term fix. Hopefully Microsoft will respond with a fix or a workaround going forward.

LouD
  • 3,776
  • 4
  • 19
  • 17
  • The Windows KB3057839 update broken numerous software ranging from Putty, Telerik, GE software, Quicken 2015, and various others I'm not mentioning. – ConfusedDeer Jun 29 '15 at 17:39
  • I was able to confirm that the new patch KB3070102 overrides the previous one and fixes this issue. – LouD Aug 06 '15 at 22:52
0

I had the same issue. The latest round of ms patches corrected the issue.

Seth Greenstein
  • 153
  • 2
  • 9
0

Check that the control has been loaded.

I was struggling with this, but it turned out the control was in another tab and wouldnt display correctly until I had clicked on the tab, then run DrawToBitmap.

Ben S
  • 33
  • 8