Have you ever wanted to move a specific part of your drawing in GDI+ and had to redraw the whole scene again?
With this library, you'll be manipulating your drawings as if they were alive!
To be able to use this class, you only have to know a little bit about GDI+ in VB .NET or C#.
About Magic Graphics Project
This project was designed and developed by Abd Allah Diab; the project is designed to make drawing in .NET applications easier.
Moving shapes in a scene was very hard because you had to redraw the whole scene again with the shape in its new location, redrawing is a huge problem when you have to remember each shape's position and colors and so, rotating and scaling was also very hard.
Using this class, you can now easily move, rotate or scale your shapes by writing a single line, assume that
r is a rectangle:
r.Move(New X, New Y)
The Structure of Magic Graphics
The namespace consists of:
IShape (any shape Interface)
Shape (base class for any shape)
ShapeContainer (a class that holds the shapes in it so you can draw, move, rotate and scale without redrawing the whole scene)
- Ellipse, Rectangle and Lines (predefined shapes that you can use in your application)
Moves the shape to the given X,Y.
Angle: Single Float
Rotates the shape with the given angle in degrees.
Adds the given angle to the previous angle.
Dx, Dy: Single Float
Scales the shape with the given dx, dy.
Multiplies its height by dy, and its width by dx.
Resets the rotation of the shape to 0.
Resets the scale of the shape to its original scale.
Draws the shape in its location and its rotation and scale in its container.
Returns a string that describes the shape.
Sets the container of the shape so the shape will be drawin in it.
Gets or sets the center point of the shape that it will rotate at.
Gets the current location of the shape, and can be changed using Move method.
Gets or sets the height of the shape without scaling it.
Gets or sets the width of the shape without scaling it.
Gets the current rotation of the shape according to its original state in degrees.
Gets the current scale on X of the shape according to its original state.
Gets the current scale on Y of the shape according to its original state.
Gets the shape container of the shape.
Shape class implements the
IShape interface, but it has a
Dispose method that accepts a Boolean value as a parameter that indicates whether to remove the shape from the container or not.
Width, Height: Integer
Constructor that builds a new instance of ShapeContainer that will draw in Graphics using the Width, Height and BackgroundColor to draw, then will put the whole drawing in vBMP if was passed, if vBMP is nothing then the class will draw in its own Bitmap.
Adds the passed shape to the container.
Clears the ShapeContainer from shapes.
Draws the result in the Graphics object that was passed in the constructor.
Removes the shape at the passed Index or removes the shape passed to it if it was found in it.
Redraws the whole scene again and saves it in the bitmap, if Flush is True then the bitmap will be drawn in the Graphics object.
If true then the Render method will always have Flush set to True.
Gets the graphics object that was passed to the constructor.
Gets the shape at a specified index.
Gets the Bitmap object that the scene will be rendered in.
Gets the shapes list in this container.
Implementing Magic Graphics in your Application
Here's an example that shows how to draw an Ellipse and a Rectangle using Magic Graphics class:
Public Class FormMain
Dim SC As MagicGraphics.ShapeContainer
Private Sub FormMain_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SC = New MagicGraphics.ShapeContainer(PictureBox1.CreateGraphics, _
PictureBox1.Width, PictureBox1.Height, Color.Black)
PictureBox1.Image = SC.BMP
SC.AutoFlush = False
Private Sub ButtonRectangle_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim Sq As New MagicGraphics.Rectangle(New Pen(Color.Black, 3), _
Brushes.Aqua, 60, 20, 50, 50)
Sq.FillingBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), _
New Point(60, 0), Color.Yellow, Color.Red)
Private Sub ButtonEllipse_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim El As New MagicGraphics.Ellipse(New Pen(Color.Black, 3), _
Brushes.Olive, 60, 88, 50, 71)
El.FillingBrush = New Drawing2D.LinearGradientBrush(New Point(0, 0), _
New Point(30, 0), Color.Red , Color.SteelBlue)
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
If SC.ShapesL.Count = 0 Then Exit Sub
For Each shp As MagicGraphics.Shape In SC.ShapesL
shp.Move(shp.Location.X + 1, shp.Location.Y + 1)
The result must be something that looks like the picture above.
This class lets you design your own shapes by inheriting the base class
Shape. You must write the
Render sub (
void) and the
ToString function. You can add properties, events and other things to your shapes and have a great library of your own shapes.
This class, as you've seen, is very easy to implement and use, and it really saves a lot of time in redrawing.
Have fun and happy drawing.