日期:2014-05-18  浏览次数:21236 次

如何利用剪帖版把gif图贴进RichTextBox
小弟新手,想做一个类似于QQ一样的RichTextBox控件,但是遇到一个问题,从网页复制出来的gif图贴到RichTextBox是只显示一帧的图像,即图片不是动态的......
  在网上搜索了很多例子,比如把图片放进PictureBox,然后把PictureBox嵌入到RichTextBox,但是我通过Clipboard.GetImage()方法获取图片,放入到PictureBox控件中显示时,也是只有一帧的。。。
  不知道有哪位高手有过类似的经过。。。。给点提醒,Thanks in advance

------解决方案--------------------
richtextbox中显示gif图片 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Zgke.WindowFrom.Window.Controls.UserControls;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
GifRichTextBox gifRichTextBox1;
private void Form1_Load(object sender, EventArgs e)
{
gifRichTextBox1 = new GifRichTextBox();
gifRichTextBox1.Width = 500;
gifRichTextBox1.Height = 500;
Controls.Add(gifRichTextBox1);
}

private void button3_Click(object sender, EventArgs e)
{

}

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

private void button2_Click_1(object sender, EventArgs e)
{
IList<MemoryStream> _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_1(object sender, EventArgs e)
{
gifRichTextBox1.AddFile(@"C:\Temp\0.gif");
}
}
}


namespace Zgke.WindowFrom.Window.Controls.UserControls
{
/// 
/// RichTextBox支持 GIF图形
/// zgke@sina.com
/// 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;}}