Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista Tutorial - Save WPF Control as PNG image

 
 
Old 09-11-2008   #1 (permalink)
Jon Harrop
Guest


 
 

Save WPF Control as PNG image


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/c...E19A!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

My System SpecsSystem Spec
Old 09-11-2008   #2 (permalink)
Mark Salsbery [MVP]
Guest


 
 

Re: Save WPF Control as PNG image

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/...using-wpf.aspx


Mark

--
Mark Salsbery
Microsoft MVP - Visual C++



"Jon Harrop" <jon@xxxxxx> wrote in message
newsaSdnVgGD6f1e1XVnZ2dnUVZ8jidnZ2d@xxxxxx
Quote:

>
> 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/c...E19A!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
My System SpecsSystem Spec
Old 09-11-2008   #3 (permalink)
Mark Salsbery [MVP]
Guest


 
 

Re: Save WPF Control as PNG image

"Jon Harrop" <jon@xxxxxx> wrote in message
news:La2dnRxn6vCZ9VTVnZ2dnUVZ8uGdnZ2d@xxxxxx
Quote:

> Mark Salsbery [MVP] wrote:
Quote:

>> 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/...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++
Quote:

>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?u
My System SpecsSystem Spec
Old 09-17-2008   #4 (permalink)
Art
Member


Join Date: Jul 2008
64bit
 
 

Re: Save WPF Control as PNG image

Quote  Quote: Originally Posted by Jon Harrop View Post
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.
Flying Frog Consultancy Ltd.
Try this "image" converter...

Pixillion Free Image File Converter - Convert jpeg, gif pdf and more.

It's free and works great.................................... ART
My System SpecsSystem Spec
Old 09-30-2008   #5 (permalink)
Newbie


Join Date: Sep 2008
Vista 32bit..
 
 

Re: Save WPF Control as PNG image

I dont know if you have found your solution or not..
but this is how it works for me..

Code:
        public static ImageSource ToImageSource(FrameworkElement obj)
        {
            // Save current canvas transform
            Transform transform = obj.LayoutTransform;
            obj.LayoutTransform = null;
            
            // fix margin offset as well
            Thickness margin = obj.Margin;
            obj.Margin = new Thickness(0, 0,
                 margin.Right - margin.Left, margin.Bottom - margin.Top);

            // Get the size of canvas
            Size size = new Size(obj.Width, obj.Height);
            
            // force control to Update
            obj.Measure(size);
            obj.Arrange(new Rect(size));

            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)obj.Width, (int)obj.Height, 96, 96, PixelFormats.Pbgra32);
            
            bmp.Render(obj);

            // return values as they were before
            obj.LayoutTransform = transform;
            obj.Margin = margin;
            return bmp;
        }
My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
save image from picturebox .NET General
How to save image as HD Photo? Vista music pictures video
Save drive image to network storage device Vista General
Save Image as PDF? Vista print fax & scan


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46