Search code examples
c#winformsuser-controlscontrolsdrawtobitmap

DrawToBitmap returns black Image from UserControl


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


Solution

  • 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.