| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3124 人关注过本帖
标题:C#动画处理,gif老是无法正常显示
只看楼主 加入收藏
CMYK
Rank: 2
等 级:论坛游民
帖 子:67
专家分:74
注 册:2011-9-19
结帖率:46.15%
收藏
已结贴  问题点数:20 回复次数:8 
C#动画处理,gif老是无法正常显示
在做图片软件的时候,遇到gif无法正常显示,这是显示窗体的代码
程序代码:
using System.Drawing;
using System.Windows.Forms;
using System;

namespace WindowsFormsApplication1
{
    class F:Form
    {
        Bitmap bitmap;
        //是否为动画,用于ImageAnimator.Stop判断
        bool IsGif = false;
        private void OnFrameChanged(object o, EventArgs e)
        {
            ImageAnimator.UpdateFrames();
            this.CreateGraphics().DrawImage(this.bitmap, 0, 0);
        }

        //传入图片
        public void ShowImg(Bitmap bt)
        {
            bitmap = bt;
            //窗体设置
            InitializeComponent();

            this.ShowDialog();            
        }

        //绘制
        protected override void OnPaint(PaintEventArgs e)
        {
            if (ImageAnimator.CanAnimate(bitmap))
            {
                //静态图片处理
                ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
                IsGif = true;
            }
            else
            {
                //动态图片处理
                this.CreateGraphics().DrawImage(this.bitmap, 0, 0);
                IsGif = false;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // F
            // 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Width = bitmap.Width + 16;
            this.Height = bitmap.Height + 38;
            this.ShowIcon = false;
            this.MaximizeBox = false;

           // this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "F";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.F_FormClosing);
            this.ResumeLayout(false);

        }

        //关闭窗体时,释放资源
        private void F_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsGif)
                ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged)); 
            bitmap.Dispose();
        }
    }
}
[local]2[/local]在做图片软件的时候,遇到gif无法正常显示,这是显示窗体的代码
程序代码:
using System.Drawing;
using System.Windows.Forms;
using System;

namespace WindowsFormsApplication1
{
    class F:Form
    {
        Bitmap bitmap;
        //是否为动画,用于ImageAnimator.Stop判断
        bool IsGif = false;
        private void OnFrameChanged(object o, EventArgs e)
        {
            ImageAnimator.UpdateFrames();
            this.CreateGraphics().DrawImage(this.bitmap, 0, 0);
        }

        //传入图片
        public void ShowImg(Bitmap bt)
        {
            bitmap = bt;
            //窗体设置
            InitializeComponent();

            this.ShowDialog();            
        }

        //绘制
        protected override void OnPaint(PaintEventArgs e)
        {
            if (ImageAnimator.CanAnimate(bitmap))
            {
                //静态图片处理
                ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));
                IsGif = true;
            }
            else
            {
                //动态图片处理
                this.CreateGraphics().DrawImage(this.bitmap, 0, 0);
                IsGif = false;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // F
            // 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Width = bitmap.Width + 16;
            this.Height = bitmap.Height + 38;
            this.ShowIcon = false;
            this.MaximizeBox = false;

           // this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "F";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.F_FormClosing);
            this.ResumeLayout(false);

        }

        //关闭窗体时,释放资源
        private void F_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsGif)
                ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged)); 
            bitmap.Dispose();
        }
    }
}
[local]2[/local]
搜索更多相关主题的帖子: 动画 软件 
2012-02-27 10:34
CMYK
Rank: 2
等 级:论坛游民
帖 子:67
专家分:74
注 册:2011-9-19
收藏
得分:0 
pic.rar (30.03 KB)
呃,这是程序,大家看看
2012-02-27 10:35
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
winform不能显示动画 例如动态gif 这个你需要特殊处理,只能显示静态gif

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2012-02-27 11:18
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
看看这篇
http://tech.

站在春哥的肩膀上
2012-02-27 13:16
CMYK
Rank: 2
等 级:论坛游民
帖 子:67
专家分:74
注 册:2011-9-19
收藏
得分:0 
回复 4楼 xydddaxia
网页打不开,域名ping不通,呃……
2012-02-27 16:43
CMYK
Rank: 2
等 级:论坛游民
帖 子:67
专家分:74
注 册:2011-9-19
收藏
得分:0 
回复 3楼 wangnannan
应该可以吧,ImageAnimator类就是专门为动画设计的,我用不大来,版主有这方面的资料没?
2012-02-27 16:55
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:10 
回复 5楼 CMYK
真么会呢,内容是这样的

C# 让RichTextBox支持GIF

//获取选择的图形 并且保存出来

 private void button2_Click(object sender, EventArgs e)
        {
            IList _List = gifRichTextBox1.LoadSelectFile();

            for (int i = 0; i != _List.Count; i++)
            {
                File.WriteAllBytes(@"C:\Temp\A" + i.ToString() + ".gif", _List[0].ToArray());
            }
        }  

//添加一个GIF图形
        private void button3_Click(object sender, EventArgs e)
        {
            gifRichTextBox1.AddFile(@"C:\Temp\0.gif");
        }

//获取当前的RTF文字 你可以直接保存这个到数据库 或则通过SOCKET发送出去.
        string _Text = "";
        private void button1_Click(object sender, EventArgs e)
        {
            _Text = gifRichTextBox1.Rtf;


        }

//重新显示RTF内容

        private void button4_Click(object sender, EventArgs e)
        {
            gifRichTextBox1.Rtf = _Text;
        }

如果没有这个类..保存出的RTF数据..在其他RTF浏览的时候可能是个白色的矩形..

下面是全部的类

using System;
using System.Collections.Generic;
using System.Text;
using
using System.Windows.Forms;
using System.Drawing;

namespace Zgke.WindowFrom.Window.Controls.UserControls
{
    ///
    /// RichTextBox支持 GIF图形
    /// zgke@
    /// qq:116149
    ///
    public class GifRichTextBox : RichTextBox
    {      
        public GifRichTextBox()
        {
         
        }

        ///
        /// 重绘
        ///
        ///
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xF)
            {
                foreach (Control _SubControl in base.Controls)
                {
                    _SubControl.Tag = "1";
                }

                GetRichTextObjRectangle();

                for (int i = 0; i != base.Controls.Count; i++)
                {
                    if (base.Controls[i].Tag.ToString() == "1")
                    {
                        base.Controls.RemoveAt(i);
                        i--;
                    }
                }
            }
            base.WndProc(ref m);
        }

        ///
        /// 添加一个文件资源到RTF数据
        ///
        /// 文件路径   
        public void AddFile(string p_FileFullPath)
        {
            byte[] _FileBytes = File.ReadAllBytes(p_FileFullPath);
            Image _Image = Image.FromStream(new MemoryStream(_FileBytes));
            string _Guid = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "");
            StringBuilder _RtfText = new StringBuilder(@"{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}\uc1\pard\lang2052\f0\fs18{\object\objemb{\*\objclass Paint.Picture}");
            int _Width = _Image.Width * 15;
            int _Height = _Image.Height * 15;
            _RtfText.Append(@"\objw" + _Width.ToString() + @"\objh" + _Height.ToString());
            _RtfText.AppendLine(@"{\*\objdata");
            _RtfText.AppendLine(@"010500000200000007000000504272757368000000000000000000" + BitConverter.ToString(BitConverter.GetBytes(_FileBytes.Length + 20)).Replace("-", ""));
            _RtfText.Append("7A676B65" + _Guid); //标记            
            _RtfText.AppendLine(BitConverter.ToString(_FileBytes).Replace("-", ""));
            _RtfText.AppendLine(@"0105000000000000}{\result{\pict\wmetafile0}}}}");
            base.SelectedRtf = _RtfText.ToString();
        }

        ///
        /// 获取选择的文件
        ///
        /// 文件列表
        public IList LoadSelectFile()
        {
            IList _FileList = new List();
            int _Index = base.SelectedRtf.IndexOf(@"{\*\objdata");
            if (_Index == -1) return _FileList;

            while (_Index != -1)
            {
                MemoryStream _File = new MemoryStream();
                _Index += 80;
                string _LengthText = base.SelectedRtf.Substring(_Index, 8);

                int _Length = BitConverter.ToInt32(new byte[] { Convert.ToByte(_LengthText.Substring(0, 2), 16), Convert.ToByte(_LengthText.Substring(2, 2), 16), Convert.ToByte(_LengthText.Substring(4, 2), 16), Convert.ToByte(_LengthText.Substring(6, 2), 16) }, 0);
                _Index += 10;

                string _Head = base.SelectedRtf.Substring(_Index, 8);              
                if (_Head.ToUpper() == "7A676B65")
                {
                    _Index += 40;
                    _Length -= 20;
                    int _EndIndex = base.SelectedRtf.IndexOf("01050000", _Index);               
                    int _ReadIndex = 0;
                    _FileList.Add(LoadMemoryStream(base.SelectedRtf.Substring(_Index, _EndIndex - _Index), ref _ReadIndex, _Length));
                    _Index = _EndIndex;
                }
                _Index = base.SelectedRtf.IndexOf(@"{\*\objdata", _Index);
            }
            return _FileList;
        }

        ///
        /// 获取图形或则改动PictureBox的位置
        ///
        ///
        private void PointFile(string p_Rtf, Point p_StarPoint, int p_Width, int p_Height)
        {
            int _Index = p_Rtf.IndexOf(@"{\*\objdata");
            if (_Index == -1) return;      
            _Index += 80;
            string _LengthText = p_Rtf.Substring(_Index, 8);

            int _Length = BitConverter.ToInt32(new byte[] { Convert.ToByte(_LengthText.Substring(0, 2), 16), Convert.ToByte(_LengthText.Substring(2, 2), 16), Convert.ToByte(_LengthText.Substring(4, 2), 16), Convert.ToByte(_LengthText.Substring(6, 2), 16) }, 0);
            _Index += 10;

            string _Head = p_Rtf.Substring(_Index, 8);
            if (_Head.ToUpper() != "7A676B65") return;   //如果不是标记出来的 就不生成PictureBox
            _Index += 8;

            string _Guid = p_Rtf.Substring(_Index, 32);

            Control _Controls = base.Controls[_Guid];
            if (_Controls == null)
            {
                PictureBox _PictureBox = new PictureBox();
                _PictureBox.Name = _Guid;
                _PictureBox.Tag = "0";
                _PictureBox.Location = p_StarPoint;
                _PictureBox.Size = new Size(p_Width, p_Height);

                _Index += 32;
                _Length -= 20;

                _PictureBox.Image = Image.FromStream(LoadMemoryStream(p_Rtf, ref _Index,_Length));

                base.Controls.Add(_PictureBox);
            }
            else
            {
                _Controls.Location = p_StarPoint;
                _Controls.Size = new Size(p_Width, p_Height);
                _Controls.Tag = "0";
            }
        }

        ///
        /// 根据RTF保存的内容获取内存流
        ///
        /// RTF
        /// 开始位置
        /// 读取数量
        /// 内存流
        private MemoryStream LoadMemoryStream(string p_Text,ref int p_Index,int p_Count)
        {
            MemoryStream _File =new MemoryStream();
            char[] _Text = p_Text.ToCharArray();
            for (int i = 0; i != p_Count; i++)
            {
                if (_Text[p_Index] == '\r' && _Text[p_Index + 1] == '\n')
                {
                    i--;
                }
                else
                {
                    _File.WriteByte(Convert.ToByte(_Text[p_Index].ToString() + _Text[p_Index + 1].ToString(), 16));
                }
                p_Index += 2;
            }
            return _File;
        }
      
        ///
        /// 获取RICHTEXTBOX所有图形的位置
        ///
        /// RICHTEXTBOX
        /// 位置信息
        private void GetRichTextObjRectangle()
        {
            RichTextBox _RichText = new RichTextBox();
            _RichText.Rtf = base.Rtf;
            int _Count = base.Text.Length;

            for (int i = 0; i != _Count; i++)
            {
                if (base.Text[i] == ' ')
                {
                    _RichText.Select(i, 1);

                    if (_RichText.SelectionType.ToString() == "Object")
                    {
                        Point _StarPoint = base.GetPositionFromCharIndex(i);

                        System.Text.RegularExpressions.Regex _RegexWidth = new System.Text.RegularExpressions.Regex(@"(?<=\\objw)[^\\]+");
                        System.Text.RegularExpressions.Regex _RegexHeight = new System.Text.RegularExpressions.Regex(@"(?<=\\objh)[^{]+");

                        int _Width = 0;
                        int _Height = 0;

                        if (int.TryParse(_RegexWidth.Match(_RichText.SelectedRtf).Value, out _Width) && int.TryParse(_RegexHeight.Match(_RichText.SelectedRtf).Value, out _Height))
                        {
                            _Width = _Width / 15;
                            _Height = _Height / 15;
                            PointFile(_RichText.SelectedRtf, _StarPoint, _Width, _Height);
                        }
                    }
                }
            }
            _RichText.Dispose();
        }      
    }
}



站在春哥的肩膀上
2012-02-27 17:03
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:10 
this.CreateGraphics().DrawImage(.....)  只能显示gif动画第一帧
这个方法是无法LZ满足要求的
程序代码:
 private void OnFrameChanged(object o, EventArgs e)
         {
             ImageAnimator.UpdateFrames();
             this.CreateGraphics().DrawImage(this.bitmap, 0, 0);
         }

这里应该有问题
可以这样写 LZ试试看
    private void OnFrameChanged(object o, EventArgs e)
        {
            this.Invalidate();//该函数的作用是使整个窗口客户区无效。窗口的客户区无效意味着需要重绘
            ImageAnimator.UpdateFrames();
            this.CreateGraphics().DrawImage(this.bitmap, 0, 0);

        } 


[ 本帖最后由 wangnannan 于 2012-3-1 09:12 编辑 ]

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2012-03-01 09:06
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
具体参照这里 http://hi.baidu.com/gmrbrian/blog/item/e0c85650222c296484352482.html
ImageAnimator允许你在窗口中直接插入带动画效果的GIF文件 而不用自己去控制一系列的BMP文件的加载更新

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2012-03-01 09:11
快速回复:C#动画处理,gif老是无法正常显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024994 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved