0

I have a 28MB animated gif as an embedded resource which I am trying to load on my 'About' form.

The forms code is as follows :

About

private void About_Load(object sender, EventArgs e)
{
    pictureBox1.Image = EmbeddedResources.image("mygif.gif");
}

EmbeddedResources (simplified)

public class EmbeddedResources
{
    public static Assembly self { get { return Assembly.GetExecutingAssembly(); } }

    public static Image image(string name)
    {
        Image result = null;
        using (Stream res = self.GetManifestResourceStream("MyProject." + name))
        {
            result = Image.FromStream(res);
        }
        return result;
    }

}

The code seems to have no issues finding the resource, as result in EmbeddedResources.image() is filled with the data (not null), and the line pictureBox1.Image = EmbeddedResources.image("mygif.gif"); in About_Load() seems to pass data without error, but I am getting the following exception after loading the form, on the ShowDialog() method.

This is the code I am using (from a different form .. Form1 ) to load and display the About form.

private void button1_Click(object sender, EventArgs e)
{
    About frm = new About();
    frm.ShowDialog(this);
}

enter image description here

Why am I getting this exception, and what do I need to do to fix it so that my animated gif ( about 30 second loop ) can load ? (that is as small as I can get the loop and figured using an animated gif would be simpler than messing around with dated activex/com media controls or older directx framework to support video as a background and then have to mess with adding controls OVER the video -- big messy nightmare)

Stack Trace

   at System.Drawing.Image.SelectActiveFrame(FrameDimension dimension, Int32 frameIndex)
   at System.Drawing.ImageAnimator.ImageInfo.UpdateFrame()
   at System.Drawing.ImageAnimator.UpdateFrames(Image image)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • I assume this code works with a 100KB gif? – rene Aug 19 '16 at 21:55
  • Instead of your code, try using Resources from project properties as described in this post [http://stackoverflow.com/a/90699/2592875](http://stackoverflow.com/a/90699/2592875) – TnTinMn Aug 19 '16 at 22:28
  • @rene don't have a 100kb gif -- allegedly this is correct how to load it tho – Kraang Prime Aug 19 '16 at 22:30
  • @TnTinMn i would rather not embed as part of resources directly. -- Would prefer to get this to work using this method of embedding. Like I said, I have 0 issues in obtaining the data from where it is embedded. I feel this is a problem more with how it's set (possible not type 'Image' ??) not in the location of the source the data is streamed from. – Kraang Prime Aug 19 '16 at 22:32
  • @SamuelJackson, trying that way will give you a base line to compare against. If its works, then you at least know that the GIF can be loaded. – TnTinMn Aug 19 '16 at 22:43
  • @TnTinMn - I added a stack trace which shows there is clearly a problem in digesting the frames. Using this code [GifImage](http://www.vcskicks.com/csharp_animated_gif2.php) I was able to render the first frame, but then it still gave that exception. – Kraang Prime Aug 19 '16 at 22:55
  • @TnTinMn - I just tried with a 900kb gif image -- exact same issue. I feel I am close to a resolution as I can get the first frame, just something is borking lol. – Kraang Prime Aug 19 '16 at 23:04
  • At least now you know that the image is created, but there is something about one of the frames causing an issue. It has been quite a while since I did any coding for GIFs and will see if I can dig up so info to help. See if you can load the GIF using Internet Explorer or some other GIF viewer. – TnTinMn Aug 19 '16 at 23:25
  • @TnTinMn - the gif loads in ie, edge, chrome, safari, firefox just fine. – Kraang Prime Aug 20 '16 at 00:31
  • Could you post a link to your 900kb GIF that also failed. This make take some hacking to solve. – TnTinMn Aug 20 '16 at 00:33
  • @TnTinMn - if it helps, the gif displays fine when loaded from a file. When loaded from a resource is the only time it encounters this problem. – Kraang Prime Aug 20 '16 at 02:30

1 Answers1

1

Ok, I'm embarrassed that I did not catch this sooner. The issue is that by wrapping the Stream access in a using block, that the Stream is disposed on completion. I've seen this done before and always wondered about the consequence of it. Now I know the consequence.

A simple fix, do not Dispose the stream.

public class EmbeddedResources
{
    public static Assembly self { get { return Assembly.GetExecutingAssembly(); } }

    public static Image image(string name)
    {
        Image result = null;

        // note: typeof(EmbeddedResources).Namespace will only work if EmbeddedResources is defined in the default namespace
        Stream res = self.GetManifestResourceStream(typeof(EmbeddedResources).Namespace + "." + name);
        result = Image.FromStream(res);
        return result;
    }

}
TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • It works, but it's no longer animated. It's just the first frame. I wanted to check to be sure, before writing this. It is very close tho as it loads without crashing now. So definitely a problem with the stream. Is there perhaps a way to copy the whole image instead of just one frame ? (upvoted because of how close it is) :) – Kraang Prime Aug 20 '16 at 07:51
  • @SamuelJackson, I thought that it was a slow animation and thought I saw movement, but it was just tired eyes. In any case, I modified the answer. – TnTinMn Aug 20 '16 at 12:28
  • Thank you for taking the time to work this problem. Your solution above worked ! (Also, I understand what you mean, took me a second to notice that the image wasn't animated when I checked it. Long hours in front of a computer screen, then looking at abstract images you start to see things.... lol ) – Kraang Prime Aug 20 '16 at 15:32
  • If you feel like taking a shot at it (related to this), I have posted an animation control which is having some performance issues here [Custom Animated Image Control](http://codereview.stackexchange.com/questions/139223/custom-animated-image-control) . The objective is more granular control over the speed and direction of the animation. as well as convenient `LoadImage` methods to support from file, embedded resource, and direct image. – Kraang Prime Aug 20 '16 at 17:00