Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Bitmap b=new Bitmap((Image)Ims[0]);

IEnumerator E= (b).FrameDimensionsList.GetEnumerator();

E.MoveNext();

b= (Bitmap)E.Current;

 

Invalid cast exception. I must be doing this wrong. I have look all over the net form information on how to do this but could find nothing.

C#
Guest Deleted member 22320
Posted

try this

 

  
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms

Public Module test
Sub Main()
	Application.Run(New myform)
End Sub
End Module

Public Class myform
Inherits System.Windows.Forms.Form

       Private components As System.ComponentModel.Container
Private newimg As Bitmap = New Bitmap("C:\l5r\anielg.gif")

Public Sub New()


        MyBase.New()
        Me.components = New System.ComponentModel.Container()
	ImageAnimator.Animate(newimg, New EventHandler(AddressOf Me.go))
End Sub

Protected Overrides Sub OnPaint(e As PaintEventArgs)
    	ImageAnimator.UpdateFrames()
    	e.Graphics.DrawImage(newimg, 20, 20)
End Sub

Public Sub go(ByVal sender As Object, ByVal e As EventArgs)
	' Without this line, the form will not keep fireing OnPaint
	Me.Invalidate()
End Sub
End Class

Guest Deleted member 22320
Posted

I think you should be able to 'adapt' it to do that.

 

If you look in the paint sub, it extracts each frame and paints it to the control. I guess all you would need to do is whatever you wanted to do with the image object before.. save it, convert it or whatever..

 

 

hope it helps.. I can't explain the code in detail as I found it myself while looking up code for manipulating GIFs.

  • 6 years later...
Posted

Here is the code I use to extract GIF frames

 

EDIT: just so you are aware Image.FrameDimensionsList is an array of Guid's not Images/Bitmaps

 

class Frame
{
    public int Duration;
    public Bitmap Image;

    public Frame(int duration, Bitmap image)
    {
         Image = image;
         Duration = duration;
    }
}

// implementation
Frame[] frames;

FrameDimension fd = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(fd);

if (frameCount > 1)
{
     frames = new Frame[frameCount];

     //0x5100 is the property id of the GIF frame's durations
     //this property does not exist when frameCount <= 1
     byte[] times = image.GetPropertyItem(0x5100).Value;

     for (int i = 0; i < frameCount; i++)
     {
           //selects GIF frame based on FrameDimension and frameIndex
           image.SelectActiveFrame(fd, i);

           //length in milliseconds of display duration
           int length = BitConverter.ToInt32(times, 4 * i) * 10;

           //save currect image frame as new bitmap
           frames[i] = new Frame(length, new Bitmap(image));
      }
} // Not animated

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...