将图像转换为Gif,部分工作? [英] Converted Image to Gif, Works Partially?

查看:158
本文介绍了将图像转换为Gif,部分工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简短的方法将图像(Image类)转换为GifEncoder,最后将Gif保存到文件中。但是,当我打开生成的Gif时,它有一些问题。 1)Gif在浏览器中停止播放2个循环后停止播放。 2)当通过图像编辑器加载时,颜色似乎在像素之间混合。

I've written a short method for converting images(Image class) to a GifEncoder and finally saving that Gif to a file. However, when I go to open the resulting Gif created, it has some problems. 1) The Gif stops playing after 2 cycles in a browser. 2) When loaded through an image editor the colors seem to mix up a bit between pixels.

public void ConvertToGif( string DestinationPath , Image myImage , int myFrames ) {
    Bitmap myBitmap = new Bitmap( myImage.Width / myFrames , myImage.Height );
    GifBitmapEncoder myEncoder = new GifBitmapEncoder();

    int i = 0;
    while( i < myFrames ) {
        Graphics GrDrw = Graphics.FromImage( myBitmap );
        var DestRegion = new Rectangle( 0 , 0 , myBitmap.Width , myBitmap.Height );
        var SrceRegion = new Rectangle( myBitmap.Width * i , 0 , myBitmap.Width , myBitmap.Height );
        GrDrw.DrawImage( myImage , DestRegion , SrceRegion , GraphicsUnit.Pixel );
        BitmapSource mySource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( myBitmap.GetHbitmap() , IntPtr.Zero , Int32Rect.Empty , BitmapSizeOptions.FromEmptyOptions() );
        myEncoder.Frames.Add( BitmapFrame.Create( mySource , mySource ) );
        GrDrw.Dispose();
        i += 1;
    }

    System.IO.FileStream myStream = new System.IO.FileStream( @DestinationPath , System.IO.FileMode.Create );
    myEncoder.Save( myStream );
}

使用ConvertToGif()创建的图像中的帧数也不似乎在通过我的其他方法运行生成的Gif时注册,ConvertFromGif():

The number of frames from an image created with ConvertToGif() also don't seem to register when running the resulting Gif through my other method, ConvertFromGif():

    public Image ConvertFromGif( Image myImage ) {
        var myDimensions = new FrameDimension( myImage.FrameDimensionsList[ 0 ] );
        var myFrames = myImage.GetFrameCount( myDimensions );
        var newImage = new Bitmap( myImage.Width * myFrames , myImage.Height );

        for( int i = 0; i < myFrames; i ++ ) {
            myImage.SelectActiveFrame( myDimensions , i );
            var DestRegion = new Rectangle( myImage.Width * i , 0 , myImage.Width , myImage.Height );
            var SrceRegion = new Rectangle( 0 , 0 , myImage.Width , myImage.Height );

            Graphics GrDrw = Graphics.FromImage( newImage );
            GrDrw.DrawImage( myImage , DestRegion , SrceRegion , GraphicsUnit.Pixel );
            GrDrw.Dispose();
        }

        return newImage;
    }

我可以通过我的ConvertFromGif()方法运行任何Gif,但Gifs由我的ConvertToGif()方法不起作用。

I can run any Gif through my ConvertFromGif() method, but Gifs made by my ConvertToGif() method won't work.

推荐答案

我环顾四周,发现GifBitmapEncoder只是真的要阅读GIF,因此它们可以在WPF中显示,这个类将允许您创建多帧GIF,但是它不提供元数据处理程序,允许您放置关键帧信息,如循环计数,帧之间的延迟等。它们已离开元数据部分第三方实施

I had a Look around and discovered that GifBitmapEncoder is only really meant to read GIFs so they can be displayed in WPF this class will allow you to create multi-framed GIFs however it does not provide a Metadata handler that allows you to put key frame information like cycle count, delay between frame etc. They have left the Metadata part out for 3rd parties to implement


不幸的是,我们不公开GIF的元数据读取器或编写器
(动画或其他) 。为了生成正确的动画GIF,
你需要用时间信息写出一些元数据。由于
不提供GIF元数据处理程序,我们确实为
第三方提供了编写自己的机制。可以在此处找到用于编写元数据
处理程序的文档:
http://msdn2.microsoft.com/en-us/library/ms737407.aspx 以及
这篇文章
http://msdn.microsoft.com/library/default.asp?url = / library / en-us / dnlong / html / wiccodec.asp

引自: http://social.msdn.microsoft.com/Forums/en-US/6ef358a7-d1ac-4267-91d9-166024aad8ca/creating-animated-gif-files-in-wpf?forum=wpf

Gifs有一些默认的时间信息,所以你得到了2个周期,但这也可能与其他浏览器等不同。

Gifs have some default timing information so you are getting the 2 cycles but that is all it may also differ from other browsers etc.

同时在网上看到我看到其他用户在帧之间出现相同问题也有同样的问题。

Also while looking online I saw other users having the same issue with colour bleed between frames.

您有两种途径:使用第三方.Net允许您编写Gif或自己编写的库。

There is two avenues for you: Use a 3rd party .Net Library that allows you to write Gifs or write you own.

上一个问题将图像列表转换为GIf 我建议你使用NGif - http://www.codeproject.com/Articles/11505/NGif-Animated-GIF- Encoder-for-NET

From your previous question Convert a List of Images to a GIf I would suggest you use NGif - http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET

但是,如果您想为GifBitmapEncoder创建自己的元数据编写器,请按照上面帖子中提供的链接获取元数据文档读者和作家。

However if you want to create your own metadata writer for the GifBitmapEncoder follow the links provided on the post above for the documentation for metadata read and writers.

这篇关于将图像转换为Gif,部分工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆