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 - Render WPF Pages to Images

 
 
Old 08-23-2006   #1 (permalink)
liubin.net@gmail.com


 
 

Render WPF Pages to Images

Is there any way to render WPF pages to images, just like mem dc in GDI
or Graphics(&Bitmap(...)) in GDI+ ?

Thanks very much.


My System SpecsSystem Spec
Old 08-24-2006   #2 (permalink)
=?Utf-8?B?VGhlUkhvZ3Vl?=


 
 

RE: Render WPF Pages to Images

You want to use a RenderTargetBitmap which takes any FrameworkElement...here
is a code snippet:

private Brush CreateBrushFromUIElementWithBitmap(UIElement element,
BrushMappingMode viewboxUnits, Rect viewbox, BrushMappingMode viewportUnits,
Rect viewport)
{
FrameworkElement fe = element as FrameworkElement;
if (fe == null)
return null;

//Snap the current visual of "this" to a bitmap to be used in 3d
animation
RenderTargetBitmap bitmapImage = new
RenderTargetBitmap((int)fe.ActualWidth, (int)fe.ActualHeight, 96, 96,
PixelFormats.Pbgra32);
bitmapImage.Render(element);

ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;

imageBrush.ViewboxUnits = viewboxUnits;
imageBrush.Viewbox = viewbox;
imageBrush.ViewportUnits = viewportUnits;
imageBrush.Viewport = viewport;

return imageBrush as Brush;
}


If you want to save the image as a file, you can do something like this:

_Encoder = new BmpBitmapEncoder();
RenderTargetBitmap bitmapImage = new
RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 96, 96,
PixelFormats.Pbgra32);
bitmapImage.Render(element);

_Encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
FileStream stream = new FileStream(_Path, FileMode.Create);
_Encoder.Save(stream);
stream.Flush();
stream.Close();


"liubin.net@gmail.com" wrote:

> Is there any way to render WPF pages to images, just like mem dc in GDI
> or Graphics(&Bitmap(...)) in GDI+ ?
>
> Thanks very much.
>
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
render xml page .NET General
CS1.6 can only run as software render under vista RC1? Vista Games


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