473,804 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14431
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.co m

"Tem" <te*****@yahoo. comwrote in message
news:ee******** ******@TK2MSFTN GP03.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.c omwrote in
message news:EF******** *************** ***********@mic rosoft.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.co m

"Tem" <te*****@yahoo. comwrote in message
news:ee******** ******@TK2MSFTN GP03.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.co m

"Tem" <te*****@yahoo. comwrote in message
news:uE******** ******@TK2MSFTN GP03.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.c omwrote
in message news:EF******** *************** ***********@mic rosoft.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.co m

"Tem" <te*****@yahoo. comwrote in message
news:ee******* *******@TK2MSFT NGP03.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.OpenOr Create))
{
PngBitmapEncode r enc = new PngBitmapEncode r();
enc.Frames.Add(
BitmapFrame.Cre ate(CaptureScre enBitmap(this.R ootImage)));
enc.Save(fs);
}

with:

private BitmapSource CaptureScreenBi tmap(Panel panel)
{
return CaptureScreenBi tmap(panel,
(int) panel.ActualWid th,
(int) panel.ActualHei ght);
}

private BitmapSource CaptureScreenBi tmap(Visual target,
int width,
int height)
{
Rect bounds = VisualTreeHelpe r.GetDescendant Bounds(target);
RenderTargetBit map renderBitmap
= new RenderTargetBit map(width,
height,
96, 96,
PixelFormats.Pb gra32);

DrawingVisual visual = new DrawingVisual() ;
using (DrawingContext context = visual.RenderOp en())
{
VisualBrush brush = new VisualBrush(tar get);
context.DrawRec tangle(brush,
null,
new Rect(new Point(), bounds.Size));
}
renderBitmap.Re nder(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*********@bl uewin.chwrote in message
news:O$******** *****@TK2MSFTNG P06.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.OpenOr Create))
{
PngBitmapEncode r enc = new PngBitmapEncode r();
enc.Frames.Add(
BitmapFrame.Cre ate(CaptureScre enBitmap(this.R ootImage)));
enc.Save(fs);
}

with:

private BitmapSource CaptureScreenBi tmap(Panel panel)
{
return CaptureScreenBi tmap(panel,
(int) panel.ActualWid th,
(int) panel.ActualHei ght);
}

private BitmapSource CaptureScreenBi tmap(Visual target,
int width,
int height)
{
Rect bounds = VisualTreeHelpe r.GetDescendant Bounds(target);
RenderTargetBit map renderBitmap
= new RenderTargetBit map(width,
height,
96, 96,
PixelFormats.Pb gra32);

DrawingVisual visual = new DrawingVisual() ;
using (DrawingContext context = visual.RenderOp en())
{
VisualBrush brush = new VisualBrush(tar get);
context.DrawRec tangle(brush,
null,
new Rect(new Point(), bounds.Size));
}
renderBitmap.Re nder(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*********@bl uewin.chwrote in message
news:O$******** *****@TK2MSFTNG P06.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.OpenOr Create))
{
PngBitmapEncode r enc = new PngBitmapEncode r();
enc.Frames.Add(
BitmapFrame.Cre ate(CaptureScre enBitmap(this.R ootImage)));
enc.Save(fs);
}

with:

private BitmapSource CaptureScreenBi tmap(Panel panel)
{
return CaptureScreenBi tmap(panel,
(int) panel.ActualWid th,
(int) panel.ActualHei ght);
}

private BitmapSource CaptureScreenBi tmap(Visual target,
int width,
int height)
{
Rect bounds = VisualTreeHelpe r.GetDescendant Bounds(target);
RenderTargetBit map renderBitmap
= new RenderTargetBit map(width,
height,
96, 96,
PixelFormats.Pb gra32);

DrawingVisual visual = new DrawingVisual() ;
using (DrawingContext context = visual.RenderOp en())
{
VisualBrush brush = new VisualBrush(tar get);
context.DrawRec tangle(brush,
null,
new Rect(new Point(), bounds.Size));
}
renderBitmap.Re nder(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.


RenderTargetBit map rtb = new RenderTargetBit map(200, 200, 96, 96,
PixelFormats.Pb gra32);

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

rtb.Render(cir) ;

PngBitmapEncode r png = new PngBitmapEncode r();
png.Frames.Add( BitmapFrame.Cre ate(rtb));
using (Stream fs= File.Create("te st.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
1941
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 backrground...
0
330
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 border) My main form uses a MyDataGrid. instance as the folowing code from MyDataGrid.cs public class MyDataGrid : DataGrid
3
4268
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
3
10330
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 shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
8
1953
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
4126
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 circle in PHP? Actually for the circle I am using the imageellipse() function and it works. How do I get the description inside the circle, i.e, Circle A, Circle B, etc? Thanks heaps,
5
9244
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 Ellipse Drawing Function. The following code I found on Google works in VB5 and draws an ellipse shape no problem using the Circle Method. Private Sub cmdDrawEllipse_Click() Dim X As Long, Y As Long Dim ElipseWidth As Integer, ElipseHeight As...
0
435
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 RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
3
10439
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
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.