02-10-12, 03:18 PM
كاتب الموضوع : silverlight
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم
الجزء الخامس: كيف نقوم بعمل Rotate للصورة
الهدف هنا هو إعادة رسم الصورة بحيث تعطي انطباع للمستخدم أن الصورة قد حدث لها دوران في اتجاه عقارب الساعة أو في عكس عقارب الساعة والفكرة مبنية علي أن Bitmap Class يوجد به بعض الطرق Methods وهي بالتحديد GetPixel وSetPixel وباستخدامهما معا نستطيع الحصول علي ألوان البكسل بالصورة أو نعيد ترتيب ألوان البكسل بالصورة وسنري كلما تقدمنا في قراءة االجزاء الخاصة بنفس الموضوع أهمية هاتان الطريقتان
إذن كيف نقوم بعمل Rotation للصورة؟ المثال التالي يوضح ذلك
افتح مشروع جديد أضف له PictureBox وعدد إثنان باتون حيث سنستخدم الباتون الأول في عمل دوران Rotation للصورة في اتجاه عقارب الساعة والثاني سنستخدمه لعمل Rotation أو دوران للصورة عكس عقارب الساعة ثم اكتب الكود بالشكل التالي
كود :
Public Class Form1
Dim bmp As Bitmap = New Bitmap(My.Resources.body1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' دوران للصورة في اتجاه عقارب الساعة
Dim OrigainalBitmap As New Bitmap(PictureBox1.Image)
Dim bmpWidth As Integer = OrigainalBitmap.Width
Dim bmpHeight As Integer = OrigainalBitmap.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth, OrigainalBitmap.PixelFormat)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(bmpHeight - Y - 1, X, OrigainalBitmap.GetPixel(X, Y))
Next Y
Next X
PictureBox1.Image = RotatedBitmap
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' دوران للصورة في عكس عقارب الساعة
Dim OrigainalBitmap As New Bitmap(PictureBox1.Image)
Dim bmpWidth As Integer = OrigainalBitmap.Width
Dim bmpHeight As Integer = OrigainalBitmap.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth, OrigainalBitmap.PixelFormat)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(Y, bmpWidth - X - 1, OrigainalBitmap.GetPixel(X, Y))
Next Y
Next X
PictureBox1.Image = RotatedBitmap
End Sub
End Class
افتح مشروع وأضف الي الفورم PictureBox و Timer حيث هنا سنجعل الصورة يحدث لها Rotation مع Timer
كود :
Public Class Form1
Dim bmp As Bitmap = New Bitmap(My.Resources.body1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = bmp
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
Timer1.Interval = 250
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim OrigainalBitmap As New Bitmap(PictureBox1.Image)
Dim bmpWidth As Integer = OrigainalBitmap.Width
Dim bmpHeight As Integer = OrigainalBitmap.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth, OrigainalBitmap.PixelFormat)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(Y, bmpWidth - X - 1, OrigainalBitmap.GetPixel(X, Y))
Next Y
Next X
PictureBox1.Image = RotatedBitmap
End Sub
End Class
الدالة الأولي سنطلق عليها RotateImageClockwise والدالة الثانية سنطلق عليها RotateImageAntiClockwise وسيكون شكل الكود لهاتان الدالتان كالتالي
كود :
Public Shared Function RotateClockwise(ByVal bmp As Bitmap) As Bitmap
Dim bmpWidth As Integer = bmp.Width
Dim bmpHeight As Integer = bmp.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(bmpHeight - Y - 1, X, bmp.GetPixel(X, Y))
Next Y
Next X
Return RotatedBitmap
End Function
Public Shared Function RotateAntiClockwise(ByVal bmp As Bitmap) As Bitmap
Dim bmpWidth As Integer = bmp.Width
Dim bmpHeight As Integer = bmp.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(Y, bmpWidth - X - 1, bmp.GetPixel(X, Y))
Next Y
Next X
Return RotatedBitmap
End Function
كود :
Public Class Form1
Dim bm As Bitmap = New Bitmap(My.Resources.body1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = bm
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bmR As Bitmap = RotateClockwise(PictureBox1.Image)
PictureBox1.Image = bmR
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bmR As Bitmap = RotateAntiClockwise(PictureBox1.Image)
PictureBox1.Image = bmR
End Sub
Public Shared Function RotateClockwise(ByVal bmp As Bitmap) As Bitmap
Dim bmpWidth As Integer = bmp.Width
Dim bmpHeight As Integer = bmp.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(bmpHeight - Y - 1, X, bmp.GetPixel(X, Y))
Next Y
Next X
Return RotatedBitmap
End Function
Public Shared Function RotateAntiClockwise(ByVal bmp As Bitmap) As Bitmap
Dim bmpWidth As Integer = bmp.Width
Dim bmpHeight As Integer = bmp.Height
Dim RotatedBitmap As New Bitmap(bmpHeight, bmpWidth)
For X As Integer = 0 To bmpWidth - 1
For Y As Integer = 0 To bmpHeight - 1
RotatedBitmap.SetPixel(Y, bmpWidth - X - 1, bmp.GetPixel(X, Y))
Next Y
Next X
Return RotatedBitmap
End Function
End Class
بالتوفيق
أخوكم عمر