Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can I save any control as Image (ex: Inkcanvas or Grid control) with a TRANSPARENT background if the control's background was Transparent? I can do it in WinForms Application and I don't know if it is Possible in WPF.
____________________________________________________________________________:D :)
XML
<Thanks To="All" Regards="Best"></Thanks>
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jan-13 14:28pm    
This is a pretty interesting question which demonstrates the power of WPF. However, you could easily find it out by yourself by some minimal search and browsing of MSDN documentation. Anyway, I up-voted the question with my 4.
—SA

1 solution

Lets start from the very end: transparency won't be a problem, because PNG bitmaps support transparency: http://en.wikipedia.org/wiki/Portable_Network_Graphics[^].

For rendering of something like a control on a bitmap (you actually can render any Visual and hence any control; please see below), you need to use the classes System.Windows.Media.DrawingVisual and System.Windows.Media.Imaging.RenderTargetBitmap:
http://msdn.microsoft.com/en-us/library/system.windows.media.drawingvisual.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx[^].

If you look at the MSDN help page for the class DrawingVisual, referenced above, you will see a code sample for creating a bitmap from a System.Windows.Media.Visual, but for the Visual the class System.Windows.Media.DrawingVisual is used, which you don't need. What to do? Same thing; DrawingVisual is considered only to make the example short, but you will need to use something else.

Please look a the Visual class: http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx[^].

You can see that this class is a base class for System.Windows.UIElement, and, hence, anything you are interested in. That solves your problem.

—SA
 
Share this answer
 
v3
Comments
Abed AlSayed 20-Jan-13 16:24pm    
Thanks, I've founded the Answer by myself after 3hrs of searching and reading, but I've forget to post the answer, it is the same to use 'RenderTargetBitmap'. But thanks for this very detailed Answer.

Accepted Answer, and 5 stars :)

Note : (I know that PNG type images support transparency, but when I was using WinForm applications it doesn't save the its controls supporting transparent backgrounds, Wrong??)
Sergey Alexandrovich Kryukov 20-Jan-13 16:44pm    
You are very welcome.
Good luck, call again.

As to your note on transparency in Forms: at the level of images or custom rendering, it always worked, of course. This is just about alpha channel, nothing else. As the existing controls, transparency never worked properly, due to the legacy nature of those control.

This is totally unrelated to WPF: all UIElements are lightweighted, unrelated to legacy HWND stuff and based on DirectX. Anything can overlap, be transparent, content model is very relaxed, etc.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900