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

查看:22
本文介绍了将图像转换为 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 );
}

通过我的另一种方法 ConvertFromGif() 运行生成的 Gif 时,使用 ConvertToGif() 创建的图像的帧数似乎也没有注册:

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,但我的 ConvertToGif() 方法制作的 Gif 无法运行.

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

推荐答案

我环顾四周,发现 GifBitmapEncoder 实际上只是为了读取 GIF 以便它们可以在 WPF 中显示这个类将允许您创建多帧然而,GIF 不提供元数据处理程序,允许您放置关键帧信息,如循环计数、帧之间的延迟等.他们将元数据部分留给了第 3 方实施

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 元数据处理程序,我们确实提供了一种机制第3方自己写.编写元数据的文档处理程序可以在这里找到:http://msdn2.microsoft.com/en-us/library/ms737407.aspx 以及本文http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wiccodec.asp.

Unfortunately we don't expose a metadata reader or writer for GIFs (animated or otherwise). In order to produce correct animated GIFs, you need to write out some metadata with timing information. Since we don't provide a GIF metadata handler, we do provide a mechanism for 3rd parties to write their own. Documentation for writing a metadata handler can be found here: http://msdn2.microsoft.com/en-us/library/ms737407.aspx as well as in this article 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.

有两种途径供您选择:使用 3rd 方 .Net 库,它允许您编写 Gif 或编写您自己的.

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

来自您之前的问题 ConvertGIf 的图像列表 我建议您使用 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天全站免登陆