تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - الجزء الثاني
#1
كاتب الموضوع : silverlight

التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI
اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم

الجزء الثاني: كيفي نجعل الصورة تأخذ شكلا معينا

من المؤكد أن الكثيرين منا يريدون أن يغيروا شكل الصورة بأن يجعلوها تأخذ أي شكل ما مثل الشكل البيضاوي أو يجعلون زوايا الصورة دائرية أو يجعلون الصورة تصبح نصف دائرية أو أي شكل أخر يريدون. إذن كيف نفعل ذلك؟
في واقع الأمر أن الفكرة وببساطة شديدة يمكن تلخيصها كالتالي
نحدد الصورة ثم نستخدم GraphicsPath Class ونربطه بالصورة ثم نرسم الصورة وأخيرا نرسم GraphicsPath
مثال علي ذلك
افتح مشروع جديد فقط ثم عليك إضافة صورة الي الفورم ثم في الحدث Paint الخاص بالفورم عليك تعريف New Bitmap ثم عليك تعريف GraphicsPath وليكن شكلا بيضاويا Ovalمثلا ثم تعيد رسم الصورة ثم ترسم عليها GraphicsPath والكود التالي يوضح ذلك


كود :
Public Class Form1

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

'Create Bitmap
Dim SourceBitmap As New Bitmap(My.Resources.pre_en_cats_large)

Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath.StartFigure()
ImagePath.AddRectangle(New Rectangle(0, 0, SourceBitmap.Width + 1, SourceBitmap.Height + 1))
ImagePath.AddEllipse(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
ImagePath.CloseFigure()
e.Graphics.DrawImageUnscaled(SourceBitmap, 0, 0)
e.Graphics.FillPath(Brushes.Red, ImagePath)

End Sub

End Class
عليك أن تلاحظ شيئا عزيزي القارئ في هذه الكود أن GraphicsPath ثم رسمه بلون أحمر
لكن كيف نجعل هذا اللون الأحمر يختفي عند رسم الصورة في الفورم. لنجرب هذا الكود ونري الفارق


كود :
Public Class Form1

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

'Create Bitmap
Dim SourceBitmap As New Bitmap(My.Resources.pre_en_cats_large)

Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()
ImagePath.StartFigure()
ImagePath.AddRectangle(New Rectangle(0, 0, SourceBitmap.Width + 1, SourceBitmap.Height + 1))
ImagePath.AddEllipse(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))
ImagePath.CloseFigure()
Dim brush As New SolidBrush(Me.BackColor)
e.Graphics.DrawImageUnscaled(SourceBitmap, 0, 0)
e.Graphics.FillPath(brush, ImagePath)

End Sub

End Class
ستلاحظ عزيزي القارئ في الكود أعلاه أن اللون الأحمر المستخدم في رسم GraphicsPath قد اختفي والسبب أننا استخدمنا SolidBrush جديدة واستخدمنا هنا لون الفورم لتعريفها ثم استخدمنا هذه Brush لرسم GraphicsPath
في المثال السابق قمنا برسم الصورة علي هيئة شكل بيضاوي ماذا لو أرنا أن نجعل الصورة تظهر بشكل أخر
مثال أخر علي ذلك و هنا سوف نرسم الصورة بحيث نجعل الزوايا الأربعة لها تأخذ شكل القوس


كود :
Public Class Form1

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

'Create Bitmap
Dim SourceBitmap As New Bitmap(My.Resources.pre_en_cats_large)

Dim ImagePath As New System.Drawing.Drawing2D.GraphicsPath()

Dim Radius As Double = 0.05
Dim Corner As Integer = CInt(SourceBitmap.Width * Radius)
Const 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()

Dim brush As New SolidBrush(Me.BackColor)
e.Graphics.DrawImageUnscaled(SourceBitmap, 0, 0)
e.Graphics.FillPath(brush, ImagePath)

End Sub

End Class
الأن لنبني مجموعة من الدوال Functions من الكود أعلاه بحيث يمكننا أن نستخدمها في أي وقت نشاء
ولنفكر قليلا هل نحن نحتاج الي بناء دالة واحدة فقط أم نريد أن نبني الكثير من الدوال من الكود اعلاه.
من المؤكد أننا نريد أن يكون لدينا أكثر من دالة Function لذلك سنقوم بتقسيم الكود اعلاه الي دالتان حيث ستختص الدالة الأولي بتعريف GraphicsPath والدالة الثانية ستختص باسترجاع الصورة ومن هاتان الدالتان يمكننا بعد ذلك أن نبني أكثر من دالة.
الدالة الخاصة باسترجاع GraphicsPath ستكون كالتالي
هنا سوف نقوم باسترجاع الشكل البيضاوي والكود التالي يوضح شكل الدالة ولقد أطلقت عليها اسم CreateOvalPath Function وبما أننا نريد استرجاع GraphicsPath الخاص بالصورة لذا تم ربط هذا GraphicsPath بالصورة. أي أننا سنقوم بإدخال صورة الي Function ولكن نسترجع منها GraphicsPath


كود :
Public Shared Function CreateOvalPath(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.Height))
ImagePath.AddEllipse(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))

Return ImagePath

End Function 'CreateOvalPath Function
الدالة الخاصة باسترجاع الصورة علي شكل بيضاوي ستكون كالتالي
والدالة هنا الهدف منها هو إدخال صورة ما وأيضا لون Color ما وهذا اللون سيتيح لنا أن نجعل GraphicsPath يتم تلوينه باللون الذي نختاره ثم نسترجع هذه الصورة ونستخدمها كيفما شئنا بعد ذلك ولقد أطلقت علي الدالة اسم DrawOvalBitmap


كود :
Public Shared Function DrawOvalBitmap(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 = CreateOvalPath(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 ' DrawOvalBitmap Function
الأن لنعطي مثالا يوضح كيفية الاستفادة من هاتان الدالتان اعلاه
افتح مشروع ثم أضف الي الفورم PictureBox ثم اكتب الكود بالشكل التالي


كود :
Public Class Form1

Public Shared Function CreateOvalPath(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.Height))
ImagePath.AddEllipse(New Rectangle(0, 0, SourceBitmap.Width, SourceBitmap.Height))

Return ImagePath

End Function 'CreateOvalPath Function

Public Shared Function DrawOvalBitmap(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 = CreateOvalPath(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 ' DrawOvalBitmap 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 = DrawOvalBitmap(bmp, PictureBox1.BackColor)
PictureBox1.Image = bmp1
End Sub
End Class
الأن لنبني دوال أخري من نفس الكود
وهذه المرة سنبني دالتان بحيث نستخدمهما لكي نجعل زوايا الصورة تبدو علي شكل Curve وذلك باستخدام نفس الأسلوب الذي تحدثنا عنه سابقا وهو كيفية بناء دوال لرسم الصورة علي شكل بيضاوي إذن نحن في حاجة لبناء دالتان أيضا
الدالة الخاصة باسترجاع GraphicsPath ستكون كالتالي
هنا سوف نقوم باسترجاع مستطيل له زوايا مقوسة والكود التالي يوضح شكل الدالة ولقد أطلقت عليها اسم 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
}}}
تم الشكر بواسطة:
#2

الدالة الخاصة باسترجاع الصورة علي شكل صورة جوانبها علي شكل Curve ستكون كالتالي
والدالة هنا الهدف منها هو إدخال صورة ما وأيضا لون 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
لقد تركت الدالتان اللتان لهما نفس الاسم لكي تري الفارق بينهما في الكود اعلاه ومن داخل الدالة DrawRoundEdgBitmap قمت باستخدام الدالة الجديدة وستلاحظ هنا أنني قد استخدمت القيمة 0.6 لتحديد قيمة نصف القطر المطلوب وعندما تحاول أن تجعل قيمة نصف القطر اكبر من 1 أو اقل من 0.005 سيحدث خطأ عند تنفيذ الكود لأنه من داخل الدالة قمنا باستخدام شرط ما وإن لم يتحقق هذا الشرط يحدث خطأ مباشرة أثناء Debugging للمشروع
أعتقد عزيزي القارئ أنك الأن تستطيع بناء بعض الدوال باستخدام نفس الأفكار وهذا هو الهدف من المقال بشكل خاص وكل ما عليك أن تفعله هو أن تتلاعب قليلا بشكل الدالة التي نسترجع منها 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

بالتوفيق
أخوكم عمر
}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  نظرة على DropBox SDK و التعامل معها +برنامج رفع و تحميل ملفات ابو ليلى 4 4,871 08-09-22, 11:54 AM
آخر رد: saif2023
  الجزء الثالث من:كيف تجعل الـ Text Box ذكي!يترجم العمليات الحسابية ويخرج الناتج (الأقواس المتعددة) !! أنس محمود 10 7,825 19-07-22, 12:15 AM
آخر رد: StartLight4000
Video [درس فيديو] تقارير الكريستال ريبورت وتغيير مسار الصور أثناء التشغيل رمضان272 0 1,607 28-03-22, 03:18 AM
آخر رد: رمضان272
  شرح خوارزميات معالجة الصور (من دروس الاستاذ فوزي برزنجي) ناديه الشجيري 19 34,329 20-02-22, 02:13 PM
آخر رد: رضوان الجماعي
  التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - مقدمة RaggiTech 3 5,855 30-07-21, 05:14 PM
آخر رد: kebboud
Lightbulb [مقال] التعامل مع ملفات اوفيس من خلال مكتبة NPOI ابو ليلى 2 4,190 01-07-21, 11:42 AM
آخر رد: kebboud
Lightbulb [مقال] التعامل مع اختصارات الملفات Shortcuts Magic Sword 2 4,414 01-10-20, 11:36 AM
آخر رد: abomo3ath
  [مقال] دوال التعامل مع النصوص Strings - VB.NET ابو ليلى 10 25,056 15-04-19, 07:09 PM
آخر رد: alsouf
  Compare Images المقارنة بين الصور Abu Ehab 0 3,268 31-10-18, 04:27 PM
آخر رد: Abu Ehab
  اصنع محرر أكواد خاص بك باستخدام الأداة RichTextBox السكر المغرور 13 7,504 19-08-18, 09:27 AM
آخر رد: elgokr

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم