Hi,
I would like to use WPF because is the newer technology. However I could
not find a code sample on how to do this.
This is what I need to do
The app has a button called generate circle
when the buttons is clicked it generates a gif file of a circle in the
local dir.
From what I understand I don't need to use XAML, no UI here, just need
to generate a gif file
Thank you,
Tem
Saving any XAML scene to a picture is very easy. I prefer to use PNG, so
that's what this example is about, but I guess you can choose other
formats:
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(
BitmapFrame.Create(CaptureScreenBitmap(this.RootImage)));
enc.Save(fs);
}
with:
private BitmapSource CaptureScreenBitmap(Panel panel)
{
return CaptureScreenBitmap(panel,
(int) panel.ActualWidth,
(int) panel.ActualHeight);
}
private BitmapSource CaptureScreenBitmap(Visual target,
int width,
int height)
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap renderBitmap
= new RenderTargetBitmap(width,
height,
96, 96,
PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush brush = new VisualBrush(target);
context.DrawRectangle(brush,
null,
new Rect(new Point(), bounds.Size));
}
renderBitmap.Render(visual);
return renderBitmap;
}
HTH,
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