02-10-12, 03:29 PM
كاتب الموضوع : silverlight
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم
مقدمة
التعامل مع الصور في الدوت نت شئ هام جدا لأنه غالبا ما نحتاج إلي إضافة الصور الي برامجنا بشكل ما فأنت إن حاولت أن تصف شخص ما علي سبيل المثال فستحتاج الي كتابة الكثير من الكلمات لتوضح كيف يبدو هذا الشخص لكنك لو قمت بعرض صورته مباشرة فستوفر علي نفسك الكثير من الجهد والوقت لذلك فإن المقولة الشهيرة أن الصورة تساوي ألف كلمة صحيحة مائة بالمائة.
في واقع الأمر الهدف من هذا المقال هو ليس فقط أن نتعلم كيفية التعامل مع الصور و هنا كلمة التعامل مع الصور في بيئة الدوت النت لن تكون قاصرة علي كيفية عرض الصور في الفورم فقط بل أيضا الهدف منها هو أن نتعلم كيفية التعديل بهذه الصور كما نريد عن طرق إضافة مؤثرات خاصة لهذه الصور وذلك باستخدام +GDI
وبشكل عام فإن الهدف النهائي أو الهدف الأكبر من المقال سيكون عبارة عن بناء كلاس و سوف نقوم بتحويل هذا الكلاس الي ملف DLL حيث يمكن أن نستخدمه داخل أي برنامج نصممه وبالتالي يمكننا عندها أن نتعامل مع الصور بأي شكل نريده بدون الحاجة الي كتابة الكود أو تكرار الكود أكثر من مرة وأيضا نجعل استخدام الكود أكثر سهولة وأكثر متعة وأكثر كفاءة بشكل عام
عموما سوف نناقش كيفية التعامل مع الصور وعند الانتهاء من كل جزء في المقال سنحاول أن نخرج بدالة Function ثم نقوم بتجميع كل هذه الدوال في نهاية المقال لنبني بها الكلاس الذي من الممكن أن نستخدمه في التعامل مع الصور ثم نحوله الي ملف DLL ثم نناقش كيفية استخدامه داخل أي برنامج نريده ولنتحدث قليلا عن كيفية بناء الصورة
يوجد في الدوت نت كلاس اسمه Bitmap Class وهو كلاس معروف للجميع هذا الكلاس يسمح لنا بتعريف و بناء أي صورة نريدها وذلك عن طريق تعريف متغير قيمته تساوي Bitmap حيث يمكننا هنا إما أن نساوي قيمة هذا المتغير بصورة ما علي جهاز الكمبيوتر لدينا أو يمكننا أن رسم صورة بالكامل من الصفر أو أن بناء صورة جديدة من صورة أو أكثر ثم نرسم ما نريد بداخلها ثم نستخدم هذه الصورة الجديدة كما نريد
مجموعة الأكواد التالية توضح بعض الأفكار لكيفية تعريف وبناء الصورة
كود :
Dim bmp As Bitmap
Dim bmp1 As Bitmap = Nothing
Dim bmp2 As New Bitmap("C:\myImage.bmp")
Dim bmp3 As Bitmap = New Bitmap("C:\myImage.bmp", True)
Dim bmp4 As Bitmap = New Bitmap("C:\myImage.bmp", False)
Dim bmp5 As Bitmap = New Bitmap("C:\myImage.bmp")
Dim bmp6 As New Bitmap(My.Resources.blender)
Dim bmp7 As New Bitmap(PictureBox1.image)
Dim bmp8 As New Bitmap(Form2.BackgroundImage)
Dim bmp9 As New Bitmap(Me.BackgroundImage)
Dim bmp10 As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim bmp11 As New Bitmap(100, 100)
ولو قمت عزيزي القارئ باستخدام الكلاسان معا أي Graphics Class مع Bitmap Class يمكنك حينها أن ترسم ما تريد
والكود التالي يوضح كيفية تعريف Graphics Class من صورة ما
كود :
Dim g As Graphics = Graphics.FromImage(bmp)
ولتنفيذ المثال افتح مشروع وأضف الي الفورم 6 PictureBox ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim img As New Bitmap(My.Resources.blender)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateImage()
CreateImage1()
CreateImage2()
CreateImage3()
CreateImage4()
CreateImage5()
End Sub
Private Sub CreateImage()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(Brushes.Black, rect)
Dim lgb As New Drawing2D.LinearGradientBrush(rect, Color.Blue, Color.BlueViolet, Drawing2D.LinearGradientMode.Vertical, True)
Dim rect1 As New Rectangle(rect.X + 4, rect.Y + 4, rect.Width - 8, rect.Height - 8)
g.FillRectangle(lgb, rect1)
Dim rect2 As New Rectangle(rect1.X + 8, rect1.Y + 8, rect1.Width - 16, rect1.Height - 16)
Dim lgb1 As New Drawing2D.LinearGradientBrush(rect1, Color.Lime, Color.Green, Drawing2D.LinearGradientMode.Vertical, True)
g.FillRectangle(lgb1, rect2)
Dim rect3 As New Rectangle(rect2.X + 8, rect2.Y + 8, rect2.Width - 16, rect2.Height - 16)
rect3.Inflate(-1, -1)
g.FillRectangle(New SolidBrush(Color.FromArgb(25, Color.Black)), rect3)
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub CreateImage1()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(Brushes.Black, rect)
Dim lgb As New Drawing2D.LinearGradientBrush(rect, Color.Blue, Color.BlueViolet, Drawing2D.LinearGradientMode.Vertical, True)
Dim rect1 As New Rectangle(rect.X + 4, rect.Y + 4, rect.Width - 8, rect.Height - 8)
g.FillRectangle(lgb, rect1)
Dim rect2 As New Rectangle(rect1.X + 8, rect1.Y + 8, rect1.Width - 16, rect1.Height - 16)
Dim lgb1 As New Drawing2D.LinearGradientBrush(rect1, Color.Lime, Color.Green, Drawing2D.LinearGradientMode.Vertical, True)
g.FillRectangle(lgb1, rect2)
Dim rect3 As New Rectangle(rect2.X + 8, rect2.Y + 8, rect2.Width - 16, rect2.Height - 16)
rect3.Inflate(-1, -1)
g.FillRectangle(New SolidBrush(Color.FromArgb(25, Color.Black)), rect3)
Dim clr As Color = img.GetPixel(img.Width / 2, img.Height / 2)
Dim sb As New SolidBrush(Color.FromArgb(75, Color.Navy))
g.FillRectangle(sb, New Rectangle(rect3.X + 8, rect3.X + 8, rect3.Width - 16, rect3.Height - 16))
g.DrawImage(img, New Rectangle(rect3.X + 8, rect3.X + 8, rect3.Width - 16, rect3.Height - 16))
g.FillEllipse(sb, New Rectangle(rect3.X + 16, rect3.X + 16, rect3.Width - 32, rect3.Height - 32))
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
PictureBox2.Image = bmp
PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub CreateImage2()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(New SolidBrush(Color.FromArgb(150, Color.Black)), rect)
For x As Integer = 0 To bmp.Width - 1 Step 10
For y As Integer = 0 To bmp.Height - 1 Step 10
Dim rect1 As New Rectangle(rect.X + x, rect.Y + y, rect.Width - 2 * x, rect.Height - 2 * y)
Dim clr As Color = Color.DarkOrange
Dim Clr1 As Color = Color.FromArgb(x, clr)
g.DrawImage(img, New Rectangle(rect.X + 8, rect.X + 8, rect.Width - 16, rect.Height - 16))
g.FillRectangle(New SolidBrush(Clr1), rect1)
g.DrawRectangle(New Pen(Color.FromArgb(100, Color.Red), 1), rect1)
Next
Next
g.FillRectangle(New SolidBrush(Color.FromArgb(50, Color.Lime)), New Rectangle(rect.X + 10, rect.Y + 10, rect.Width - 20, rect.Height - 20))
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
g.FillRectangle(New SolidBrush(Color.FromArgb(100, Color.Gold)), rect)
PictureBox3.Image = bmp
PictureBox3.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub CreateImage3()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(New SolidBrush(Color.FromArgb(150, Color.Black)), rect)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim rect1 As New Rectangle(rect.X + x, rect.Y + y, rect.Width - 2 * x, rect.Height - 2 * y)
Dim clr As Color = Color.Chocolate
Dim Clr1 As Color = Color.FromArgb(x, clr)
g.DrawImage(img, New Rectangle(rect.X + 50, rect.X + 50, rect.Width - 100, rect.Height - 100))
g.FillRectangle(New SolidBrush(Clr1), rect1)
g.DrawRectangle(New Pen(Color.FromArgb(100, Color.Maroon)), rect1)
Next
Next
g.FillRectangle(New SolidBrush(Color.FromArgb(50, Color.Orange)), New Rectangle(rect.X + 10, rect.Y + 10, rect.Width - 20, rect.Height - 20))
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
PictureBox4.Image = bmp
PictureBox4.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub CreateImage4()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(New SolidBrush(Color.FromArgb(20, Color.Black)), rect)
Dim textures As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.AliceBlue, Color.Tan)
g.FillRectangle(textures, New Rectangle(rect.X + 50, rect.X + 50, rect.Width - 100, rect.Height - 100))
g.DrawImage(img, New Rectangle(rect.X + 50, rect.X + 50, rect.Width - 100, rect.Height - 100))
g.FillRectangle(New Drawing2D.LinearGradientBrush(New Rectangle(rect.X + 10, rect.Y + 10, rect.Width - 20, rect.Height - 20), Color.FromArgb(250, Color.Turquoise), Color.FromArgb(150, Color.Blue), 45, False), New Rectangle(rect.X + 10, rect.Y + 10, rect.Width - 20, rect.Height - 20))
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
PictureBox5.Image = bmp
PictureBox5.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub CreateImage5()
Dim bmp As New Bitmap(150, 150, Imaging.PixelFormat.Format32bppPArgb)
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim textures As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.SmallGrid, Color.Red, Color.FromArgb(50, Color.Red))
g.FillRectangle(textures, rect)
g.DrawImage(img, rect)
g.DrawString("Photo", New Font("Times New Roman", 14), Brushes.White, 0, 0)
PictureBox6.Image = bmp
PictureBox6.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
End Class
بالتوفيق
أخوكم عمر