Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Sat, 27 Oct 2007 02:43:14 -0700,    group: microsoft.public.windows.developer.winfx.avalon        back       


Draw a circle in WPF   
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
date: Sat, 27 Oct 2007 02:43:14 -0700   author:   Tem

Re: Draw a circle in WPF   
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]
          - mvp@spam.guard.caspershouse.com

"Tem"  wrote in message 
news:eeG0X3HGIHA.3980@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
date: Sat, 27 Oct 2007 10:19:36 -0400   author:   Nicholas Paldino [.NET/C# MVP]

Re: Draw a circle in WPF   
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]"  wrote in 
message news:EF26B6C7-FDF2-4E3A-85F4-4D9C608A85CE@microsoft.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]
>          - mvp@spam.guard.caspershouse.com
>
> "Tem"  wrote in message 
> news:eeG0X3HGIHA.3980@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
>
date: Sat, 27 Oct 2007 14:15:27 -0700   author:   Tem

Re: Draw a circle in WPF   
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]
          - mvp@spam.guard.caspershouse.com

"Tem"  wrote in message 
news:uEKSP6NGIHA.5360@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]"  wrote 
> in message news:EF26B6C7-FDF2-4E3A-85F4-4D9C608A85CE@microsoft.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]
>>          - mvp@spam.guard.caspershouse.com
>>
>> "Tem"  wrote in message 
>> news:eeG0X3HGIHA.3980@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
>>
>
date: Sat, 27 Oct 2007 19:19:06 -0400   author:   Nicholas Paldino [.NET/C# MVP]

Re: Draw a circle in WPF   
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.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
date: Sun, 28 Oct 2007 01:56:59 +0200   author:   Laurent Bugnion, MVP

Re: Draw a circle in WPF   
Thank you for the example, there's one thing

what should i replace this.RootImage with?


Tem

"Laurent Bugnion, MVP"  wrote in message 
news:O$8YRUPGIHA.936@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.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
date: Sun, 28 Oct 2007 21:08:25 -0700   author:   Tem

Re: Draw a circle in WPF   
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"  wrote in message 
> news:O$8YRUPGIHA.936@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.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 
> 

-- 
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
date: Mon, 29 Oct 2007 13:19:55 +0100   author:   Laurent Bugnion, MVP

Re: Draw a circle in WPF   
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);
}
date: Mon, 29 Oct 2007 12:31:35 -0700   author:   Tem

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us