DrawToBitmap returns black Image from UserControl - c#

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

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.

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

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.

Related

winform and control size is changed by itself

i'm developing c# winform application, and having some sizing issues.
working env. :
developing on macbook, using vmware running windows 7, with visual studio.
also using infragistics winform controls.
the problem is that every time i'm opening the project, the size of the main form is changing, also the size of several controls in it is being changed.
so, i need to resize everything manually, before publishing the project.
this is very annoying, and i can't figure out what is causing this.
did someone here having the same issue, is there anything i can do to stop this?
thanks.
I have seen similar issues when windows default font size is changed in Control Panel Display settings. It probably has something to do with the Form's AutoScaleMode.
Is it changing by a lot or a little? Is it one pixel or is it a huge difference?
There were some known issues with UltraToolbarsManager in certain styles where opening the form in the designer changed the size of the form each time, but it was a very small change and it was consistent - meaning that the form would always get a little bigger or a little smaller each time.
These issues are all fixed in the latest version as far as I know.

WPF applacation window screen shot displayed on button

i have create a few WPF applications in visual studio, and now i would like to create a final application to run all of the created apps in the final application. i would like to know how i can create a layout that displays the apps that are running inside the final app as buttons that can be clicked and cause the app to expand and allow the user to work on that app and then when finished return back to the final app and see the other apps to choose from. I imagine it would look like a Google chrome blank page that allows you see your familiar webpages and click on them to load them up. Any help or advice on what i should focus on to implement this would be much appreciated.
Thank you for taking the time to read over this.
Consider using a VisualBrush this takes other WPF UI (your "apps") and uses their UI as a brush for another UI element.
Raj Kumar has a simple article called Visual Brush in WPF for details.
The bit you probably want is at the bottom where he shows you rendering a controls content in another control. The magic being binding the controls visual to the element name whos UI it should render.

WinForms control takes too long to load

In a WPF application I have a user control that hosts a win forms RichTextBox control using the WindowsFormsHost. The user control is used in a panel that is made visible at a certain time. The problem is that the forms control is displayed a few moments before the containing panel. I tried delaying the display of the RTB until the panel is shown, but then the UI freezes for 2-4 seconds when the forms control is loaded. Please help if you know a solution to make the control load faster. Thanks!
You should use a profiler to see where most of the time is consumed, have a look here: What Are Some Good .NET Profilers?
I managed to find a solution for this :) Actually, the WindowsFormsHost had a Transparent background set, and just removing the transparency solved the performance problem.

c# Form not completely repainting in RDP session

I have a strange problem and I can't seem to figure out a solution.
I have a windows forms application written in C# .net4.0.
In this application, there's a panel where i draw a chart on.
This chart is drawn on the "Paint" event of the panel.
There's also a button which launches another application on top of this application which displays other information
All of this works fine, untill I close the opened application.
When I Close it, i see my first application again but parts of it haven't been repainted.
It seems that the panel with the chart repainted correctly, the rest of it's container panel didn't.
I removed the "Paint" event of the Panel with the chart as a test and then, everything worked fine (offcourse no chart was drawn)
Strangly, I only do have this problem when the programs are in an RDP session.
On my local pc, everything works/refreshes fine.
If anybody can help me on this, it would be very much appreciated!!
For clarity I have added screenshots below:
we have a lot of rendering problems on RDP too.
Try setting the values mentioned here:
http://blog.tedd.no/2011/04/28/optimizing-rdp-for-casual-use-windows-7-sp1-remotefx/
Also we have implemented additional refreshes when using our application over rdp, although weird, it seems to help. We haven't found a really good and clean solution yet.
You can also alter RDP behavior in the options menu when you start the RDP session. We are using transparency, for example, and had to turn of bitmap caching.

Winforms WPF Interop - WPF content fails to paint

I have a WinForm that uses an ElementHost to display a WPF UserControl. Once every 50 times or so when the form loads the WPF content fails to paint. You can see through the WinForm chrome to whatever is beneath. Resizing the window gets the WPF content to show up.
Is this a known issue? Can anyone suggest a workaround?
We have fought these types of issues before. See this WPF forum post for more info on our particular flavor (I don't know if it is the same issue or not).
The only thing that we found to work was to change the size of the ElementHost.
_elementHost.Width++;
It's a complete hack, ugly, and I'm embarrassed to even post it. But nothing else ever worked for us. So, it is definitely a workaround. (Grin)
We tried Invalidate, Refresh and everything we could think of ... on the ElementHost. We also tried InvalidateMeasure, InvalidateArrange, and InvalidateVisual on the WPF hosted content. No luck.
If you find another way to fix your issue, I would love to hear about it.
Good luck, I know I have lost some hair on this one.
Update 1:
I have submitted another WPF forum post on this. Maybe we can get a response from Microsoft. Sure seems like a bug to me.
Update 2:
After I fixed the refresh issue with the above hack ... I still had another problem to solve that I thought worth mentioning here. That is: there was a definite delay until the screen refreshed. This made it seem like the user was navigating to another screen (it wasn't ... it was just the contents of the double buffering buffer). I ended up having to manually call System.Windows.Forms.Control.Refresh() on the Control that was hosting the ElementHost. In this way, even though the pause was still there ... at least the screen was blank ... and it didn't look like the user was navigating somewhere ...
the following worked for me.
On the Form_Activated event, I added the following
elementHost1.HostContainer.InvalidateVisual();
I know this post is old, but in the later version of .net (i.e 3.5), you can try to use software rendering mode. This seems to fix a lot of problems when you host a WPF control in the ElementHost.
Ref: Software rendering mode - WPF
Maybe call Invalidate on the ElementHost?
Have you tried to use reflection to see the code behind _elementHost.Width++?

Categories