0

I'm building a WinForms app that generates a .gif file. I am achieving this through the GifBitmapEncoder class, which stores the resulting gif.

What I'd like to know is how I can put whatever was generated inside of a PictureBox control. A search has yielded things that are close to what I want, but don't quite get there (this post, for example, shows how to do it without code, but that doesn't really help me).

Does anyone know how to achieve this?

Brandon Lee
  • 38
  • 2
  • 10
  • 1
    The PictureBox Control uses the [ImageAnimator](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imageanimator) class to animate images which allow timed frames. You just need to set the Image property to an Image object created from a GIF Image. Note that `GifBitmapEncoder` is used in WPF, not WinForms. If you use this encoder to create a sequence of frames and it doesn't work somehow, you have to show how you do it. If you don't know how to convert a BitmapSource to a `System.Drawing.Image`, you can just save the Image to a file stream in the meanwhile. Or use WPF. – Jimi Sep 23 '20 at 15:49
  • @Jimi Thank you for your reply. Didn't know that GifBitmapEncoder came from WPF, that's gtk. I know how to go from BitmapSource to an Image, but how would I convert the GifBitmapEncoder to a BitmapSource? – Brandon Lee Sep 23 '20 at 18:36
  • Well, you don't *convert* from an encoder to a BitmapSource, the encoder generates an image of the specified type that you can write to a stream. You can use a MemoryStream as the source of a BitmapImage. See [BitmapImage.StreamSource](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.bitmapimage.streamsource). The problem here is not exactly to generate an Image or a BitmapSource, is to create a working GIF. You probably know about Unisys Corp., lzw , patents and royalties (at that time). So you have to build kind of a *custom encoder* for GIFs. Did you? – Jimi Sep 23 '20 at 19:17
  • Of course, if you mean that you are creating animated GIFs. If those are just single frames, it doesn't matter. – Jimi Sep 23 '20 at 19:28
  • @Jimi Ah, I should've specified that I'm looking for an animated gif, my bad. I'll look into the MemoryStream method for the time being. Also I did not build a custom encoder. – Brandon Lee Sep 23 '20 at 21:41

0 Answers0