473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Draw a circle in WPF

Tem
I need to draw a black lined circle and save it as a gif file. Can this be
done with wpf or do I need to use GDI+
The examples I found seem to only apply to UI elements not a file.

Thank you

Tem

Oct 27 '07 #1
7 14345
Tem,

This can be done in both WPF and in Windows Forms (through GDI). The
thing is, what are YOU using? If you are using Windows Forms, then in the
method you use to paint, you can use the DrawEllipse method on the Graphics
instance to draw a circle (just make sure to use a square for the bounding
coordinates and it will produce a circle).

In WPF, you should just be able to add an Ellipse element into your XAML
with the same height and width.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tem" <te*****@yahoo.comwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>I need to draw a black lined circle and save it as a gif file. Can this be
done with wpf or do I need to use GDI+
The examples I found seem to only apply to UI elements not a file.

Thank you

Tem
Oct 27 '07 #2
Tem
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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:EF**********************************@microsof t.com...
Tem,

This can be done in both WPF and in Windows Forms (through GDI). The
thing is, what are YOU using? If you are using Windows Forms, then in the
method you use to paint, you can use the DrawEllipse method on the
Graphics instance to draw a circle (just make sure to use a square for the
bounding coordinates and it will produce a circle).

In WPF, you should just be able to add an Ellipse element into your
XAML with the same height and width.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tem" <te*****@yahoo.comwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>>I need to draw a black lined circle and save it as a gif file. Can this be
done with wpf or do I need to use GDI+
The examples I found seem to only apply to UI elements not a file.

Thank you

Tem
Oct 27 '07 #3
If you are generating a GIF file, then you are better off creating a new
Bitmap instance and then getting the Graphics instance for the bitmap
(through the static FromImage method on the Graphics class). Draw on that
using the Graphics instance, and then you can save the Bitmap (as a GIF of
course).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tem" <te*****@yahoo.comwrote in message
news:uE**************@TK2MSFTNGP03.phx.gbl...
>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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:EF**********************************@microsof t.com...
>Tem,

This can be done in both WPF and in Windows Forms (through GDI). The
thing is, what are YOU using? If you are using Windows Forms, then in
the method you use to paint, you can use the DrawEllipse method on the
Graphics instance to draw a circle (just make sure to use a square for
the bounding coordinates and it will produce a circle).

In WPF, you should just be able to add an Ellipse element into your
XAML with the same height and width.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tem" <te*****@yahoo.comwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>>>I need to draw a black lined circle and save it as a gif file. Can this
be done with wpf or do I need to use GDI+
The examples I found seem to only apply to UI elements not a file.

Thank you

Tem
Oct 27 '07 #4
Hi,

Tem wrote:
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.RootIm age)));
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
Oct 28 '07 #5
Tem
Thank you for the example, there's one thing

what should i replace this.RootImage with?
Tem

"Laurent Bugnion, MVP" <ga*********@bluewin.chwrote in message
news:O$*************@TK2MSFTNGP06.phx.gbl...
Hi,

Tem wrote:
>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.RootIm age)));
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
Oct 29 '07 #6
Hi,

Tem wrote:
Thank you for the example, there's one thing

what should i replace this.RootImage with?
Tem
In my example, the RootImage is the panel containing the scene that you
want to capture to an image. It can be a grid, a canvas, etc...

Laurent
>
"Laurent Bugnion, MVP" <ga*********@bluewin.chwrote in message
news:O$*************@TK2MSFTNGP06.phx.gbl...
>Hi,

Tem wrote:
>>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.RootIm age)));
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
--
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
Oct 29 '07 #7
Tem
I tried your example I was able to generate a png but it has nothing in it.
a blank picture.
Here's my code, it also gives me a blank file.

I cannot figure out why it doesn't work.


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);
}

Oct 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chua Wen Ching | last post by:
Hi.. just wonder i draw a circle in the picturebox1 1) and i want to store the circle in memory (only circle) when i store into bmp... i want to see the circle with transparent...
0
by: Richard | last post by:
I want to put a GDI type circle on top of my DataGrid (actually I want to put GDI colored border around the entire selected row, but if I can figure out how to put a circle on top of it I can do the...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
8
by: knranjit | last post by:
my points are: 10,10 20,20 30,30 40,40 and so on .... With these points I can draw many lines continuosly. Then I would like to place the small circle in the connecting points like 20,20
1
by: hehehewalrus | last post by:
Hi folks, I'm a newbie to doing images and graphics in PHP. I would appreciate your help in the following: 1. Is there a function to draw arrowed lines in PHP? 2. Is there a way I can label a...
5
by: lgeastwood | last post by:
I have tweaked the PictureBox97.mdb (Stephen Lebans <www.lebans.com>) code to nicely draw lines, rectangles and circles to the specs that I input. I'm at a loss though with trying to setup an...
0
by: Tem | last post by:
I need to draw a black circle using WPF and generate an image file. I used the following but it resulted in a blank file. I cannot figure out what is wrong with it. Thanks, Tem ...
3
by: gemguy | last post by:
Hi, I have to draw a circle in php using the createimage function. Is there any predefined function like that in php. I need to create circle of specified width and height. Any ideas
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.