تقييم الموضوع :
  • 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
}}}
تم الشكر بواسطة:


الردود في هذا الموضوع
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - الجزء الثاني - بواسطة Raggi Tech - 02-10-12, 03:10 PM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  نظرة على DropBox SDK و التعامل معها +برنامج رفع و تحميل ملفات ابو ليلى 5 5,985 16-08-24, 04:39 PM
آخر رد: ackore
  الجزء الثالث من:كيف تجعل الـ Text Box ذكي!يترجم العمليات الحسابية ويخرج الناتج (الأقواس المتعددة) !! أنس محمود 10 8,404 19-07-22, 12:15 AM
آخر رد: StartLight4000
Video [درس فيديو] تقارير الكريستال ريبورت وتغيير مسار الصور أثناء التشغيل رمضان272 0 2,000 28-03-22, 03:18 AM
آخر رد: رمضان272
  شرح خوارزميات معالجة الصور (من دروس الاستاذ فوزي برزنجي) ناديه الشجيري 19 35,571 20-02-22, 02:13 PM
آخر رد: رضوان الجماعي
  التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - مقدمة RaggiTech 3 6,349 30-07-21, 05:14 PM
آخر رد: kebboud
Lightbulb [مقال] التعامل مع ملفات اوفيس من خلال مكتبة NPOI ابو ليلى 2 4,628 01-07-21, 11:42 AM
آخر رد: kebboud
Lightbulb [مقال] التعامل مع اختصارات الملفات Shortcuts Magic Sword 2 4,712 01-10-20, 11:36 AM
آخر رد: abomo3ath
  [مقال] دوال التعامل مع النصوص Strings - VB.NET ابو ليلى 10 27,175 15-04-19, 07:09 PM
آخر رد: alsouf
  Compare Images المقارنة بين الصور Abu Ehab 0 3,646 31-10-18, 04:27 PM
آخر رد: Abu Ehab
  اصنع محرر أكواد خاص بك باستخدام الأداة RichTextBox السكر المغرور 13 8,199 19-08-18, 09:27 AM
آخر رد: elgokr

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


يقوم بقرائة الموضوع: