02-10-12, 03:13 PM
والدالة هنا الهدف منها هو إدخال صورة ما وأيضا لون Color ما وهذا اللون سيتيح لنا أن نجعل GraphicsPath يتم تلوينه باللون الذي نختاره ثم نسترجع هذه الصورة ونستخدمها كيفما شئنا بعد ذلك ولقد أطلقت علي الدالة اسم DrawRoundEdgesBitmap
كود :
Public Shared Function DrawRoundEdgesBitmap(ByVal SourceBitmap As Bitmap, ByVal color As Color) As Bitmap
Dim bmp As New Bitmap(SourceBitmap.Width, SourceBitmap.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath = CreateRoundedPath(bmp)
Dim Brush As New SolidBrush(color)
g.DrawImage(SourceBitmap, New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
g.FillPath(Brush, ImagePath)
Return bmp
End Function ' DrawRoundEdgesBitmap Functionافتح مشروع ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Public Shared Function CreateRoundedPath(ByVal SourceBitmap As Bitmap) As System.Drawing.Drawing2D.GraphicsPath
Dim ImagePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
Dim Radius As Double = 0.05
Dim Corner As Integer = CInt(SourceBitmap.Width * Radius)
Dim Border As Integer = 1
Dim Width As Integer = SourceBitmap.Width - 1
Dim Height As Integer = SourceBitmap.Height - 1
ImagePath.AddRectangle(New Rectangle(-Border, -Border, SourceBitmap.Width + Border, SourceBitmap.Height + Border))
ImagePath.AddArc(New Rectangle(0, 0, Corner, Corner), 180, 90)
ImagePath.AddLine(Corner, 0, Width - Corner, 0)
ImagePath.AddArc(New Rectangle(Width - Corner, 0, Corner, Corner), -90, 90)
ImagePath.AddLine(Width, Corner, Width, SourceBitmap.Height - Corner)
ImagePath.AddArc(New Rectangle(Width - Corner, Height - Corner, Corner, Corner), 0, 90)
ImagePath.AddLine(Width - Corner, Height, Corner, Height)
ImagePath.AddArc(New Rectangle(0, Height - Corner, Corner, Corner), 90, 90)
ImagePath.AddLine(0, Height - Corner, 0, Corner)
ImagePath.CloseFigure()
Return ImagePath
End Function ' CreateRoundedPath Function
Public Shared Function DrawRoundEdgesBitmap(ByVal SourceBitmap As Bitmap, ByVal color As Color) As Bitmap
Dim bmp As New Bitmap(SourceBitmap.Width, SourceBitmap.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath = CreateRoundedPath(bmp)
Dim Brush As New SolidBrush(color)
g.DrawImage(SourceBitmap, New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
g.FillPath(Brush, ImagePath)
Return bmp
End Function ' DrawRoundEdgesBitmap Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bmp As Bitmap = New Bitmap(My.Resources.pre_en_cats_large)
Dim bmp1 As Bitmap = DrawRoundEdgesBitmap(bmp, PictureBox1.BackColor)
PictureBox1.Image = bmp1
End Sub
End Classولنوضح ذلك سنقوم بتطوير احدي الدوال قليلا ولنأخذ الدالة CreateRoundedPath ونقوم بالتعديل بها وسنحتفظ لها بنفس الإسم ولكن المدخلات ستتغير قليلا وبالتالي ستكون هذه الدالة Overloaded والمقصود هنا بهذه الكلمة وهي Overloaded أن هناك دالتان يشتركان في نفس الاسم ولكن استخدام كل منهما يختلف عن الأخري من حيث المدخلات
والدالة سيكون شكلها كالتالي وهنا نحن نسمح للمستخدم بتحديد كيف سيبدو Curve عن طريق إدخال قيمة لنصف قطر هذا Curve ثم وضعنا شرطا بحيث لو أن القيمة تقل أو تزيد عن قيمة معين فإنه يحدث خطأ عند تنفيذ الدالة
كود :
Public Shared Function CreateRoundedPath(ByVal SourceBitmap As Bitmap, ByVal Raduis As Double) As System.Drawing.Drawing2D.GraphicsPath
If Raduis < 0.005 OrElse Raduis > 1 Then
Throw New ArgumentOutOfRangeException("Raduis must be between 0.005 and 1.")
End If
Dim ImagePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
Dim r As Double = Raduis
Dim Corner As Integer = CInt(SourceBitmap.Width * r)
Dim Border As Integer = 1
Dim Width As Integer = SourceBitmap.Width - 1
Dim Height As Integer = SourceBitmap.Height - 1
ImagePath.AddRectangle(New Rectangle(-Border, -Border, SourceBitmap.Width + Border, SourceBitmap.Height + Border))
ImagePath.AddArc(New Rectangle(0, 0, Corner, Corner), 180, 90)
ImagePath.AddLine(Corner, 0, Width - Corner, 0)
ImagePath.AddArc(New Rectangle(Width - Corner, 0, Corner, Corner), -90, 90)
ImagePath.AddLine(Width, Corner, Width, SourceBitmap.Height - Corner)
ImagePath.AddArc(New Rectangle(Width - Corner, Height - Corner, Corner, Corner), 0, 90)
ImagePath.AddLine(Width - Corner, Height, Corner, Height)
ImagePath.AddArc(New Rectangle(0, Height - Corner, Corner, Corner), 90, 90)
ImagePath.AddLine(0, Height - Corner, 0, Corner)
ImagePath.CloseFigure()
Return ImagePath
End Function ' CreateRoundedPath Functionافتح مشروع ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Public Shared Function CreateRoundedPath(ByVal SourceBitmap As Bitmap, ByVal Raduis As Double) As System.Drawing.Drawing2D.GraphicsPath
If Raduis < 0.005 OrElse Raduis > 1 Then
Throw New ArgumentOutOfRangeException("Transparency must be between 0.005 and 1.")
End If
Dim ImagePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
Dim r As Double = Raduis
Dim Corner As Integer = CInt(SourceBitmap.Width * r)
Dim Border As Integer = 1
Dim Width As Integer = SourceBitmap.Width - 1
Dim Height As Integer = SourceBitmap.Height - 1
ImagePath.AddRectangle(New Rectangle(-Border, -Border, SourceBitmap.Width + Border, SourceBitmap.Height + Border))
ImagePath.AddArc(New Rectangle(0, 0, Corner, Corner), 180, 90)
ImagePath.AddLine(Corner, 0, Width - Corner, 0)
ImagePath.AddArc(New Rectangle(Width - Corner, 0, Corner, Corner), -90, 90)
ImagePath.AddLine(Width, Corner, Width, SourceBitmap.Height - Corner)
ImagePath.AddArc(New Rectangle(Width - Corner, Height - Corner, Corner, Corner), 0, 90)
ImagePath.AddLine(Width - Corner, Height, Corner, Height)
ImagePath.AddArc(New Rectangle(0, Height - Corner, Corner, Corner), 90, 90)
ImagePath.AddLine(0, Height - Corner, 0, Corner)
ImagePath.CloseFigure()
Return ImagePath
End Function ' CreateRoundedPath Function
Public Shared Function CreateRoundedPath(ByVal SourceBitmap As Bitmap) As System.Drawing.Drawing2D.GraphicsPath
Dim ImagePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
Dim Radius As Double = 0.05
Dim Corner As Integer = CInt(SourceBitmap.Width * Radius)
Dim Border As Integer = 1
Dim Width As Integer = SourceBitmap.Width - 1
Dim Height As Integer = SourceBitmap.Height - 1
ImagePath.AddRectangle(New Rectangle(-Border, -Border, SourceBitmap.Width + Border, SourceBitmap.Height + Border))
ImagePath.AddArc(New Rectangle(0, 0, Corner, Corner), 180, 90)
ImagePath.AddLine(Corner, 0, Width - Corner, 0)
ImagePath.AddArc(New Rectangle(Width - Corner, 0, Corner, Corner), -90, 90)
ImagePath.AddLine(Width, Corner, Width, SourceBitmap.Height - Corner)
ImagePath.AddArc(New Rectangle(Width - Corner, Height - Corner, Corner, Corner), 0, 90)
ImagePath.AddLine(Width - Corner, Height, Corner, Height)
ImagePath.AddArc(New Rectangle(0, Height - Corner, Corner, Corner), 90, 90)
ImagePath.AddLine(0, Height - Corner, 0, Corner)
ImagePath.CloseFigure()
Return ImagePath
End Function ' CreateRoundedPath Function
Public Shared Function DrawRoundEdgesBitmap(ByVal SourceBitmap As Bitmap, ByVal color As Color) As Bitmap
Dim bmp As New Bitmap(SourceBitmap.Width, SourceBitmap.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath = CreateRoundedPath(bmp, 0.6)
Dim Brush As New SolidBrush(color)
g.DrawImage(SourceBitmap, New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
g.FillPath(Brush, ImagePath)
Return bmp
End Function ' DrawRoundEdgesBitmap Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bmp As Bitmap = New Bitmap(My.Resources.pre_en_cats_large)
Dim bmp1 As Bitmap = DrawRoundEdgesBitmap(bmp, PictureBox1.BackColor)
PictureBox1.Image = bmp1
End Sub
End Classأعتقد عزيزي القارئ أنك الأن تستطيع بناء بعض الدوال باستخدام نفس الأفكار وهذا هو الهدف من المقال بشكل خاص وكل ما عليك أن تفعله هو أن تتلاعب قليلا بشكل الدالة التي نسترجع منها GraphicsPath أو بشكل الدالة التي نسترجع منها الصورة عموما الأمر متروك لخيالك طبعا و بشكل عام وكما تري عزيزي القارئ أنه قد أصبح لدينا الأن مجموعة من الدوال توفر علينا قليلا من الجهد في كتابة وتكرار الكود
وقد يتساءل البعض وأين الكلاس العام الذي سوف نستخدمه للتعامل مع الصور في بيئة الدوت نت وهنا أقول لك عزيزي القارئ لا تتعجل فربما بعد الانتهاء من قراءة المقال كاملا تعيد بناء كل شئ بشكل يرضيك أنت فهدفي هنا من المقال هو توضيح الفكرة ولكن عليك أنت أن تبني الكلاس بالشكل الذي يرضيك ويفي باحتياجاتك وبالطبع سنبني الكلاس في أخر المقال لذا لا تقلق
الكود التالي يوضح أفكارا أخري لدوال إضافية وبنفس الأسلوب الذي اتبعناه لإنشاء الدوال السابقة الخاصة بإعطاء الصورة شكلا معينا
كود :
Public Shared Function CreateSemiCircularPath(ByVal SourceBitmap As Bitmap) As System.Drawing.Drawing2D.GraphicsPath
Dim ImagePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
ImagePath.AddRectangle(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Width))
ImagePath.AddEllipse(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Width))
Return ImagePath
End Function 'CreateSemiCircularPath Function
Public Shared Function DrawSemiCircularBitmap(ByVal SourceBitmap As Bitmap, ByVal color As Color) As Bitmap
Dim bmp As New Bitmap(SourceBitmap.Width, SourceBitmap.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath = CreateCircularPath(bmp)
Dim Brush As New SolidBrush(color)
g.DrawImage(SourceBitmap, New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
g.FillPath(Brush, ImagePath)
Return bmp
End Function ' DrawSemiCircularBitmap Functionبالتوفيق
أخوكم عمر
