Using WPF to Generate an Image

T

Tem

I'm still trying to figure out how to do this simple task with WPF.
I need to draw a black circle using WPF and generate an image file. (gif or
png)

Any help is greatly appreciated.

Tem





I used the following but it resulted in a blank file. I cannot figure out
what is wrong with it.

RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96,
PixelFormats.Pbgra32);

Ellipse cir = new Ellipse();
cir.Height = 50;
cir.Width = 50;
cir.Stroke = Brushes.Black;
cir.StrokeThickness = 1.0;

rtb.Render(cir);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fs= File.Create("test.png"))
{
png.Save(fs);
}
 
W

Willy Denoyette [MVP]

Tem said:
I'm still trying to figure out how to do this simple task with WPF.
I need to draw a black circle using WPF and generate an image file. (gif
or png)

Any help is greatly appreciated.

Tem





I used the following but it resulted in a blank file. I cannot figure out
what is wrong with it.

RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96,
PixelFormats.Pbgra32);

Ellipse cir = new Ellipse();
cir.Height = 50;
cir.Width = 50;
cir.Stroke = Brushes.Black;
cir.StrokeThickness = 1.0;

rtb.Render(cir);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fs= File.Create("test.png"))
{
png.Save(fs);
}



You are missing something like:

cir.Arrange(new Rect(new Size(50, 50)));

Note that you might get quicker answers when you consult (and post) to the
msdn WPF forum first
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=119&SiteID=1

Willy.
 
T

Tem

Thank you so much, I got it to work.

Another simple question:
I need to add another circle, slightly smaller, 45 in diameter to the same
image. the final image should be a ring.
What would be the command to add another shape?



Here's my code:

RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96,
96,PixelFormats.Pbgra32);

Ellipse cir = new Ellipse();
cir.Height = 50;
cir.Width = 50;
cir.Stroke = Brushes.Black;
cir.StrokeThickness = 1.0;

cir.Arrange(new Rect(new Size(50, 50)));

rtb.Render(cir);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fs= File.Create("test.png"))
{
png.Save(fs);
}
 
W

Willy Denoyette [MVP]

Tem said:
Thank you so much, I got it to work.

Another simple question:
I need to add another circle, slightly smaller, 45 in diameter to the same
image. the final image should be a ring.
What would be the command to add another shape?



Here's my code:

RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96,
96,PixelFormats.Pbgra32);

Ellipse cir = new Ellipse();
cir.Height = 50;
cir.Width = 50;
cir.Stroke = Brushes.Black;
cir.StrokeThickness = 1.0;

cir.Arrange(new Rect(new Size(50, 50)));

rtb.Render(cir);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fs= File.Create("test.png"))
{
png.Save(fs);
}


Draw a new ellipse inside the first one and render.

...
rtb.Render(cir);
cir.Height = 45;
cir.Width = 45;
cir.Arrange(new Rect(new Size(50, 50)));
rtb.Render(cir);
....

Willy.
 
L

Laurent Bugnion, MVP

Hi,

Serge said:
Hello,


What's the NNTP address for that forum?

(H) Serge

There is none. It's unfortunate, but Microsoft is slowly pulling out of
NNTP and keeping all the forums on the web. There is a RSS feed though.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top