Sometimes you may wish to use GDI+ to draw on an ImageGlue Canvas. This allows you to take advantage of the sophisticated and comprehensive image manipulation available in ImageGlue and combine it with the powerful drawing commands available in GDI+.

 

   
1
 
Drawing our Image    
     

We create our ImageGlue canvas and draw our image onto it.

[Visual Basic]
Dim ca As New Canvas
ca.DrawFile(Server.MapPath("rez/birds.jpg"), "")

[C#]
Canvas ca = new Canvas();
ca.DrawFile(Server.MapPath("rez/birds.jpg"), "");

 

   
2
 
Getting a GDI+ Wrapper    
     

To draw on our canvas using GDI+ we'll need to wrap it in a GDI+ Graphics context. Note that while the Canvas is wrapped we won't be able to use any ImageGlue Canvas methods.

[Visual Basic]
Dim gr As Graphics
Dim wr As New CanvasWrapper()
gr = wr.Wrap(ca)

[C#]
CanvasWrapper wr = new CanvasWrapper();
Graphics gr = wr.Wrap(ref ca);

 

   
3
 
Drawing using GDI+    
     

Draw a circle and a pie using GDI+ drawing commands.

[Visual Basic]
Dim pn As New Pen(Color.DarkGoldenrod, 8)
gr.SmoothingMode = SmoothingMode.AntiAlias
gr.DrawEllipse(pn, 10, 25, 125, 125)
pn.Color = Color.SteelBlue
gr.DrawPie(pn, 10, 25, 125, 125, 45, 120)

[C#]
Pen pn = new Pen(Color.DarkGoldenrod, 8);
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.DrawEllipse(pn, 10, 25, 125, 125);
pn.Color = Color.SteelBlue;
gr.DrawPie(pn, 10, 25, 125, 125, 45, 120);

 

   
4
 
Unwrapping    
     

To export we'll need our Canvas back again which means we'll need to unwrap it. After it's unwrapped we can use the Canvas SaveAs method.

[Visual Basic]
ca = wr.Unwrap(gr)
ca.SaveAs(Server.MapPath("drawgdi.jpg"), "")

[C#]
ca = wr.Unwrap(ref gr);
ca.SaveAs(Server.MapPath("drawgdi.jpg"), "");

 

   
5
 
Tidying Up    
     

Finally we release any memory being used by our objects.

[Visual Basic]
ca.Clear()

[C#]
ca.Clear();

 

   
6
 
Sample Output    
     

A sample output image is shown below.


drawgdi.jpg