日期:2014-05-18 浏览次数:20956 次
/// <summary> /// 写一GIF文件到剪贴板 能在Work QQ里粘贴 实际是HTML 的引用 /// </summary> /// <param name="p_File">文件</param> public void SetImageToClipbord(string p_File) { string _Path = p_File; if (File.Exists(_Path)) { _Path = "file:///" + p_File; } int _EndHtml = 342 + p_File.Length; int _EndSelection = 306 + p_File.Length; StringBuilder _HtmlFormatData = new StringBuilder(); _HtmlFormatData.AppendLine("Version:1.0"); _HtmlFormatData.AppendLine("StartHTML:000000179"); _HtmlFormatData.AppendLine("EndHTML:" + _EndHtml.ToString("000000000")); _HtmlFormatData.AppendLine("StartFragment:000000294"); _HtmlFormatData.AppendLine("EndFragment:" + _EndSelection.ToString("000000000")); _HtmlFormatData.AppendLine("StartSelection:000000294"); _HtmlFormatData.AppendLine("EndSelection:" + _EndSelection.ToString("000000000")); _HtmlFormatData.AppendLine("SourceURL:file:///C:/4.htm"); _HtmlFormatData.AppendLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"); _HtmlFormatData.AppendLine(""); _HtmlFormatData.AppendLine("<HTML><HEAD></HEAD>"); _HtmlFormatData.AppendLine(""); _HtmlFormatData.AppendLine("<BODY><!--StartFragment--><IMG src=\"" + p_File + "\"><!--EndFragment--></BODY>"); _HtmlFormatData.AppendLine("</HTML>"); MemoryStream _ClipboardData = new MemoryStream(Encoding.UTF8.GetBytes(_HtmlFormatData.ToString())); Clipboard.SetData("Html Format", _ClipboardData); }
------解决方案--------------------
在园子里转的 你看看
private void button1_Click(object sender, EventArgs e)
{
var GifFilePath = @"C:\Users\Public\Downloads\0022.gif";
Clipboard.SetText(string.Format(@"Version:0.9StartHTML:00000176EndHTML:00000326StartFragment:00000210EndFragment:00000290SourceURL:file:///C:/Develop/ADITALL/MIXER/playerAS3/bin/sharp-copy-test.html <html> <body> <!--StartFragment--> <p> <strong>AAA </strong> <img src=""file:///{0}"" /> </p> <!--EndFragment--> </body> </html>", GifFilePath.Replace("\\","/")
), TextDataFormat.Html);
}
其原理就是变通地将包含动画的Html代码复制到剪切板内。
但具体为什么这么写我也不知道,没找到相关的剪贴板格式资料,以至于我修改SourceURL:file:///C:/Develop/ADITALL/MIXER/playerAS3/bin/sharp-copy-test.html为其他文件路径都会导致失效,很诡异~~
这个格式是从这里窥得的,你可以研究一下:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d54d427a-e7f7-40f6-ad1c-a788e18da463
(经我个人测试,直接复制Html代码如“ <img src="……">”到剪切板无效,粘贴时甚至可能引发目标程序崩溃,改动上述格式中的源文件路径也会引发同样结果-_-#)