• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to get the bitmap image of a control

P

Patrice

#1
Hi all

How to retrieve the bitmap image of a given WPF control (or
visualtree) ?

I'd like to be able to insert a kind of control "screen copy" in an
arbitrary destination (a Word document for instance). Is there an easy
way to do that ? I think I've seen that somewhere but I can't remember
where.

Thanx in advance.
 

My Computer

L

Laurent Bugnion, MVP

#2
Hi,

Patrice wrote:

> Hi all
>
> How to retrieve the bitmap image of a given WPF control (or
> visualtree) ?
>
> I'd like to be able to insert a kind of control "screen copy" in an
> arbitrary destination (a Word document for instance). Is there an easy
> way to do that ? I think I've seen that somewhere but I can't remember
> where.
>
> Thanx in advance.
Check the thread named "Draw a circle in WPF" earlier in this very forum.

Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

My Computer

P

Patrice

#3
On 31 oct, 23:25, "Laurent Bugnion, MVP" <galasoft...@xxxxxx>
wrote:

> Hi,
>
> Patrice wrote:

> > Hi all
>

> > How to retrieve the bitmap image of a given WPF control (or
> > visualtree) ?
>

> > I'd like to be able to insert a kind of control "screen copy" in an
> > arbitrary destination (a Word document for instance). Is there an easy
> > way to do that ? I think I've seen that somewhere but I can't remember
> > where.
>

> > Thanx in advance.
>
> Check the thread named "Draw a circle in WPF" earlier in this very forum.
>
> Greetings,
> Laurent
> --
> Laurent Bugnion [MVP ASP.NET]
> Software engineering, Blog:http://www.galasoft.ch
> PhotoAlbum:http://www.galasoft.ch/pictures
> Support children in Calcutta:http://www.calcutta-espoir.ch
Yeah, I've seen it just after having posted my message. It seems it
answers perfectly my question. thnax a lot !
 

My Computer

P

Patrice

#4
On 31 oct, 23:32, Patrice <contact-on...@xxxxxx-si.com> wrote:

> On 31 oct, 23:25, "Laurent Bugnion, MVP" <galasoft...@xxxxxx>
> wrote:
>
>
>
>
> >

> > Patrice wrote:

> > > Hi all
>

> > > How to retrieve the bitmap image of a given WPF control (or
> > > visualtree) ?
>

> > > I'd like to be able to insert a kind of control "screen copy" in an
> > > arbitrary destination (a Word document for instance). Is there an easy
> > > way to do that ? I think I've seen that somewhere but I can't remember
> > > where.
>

> > > Thanx in advance.
>

> > Check the thread named "Draw a circle in WPF" earlier in this very forum.
>

> > Greetings,
> > Laurent
> > --
> > Laurent Bugnion [MVP ASP.NET]
> > Software engineering, Blog:http://www.galasoft.ch
> > PhotoAlbum:http://www.galasoft.ch/pictures
> > Support children in Calcutta:http://www.calcutta-espoir.ch
>
> Yeah, I've seen it just after having posted my message. It seems it
> answers perfectly my question. thnax a lot !- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -
Salut Laurent,

I tried to use your code but I got the same issue described in the
last post of the thread you mentioned (empty image).

And I can't figure what I'im doing wrong.

Here is my code (vb sorry :))

Public Function GetImageFromVisual(ByVal Target As Visual, ByVal
width As Integer, ByVal height As Integer) As RenderTargetBitmap

Dim bounds As Rect =
VisualTreeHelper.GetDescendantBounds(Target)

Dim renderBitmap As New RenderTargetBitmap(width, height, 96,
96, PixelFormats.Pbgra32)

Dim visual As New DrawingVisual()
Dim context As DrawingContext = visual.RenderOpen
With context
Dim brush As New VisualBrush(Target)
context.DrawRectangle(brush, Nothing, New Rect(New
Point(), bounds.Size))
End With
renderBitmap.Render(visual)
Return renderBitmap

End Function

If ever you can see something...

Thanx in advance
 

My Computer

L

Laurent Bugnion, MVP

#5
Salut,

I don't have time to debug it, but I can send you a working example per
email if you want, and you can build on that.

My example will be in C# though :-(

Let me know

Greetings,
Laurent

Patrice wrote:

> On 31 oct, 23:32, Patrice <contact-on...@xxxxxx-si.com> wrote:

>> On 31 oct, 23:25, "Laurent Bugnion, MVP" <galasoft...@xxxxxx>
>> wrote:
>>
>>
>>
>>
>>

>>> Hi,
>>> Patrice wrote:
>>>> Hi all
>>>> How to retrieve the bitmap image of a given WPF control (or
>>>> visualtree) ?
>>>> I'd like to be able to insert a kind of control "screen copy" in an
>>>> arbitrary destination (a Word document for instance). Is there an easy
>>>> way to do that ? I think I've seen that somewhere but I can't remember
>>>> where.
>>>> Thanx in advance.
>>> Check the thread named "Draw a circle in WPF" earlier in this very forum.
>>> Greetings,
>>> Laurent
>>> --
>>> Laurent Bugnion [MVP ASP.NET]
>>> Software engineering, Blog:http://www.galasoft.ch
>>> PhotoAlbum:http://www.galasoft.ch/pictures
>>> Support children in Calcutta:http://www.calcutta-espoir.ch
>> Yeah, I've seen it just after having posted my message. It seems it
>> answers perfectly my question. thnax a lot !- Masquer le texte des messages précédents -
>>
>> - Afficher le texte des messages précédents -
>
> Salut Laurent,
>
> I tried to use your code but I got the same issue described in the
> last post of the thread you mentioned (empty image).
>
> And I can't figure what I'im doing wrong.
>
> Here is my code (vb sorry :))
>
> Public Function GetImageFromVisual(ByVal Target As Visual, ByVal
> width As Integer, ByVal height As Integer) As RenderTargetBitmap
>
> Dim bounds As Rect =
> VisualTreeHelper.GetDescendantBounds(Target)
>
> Dim renderBitmap As New RenderTargetBitmap(width, height, 96,
> 96, PixelFormats.Pbgra32)
>
> Dim visual As New DrawingVisual()
> Dim context As DrawingContext = visual.RenderOpen
> With context
> Dim brush As New VisualBrush(Target)
> context.DrawRectangle(brush, Nothing, New Rect(New
> Point(), bounds.Size))
> End With
> renderBitmap.Render(visual)
> Return renderBitmap
>
> End Function
>
> If ever you can see something...
>
> Thanx in advance
>
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)