تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI - الجزء الخامس
#1
كاتب الموضوع : 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
الأن لنبني دالتان Functions من الكود السابق حيث يمكن تختص احدهما بعمل دوران للصورة في اتجاه عقارب الساعة Clockwise والثانية تقوم بالعكس أي Anticlockwise
الدالة الأولي سنطلق عليها 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
المثال التالي يوضح كيفية استخدام الدالتان أعلاه ولتنفيذ ذلك افتح مشروع وأضف له صورة ثم اضف له PictureBox وأضف له عدد 2 باتون ثم اكتب الكود بالشكل التالي


كود :
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



بالتوفيق

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


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

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


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