02-10-12, 03:15 PM
كاتب الموضوع : silverlight
التعامل مع الصور Images في بيئة الدوت نت باستخدام +GDI اللغة المستخدمة: الفيجوال بيسك
التطبيق: فيجوال استوديو 2005 و 2008
المستوي: التقييم متروك للقارئ
إعداد: مهندس / عمر أمين إبراهيم
الجزء الثالث: كيف نأخذ صورة لسطح المكتب
كما سبق وتحدثنا يوجد في الدوت نت كلاس هام جدا وهو Graphics Class وهذا الكلاس هو عصب +GDI وهناك طريقة Method بهذا الكلاس وهي CopyFromScreen وهذه الطريقة نستطيع باستخدامها أن نأخذ صورة لسطح المكتب. كيف نفعل ذلك
الفكرة ببساطة أننا نقوم بتعريف صورة Bitmap وأيضا Rectangle أبعاده تساوي أبعاد شاشة الكمبيوتر ثم باستخدام Graphics Class نقوم بأخذ صورة لسطح المكتب ثم يمكننا أن نستخدمها كما نشاء بعد ذلك والمثال التالي يوضح كيفية عمل ذلك
مثال علي ذلك
افتح مشروع جديد ثم أضف للفورم باتون و PictureBox ثم اكتب الكود بالشكل التالي في الحدث Click الخاص بالباتون
كود :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WindowState = FormWindowState.Minimized
Dim bounds As Rectangle = Screen.PrimaryScreen.Bounds
Dim screenshot As System.Drawing.Bitmap = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot.GetThumbnailImage(PictureBox1.Width, PictureBox1.Height, Nothing, Nothing)
Me.WindowState = FormWindowState.Normal
End Sub
End Class
كود :
Public Shared Function GetImageFromDeskTop() As Bitmap
Dim bounds As Rectangle = Screen.PrimaryScreen.Bounds
Dim screenShot As System.Drawing.Bitmap = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenShot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Return screenShot
End Function
افتح مشروع جديد ثم أضف للفورم باتون و PictureBox ثم اكتب الكود بالشكل التالي في الحدث Click الخاص بالباتون
كود :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WindowState = FormWindowState.Minimized
Dim scrBmp As Bitmap = GetImageFromDeskTop()
PictureBox1.Image = scrBmp.GetThumbnailImage(PictureBox1.Width, PictureBox1.Height, Nothing, Nothing)
Me.WindowState = FormWindowState.Normal
End Sub
Public Shared Function GetImageFromDeskTop() As Bitmap
Dim bounds As Rectangle = Screen.PrimaryScreen.Bounds
Dim screenShot As System.Drawing.Bitmap = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenShot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Return screenShot
End Function
End Class
الكود التالي يوضح شكل أخر لهذه Function
كود :
Public Shared Function GetImageFromDeskTop(ByVal Bounds As Rectangle) As Bitmap
Dim screenShot As System.Drawing.Bitmap = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenShot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Return screenShot
End Function
والكود التالي يوضح شكل أخر لهذه Function
كود :
Public Shared Function GetImageFromDeskTop(ByVal Bounds As Rectangle, ByVal Owner As Form) As Bitmap
Dim frm As Form = Owner
Owner.WindowState = FormWindowState.Minimized
Dim screenShot As System.Drawing.Bitmap = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenShot)
graph.CopyFromScreen(Bounds.X, Bounds.Y, 0, 0, Bounds.Size, CopyPixelOperation.SourceCopy)
Owner.WindowState = FormWindowState.Normal
Return screenShot
End Function
والكود التالي يوضح كيفية استخدام الدالة الأخيرة
كود :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim scrBmp As Bitmap = GetImageFromDeskTop(New Rectangle(100, 100, 200, 200), Me)
PictureBox1.Image = scrBmp.GetThumbnailImage(PictureBox1.Width, PictureBox1.Height, Nothing, Nothing)
End Sub
Public Shared Function GetImageFromDeskTop(ByVal Bounds As Rectangle, ByVal Owner As Form) As Bitmap
Dim frm As Form = Owner
Owner.WindowState = FormWindowState.Minimized
Dim screenShot As System.Drawing.Bitmap = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim graph As Graphics = Graphics.FromImage(screenShot)
graph.CopyFromScreen(Bounds.X, Bounds.Y, 0, 0, Bounds.Size, CopyPixelOperation.SourceCopy)
Owner.WindowState = FormWindowState.Normal
Return screenShot
End Function
End Class
بالتوفيق
أخوكم عمر