05-10-12, 03:39 PM
كاتب الموضوع : silverlight
بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله وبركاته
مقدمـــــة:
من المؤكد أن قارئ هذا المقال فد سمع عن أو استخدم كلمة GIF وهي كلمة أو إمتداد يستخدم لملفات الصور المتحركة............... وبالتأكيد جميعنا قد استخدم مثل هذا النوع من الصور في برامحه بشكل ما. وفي بعض المقالات التي كتبتها سابقا كنت قد استعرضت بعض الأفكار للتعامل مع الصور المتحركة وذلك باستحدام الجرافكس +GDI وتستطيع عزيزي القارئ أن تقرأ هذا المقالات في اللينك التالي:
الصور المتحركة في الدوت نت Animation
الصور المتحركة في الدوت نت Animation الجزء الثاني
الصور المتحركة في الدوت نت Animation الجزء الثالث
الصور المتحركة في الدوت نت Animation الجزء الرابع
كيف نتأكد أن الصورة متحركة:
لنتأكد أن الصورة متحركة علينا إتباع الخطوات التالية:
1- نقوم بتعريف متغير يعبر عن أي صورة متحركة حيث هنا الصورة المتحركة موجودة في Resouces الخاصة بالمشروع ومن ثم يكون لدينا الكود بالشكل الأتي:
كود :
Dim gifImage As Image = My.Resources.sweet_gifs_003
كود :
Dim fDimensions As FrameDimension = New FrameDimension(gifImage.FrameDimensionsList(0))
FrameDimension Class On MSDN
3- ثم نقوم بتعريف متغير يعبر عن عدد Frames الموجودة في الصورة وهو عبارة عن متغير رقمي وهذا المتغير نحصل علي قيمتة عن طريق دالة موجودة بالكلاس Image واسم هذه الدالة هو GetFrameCount حيث يتم تمرير المتغير الذي تم تعريفه بالخطوة السابقة الي هذه الدالة ومن ثم نحصل علي الكود بالشكل الأتي:
كود :
Dim frames As Integer = gifImage.GetFrameCount(fDimensions)
كود :
If frames > 1 Then
MessageBox.Show("Image is an animated GIF" + " and the number of frames are" + " " + frames.ToString + " frames")
Else
MessageBox.Show("Image is not an animated GIF.")
End If
كود :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim gifImage As Image = My.Resources.sweet_gifs_003
Dim fDimensions As FrameDimension = New FrameDimension(gifImage.FrameDimensionsList(0))
Dim frames As Integer = gifImage.GetFrameCount(fDimensions)
If frames > 1 Then
MessageBox.Show("Image is an animated GIF" + " and the number of frames are" + " " + frames.ToString + " frames")
Else
MessageBox.Show("Image is not an animated GIF.")
End If
End Sub
وبعد أن أوضحت لكم كيف نتأكد أن الصورة متحركة أم لا عن طريق الكود نحتاج هنا لبناء دالة نسترجع منها نفس النتائج التي حصلنا عليها بالخطوات السابقة ولكن مع تعديل بسيط في الكود حيث أن الدالة التي سنقوم بكتابة الكود الخاص بها سنسترجع منها قيمة لمتغير عبارة عن Boolean والشكل النهائي للدالة سيكون كالأتي
كود :
''' <summary>
''' Check if the imag is Animated or not
''' </summary>
''' <param name="img"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function IsGifImage(ByVal img As Image) As Boolean
If (img Is Nothing) Then
Throw New ArgumentNullException("img")
End If
Dim fDimensions As FrameDimension = New FrameDimension(img.FrameDimensionsList(0))
Dim frames As Integer = img.GetFrameCount(fDimensions)
Dim isGif As Boolean = False
If frames > 1 Then
isGif = True
End If
Return isGif
End Function
كود :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim gifImage As Image = My.Resources.sweet_gifs_003
If IsGifImage(gifImage) Then
MessageBox.Show("Image is an animated GIF")
Else
MessageBox.Show("Image is not an animated GIF.")
End If
End Sub
أيضا الدالة الثانية توضح كيفية الحصول علي عدد Frames الموجودة في الصورة المتحركة
كود :
''' <summary>
''' Get Gif Image Frame
''' </summary>
''' <param name="img"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetImageFrames(ByVal img As Image) As Integer
If IsGifImage(img) Then
Dim fDimensions As FrameDimension = New FrameDimension(img.FrameDimensionsList(0))
Dim frames As Integer = img.GetFrameCount(fDimensions)
Return frames
End If
Return 0
End Function
بالتأكيد هذا السؤال يجول في خاطر القارئ ولكي نفعل ذلك سوف نستحدم الدوال التي كتبناها سابقا لنسترجع الصور الموجودة في الصورة المتحرك ونبني كلاسا نستخدمه في استرجاع الصور المتحركة ومن الممكن أن نضيف للكلاس بعض الدوال أو Properties أو اي شئ أخر نراه مناسبا لهدفنا من هذا الكلاس. ولسوف أطلق علي هذا الكلاس إسم GifImageFrames
كود :
Public Class GifImageFrames
End Class
1- متغير يعبر عن الصورة
2- متغير يعبر عن عدد Frames الموجودة بالصورة وهو عبارة عن متغير رقمي
كود :
Public Class GifImageFrames
#Region " Fields "
Private _frames As Integer
Private _image As Bitmap
#End Region
كود :
Public Class GifImageFrames
#Region " Fields "
Private _frames As Integer
Private _image As Bitmap
#End Region
#Region " Constructor "
Public Sub New()
Me._image = Nothing
Me._frames = 0
End Sub
#End Region
End Class
ستلاحظون من الكود أدناه أنني أضفت دالة جديدة نسترجع منها الصور الموجود في الصورة المتحركة وهي الدالة GetImages
وأخيرا لنضيف بعض Properties وايضا المزيد من Constructor لهذا الكلاس ومن ثم يكون الشكل النهائي لهذا الكلاس كالتالي وهو كلاس بسيط جدا جدا
كود :
Imports System.Drawing.Imaging
Public Class GifImageFrames
#Region " Fields "
Private _frames As Integer
Private _image As Bitmap
#End Region
#Region " Constructor "
Public Sub New()
Me._image = Nothing
Me._frames = 0
End Sub
Public Sub New(ByVal frames As Integer, ByVal image As Bitmap)
Me._image = image
Me._frames = frames
End Sub
#End Region
#Region " Properties "
Public ReadOnly Property IsGif As Boolean
Get
If Me._image IsNot Nothing Then
Return Me.IsGifImage(Me._image)
End If
Return Nothing
End Get
End Property
Public ReadOnly Property Image As Bitmap
Get
Return Me._image
End Get
End Property
Public ReadOnly Property Frames As Integer
Get
If Me._image IsNot Nothing Then
Return Me.GetImageFrames(Me._image)
End If
Return 0
End Get
End Property
Public ReadOnly Property Images As List(Of Bitmap)
Get
Return GetImages()
End Get
End Property
#End Region
#Region " Methods "
Private Function GetImageFrames(ByVal img As Image) As Integer
If IsGifImage(img) Then
Dim fDimensions As FrameDimension = New FrameDimension(img.FrameDimensionsList(0))
Dim frames As Integer = img.GetFrameCount(fDimensions)
Return frames
End If
Return 0
End Function
Private Function IsGifImage(ByVal img As Image) As Boolean
If (img Is Nothing) Then
Throw New ArgumentNullException("img")
End If
Dim fDimensions As FrameDimension = New FrameDimension(img.FrameDimensionsList(0))
Dim frames As Integer = img.GetFrameCount(fDimensions)
Dim isGif As Boolean = False
If frames > 1 Then
isGif = True
End If
Return isGif
End Function
Private Function GetImages() As List(Of Bitmap)
If Me._image IsNot Nothing Then
Dim images As New List(Of Bitmap)
Dim gifImage As Image = Me._image
Dim frames As GifImageFrames() = New GifImageFrames() {}
Dim fd As New FrameDimension(gifImage.FrameDimensionsList(0))
Dim frameCount As Integer = gifImage.GetFrameCount(fd)
If frameCount > 1 Then
frames = New GifImageFrames(frameCount - 1) {}
Dim times As Byte() = gifImage.GetPropertyItem(&H5100).Value
For i As Integer = 0 To frameCount - 1
gifImage.SelectActiveFrame(fd, i)
Dim length As Integer = BitConverter.ToInt32(times, 4 * i) * 10
frames(i) = New GifImageFrames(length, New Bitmap(gifImage))
Next
End If
For Each frm As GifImageFrames In frames
images.Add(frm.Image)
Next
Return images
End If
Return Nothing
End Function
#End Region
End Class ' GifImageFrames
كيف نستحدم الكلاس
1- نقوم بتعريف صورة متحركة
2- نقوم بتعريف متغير يعبر عن هذا الكلاس الذي كتبناه ومن ثم نمرر له الصور المتحركة
3- بعد ذلك ومن الصفة Images Property يكون لدينا الصور الموجودة في الصورة المتحركة حيث نستخدمها كيفما شئنا بعد ذلك
والمثال التالي يوضح الحصول عي الصور الموجودة بصورة متحركة ومن ثم نقوم برسم جميع الصور علي سطح الفورم
كود :
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim gifImage As Image = My.Resources.sweet_gifs_003
Dim frames As GifImageFrames = New GifImageFrames(gifImage)
For Each img As Bitmap In frames.Images
For x = 0 To frames.Frames - 1
e.Graphics.DrawImage(img, CInt(gifImage.Width * x / frames.Frames), 0, CInt(img.Width / frames.Frames), CInt(img.Height / frames.Frames))
Next
Next
End Sub
End Class
أتمني أن يكون الموضوع الذي ناقشناه مفيدا لكم بشكل ما
بالمرفقات ستجدون نسخة من الكلاس وكيفية استخدامة بنسخة الفيجوال استوديو 2010
ولمن لا يملك نسخة 2010 يستطيع أن يعيد كتابة الكود مرة أخري ليتناسب مع نسخة الفيجوال استوديو الذي لديه
وبالتوفيق لكم جميعا
أخوكم عمر