Windows Vista Beta | WinVistaBeta.com - Message | Save WPF Control as PNG image

November 19, 2008  
Subject: Save WPF Control as PNG image
Group: microsoft.public.windows.developer.winfx.avalon
Date: 9/11/2008 3:35:48 AM
From: Jon Harrop [Email Address Protection]


I'd like to save a WPF control as a PNG image. I've tried using the
following code:

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry

but the calls the Measure and Arrange screw up the layout of the control
which is then drawn incorrectly until the user resizes the window. I don't
understate what state they've changed but I am unable to fix it. I've tried
adding calls to InvalidateMeasure and InvalidateArrange and UpdateLayout
but they do not fix the problem.

I just discovered that you can use the backwards compatibility with WinForms
stuff in "Forms.Integration" to create an ElementHost that has a
DrawToBitmap method for Win32 bitmaps.

What is the recommended way to do this? Am I really supposed to resort to
calling the backwards compatibility stuff and using WinForms?

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u

Back
Subject: Re: Save WPF Control as PNG image
Group: microsoft.public.windows.developer.winfx.avalon
Date: 9/11/2008 9:36:51 AM
From: "Mark Salsbery [MVP]" [Email Address Protection]

There's lots of other examples of using RenderTargetBitmap floating around
that don't use Measure/Arrange.

Here's just one:
http://blogs.msdn.com/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx


Mark

--
Mark Salsbery
Microsoft MVP - Visual C++



"Jon Harrop" <jon@ffconsultancy.com> wrote in message
news:DaSdnVgGD6f1e1XVnZ2dnUVZ8jidnZ2d@bt.com...
>
> I'd like to save a WPF control as a PNG image. I've tried using the
> following code:
>
> http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry
>
> but the calls the Measure and Arrange screw up the layout of the control
> which is then drawn incorrectly until the user resizes the window. I don't
> understate what state they've changed but I am unable to fix it. I've
> tried
> adding calls to InvalidateMeasure and InvalidateArrange and UpdateLayout
> but they do not fix the problem.
>
> I just discovered that you can use the backwards compatibility with
> WinForms
> stuff in "Forms.Integration" to create an ElementHost that has a
> DrawToBitmap method for Win32 bitmaps.
>
> What is the recommended way to do this? Am I really supposed to resort to
> calling the backwards compatibility stuff and using WinForms?
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?u


Back
Subject: Re: Save WPF Control as PNG image
Group: microsoft.public.windows.developer.winfx.avalon
Date: 9/11/2008 12:48:49 PM
From: Jon Harrop [Email Address Protection]

Mark Salsbery [MVP] wrote:
> There's lots of other examples of using RenderTargetBitmap floating around
> that don't use Measure/Arrange.
>
> Here's just one:
>
http://blogs.msdn.com/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx

That solution does not work correctly when the control is already in a
window. Specifically, the control appears in the output image at its offset
in the window.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u

Back
Subject: Re: Save WPF Control as PNG image
Group: microsoft.public.windows.developer.winfx.avalon
Date: 9/11/2008 12:57:05 PM
From: "Mark Salsbery [MVP]" [Email Address Protection]

"Jon Harrop" <jon@ffconsultancy.com> wrote in message
news:La2dnRxn6vCZ9VTVnZ2dnUVZ8uGdnZ2d@bt.com...
> Mark Salsbery [MVP] wrote:
>> There's lots of other examples of using RenderTargetBitmap floating
>> around
>> that don't use Measure/Arrange.
>>
>> Here's just one:
>>
> http://blogs.msdn.com/swick/archive/2007/12/02/rendering-ink-and-image-to-a-bitmap-using-wpf.aspx
>
> That solution does not work correctly when the control is already in a
> window. Specifically, the control appears in the output image at its
> offset
> in the window.

I only meant to point out there's lots of different examples of using
RenderTargetBitmap.

Here's another, possibly addressing the offset bitmap...
http://www.west-wind.com/Weblog/posts/150676.aspx

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?u


Back