Magic Graphics






4.39/5 (15投票s)
一个让您轻松操作绘图的库。

引言
您是否曾经想过在 GDI+ 中移动绘图的特定部分,却不得不重新绘制整个场景?
有了这个库,您将可以像操作“活动”的绘图一样操作您的绘图!
背景
要使用这个类,您只需要对 VB .NET 或 C# 中的 GDI+ 有一点了解。
关于 Magic Graphics 项目
这个项目由 Abd Allah Diab 设计和开发;该项目旨在简化 .NET 应用程序中的绘图。
在场景中移动图形非常困难,因为您必须重新绘制整个场景,并在图形的新位置上重绘,重新绘制是一个巨大的问题,因为您需要记住每个图形的位置和颜色,因此,旋转和缩放也变得非常困难。
使用这个类,您现在可以轻松地移动、旋转或缩放您的图形,只需写一行代码即可,假设 r
是一个矩形
r.Move(New X, New Y)
Magic Graphics 的结构
该命名空间包含
IShape
(任何形状的接口)Shape
(任何形状的基类)ShapeContainer
(一个容纳图形的类,这样您就可以在不重新绘制整个场景的情况下进行绘制、移动、旋转和缩放)- Ellipse, Rectangle 和 Lines (您可以在应用程序中使用的预定义形状)
IShape 结构
a. 方法
名称 |
参数 |
描述 |
移动 |
X,Y: Integer |
将形状移动到给定的 X,Y 位置。 |
旋转 |
Angle: Single Float |
以给定的角度(以度为单位)旋转形状。 将给定角度添加到之前的角度。 |
Scale |
Dx, Dy: Single Float |
以给定的 dx, dy 缩放形状。 将其高度乘以 dy,宽度乘以 dx。 |
ResetRotation |
- |
将形状的旋转重置为 0。 |
ResetScale |
- |
将形状的缩放重置为其原始缩放。 |
Render |
- |
在容器中绘制形状,并考虑其位置、旋转和缩放。 |
ToString |
- |
返回一个描述形状的字符串。 |
SetContainer |
Container:ShapeContainer |
设置形状的容器,以便形状在该容器中绘制。 |
b. 属性
名称 |
只读 |
类型 |
描述 |
中心 |
否 |
System.Drawing.Point |
获取或设置形状的中心点,形状将围绕该点旋转。 |
Location |
是 |
System.Drawing.Point |
获取形状的当前位置,可以通过 Move 方法更改。 |
高度 |
否 |
整数 |
获取或设置形状的高度,不考虑缩放。 |
宽度 |
否 |
整数 |
获取或设置形状的宽度,不考虑缩放。 |
旋转 |
是 |
Single Float |
获取形状相对于其原始状态的当前旋转角度(以度为单位)。 |
ScaleX |
是 |
Single Float |
获取形状相对于其原始状态的当前 X 轴缩放。 |
ScaleY |
是 |
Single Float |
获取形状相对于其原始状态的当前 Y 轴缩放。 |
容器 |
是 |
ShapeContainer |
获取形状的形状容器。 |
Shape 结构
Shape
类实现了 IShape
接口,但它有一个 Dispose
方法,该方法接受一个布尔值作为参数,指示是否从容器中移除该形状。
ShapeContainer 结构
a. 方法
名称 |
参数 |
描述 |
新建 |
Graphics: System.Drawing.Graphics Width, Height: Integer BackgroundColor: System.Drawing.Color vBMP(可选): System.Drawing.Bitmap
|
构造函数,用于构建一个新的 ShapeContainer 实例,该实例将使用 Graphics、Width、Height 和 BackgroundColor 进行绘制,然后如果传递了 vBMP,则将整个绘图放入 vBMP 中。如果 vBMP 为 null,则该类将在其自己的 Bitmap 中进行绘制。 |
AddShape |
Shp: Shape |
将传入的形状添加到容器中。 |
Clear |
- |
清除 ShapeContainer 中的形状。 |
Flush |
- |
将结果绘制到构造函数中传递的 Graphics 对象上。 |
RemoveShape |
Shp: Shape 或者 Index: Integer |
移除指定索引处的形状,或者移除在容器中找到的传入形状。 |
Render |
Flush(可选): Boolean |
重新绘制整个场景并将其保存在 bitmap 中。如果 Flush 为 True,则 bitmap 将绘制到 Graphics 对象上。 |
b. 属性
名称 |
类型 |
描述 |
AutoFlush |
布尔值 |
如果为 true,则 Render 方法将始终将 Flush 设置为 True。 |
图形 |
System.Drawing.Graphics (只读) |
获取传递给构造函数的 graphics 对象。 |
形状 |
Shape(只读, 默认) |
获取指定索引处的形状。 |
c. 字段
名称 |
类型 |
描述 |
BMP |
System.Drawing.Bitmap |
获取场景将渲染到的 Bitmap 对象。 |
ShapesL |
List(Of Shape) |
获取此容器中的形状列表。 |
在您的应用程序中实现 Magic Graphics
这是一个展示如何使用 Magic Graphics 类绘制 Ellipse 和 Rectangle 的示例
Imports System.Drawing
Imports System.Drawing.Drawing2D
' Create a Form and put a PictureBox on it,
' and put a Button to draw an ellipse and another to draw a rectangle.
' Now let's add a Timer to move and rotate, Timer1 (Interval = 100; Enabled = True).
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
End Sub
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)
SC.AddShape(Sq)
'From here you can easily manipulate this Rectangle by refering to Sq.
End Sub
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)
SC.AddShape(El)
'From here you can easily manipulate this Ellipse by refering to El.
End Sub
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.Rotate(10) 'Rotates every shape in this container by 10 degrees.
shp.Move(shp.Location.X + 1, shp.Location.Y + 1) 'Offsets every shape
' in this container by 1 pixel on X axis, and 1 pixel on Y axis.
Next
End Sub
End Class
结果应该看起来像上面的图片。
自定义形状
这个类允许您通过继承基类 Shape
来设计自己的形状。您必须编写 Render sub
(void
) 和 ToString
函数。您可以为您的形状添加属性、事件和其他内容,并拥有一个很棒的自定义形状库。
摘要
正如您所见,这个类非常易于实现和使用,并且它确实节省了很多重新绘制的时间。
祝您玩得开心,绘图愉快。