c# – 如何通过Drag’n’Droping在Winform中将图元文件转换为图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何通过Drag’n’Droping在Winform中将图元文件转换为图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1738字,纯文字阅读大概需要3分钟。
内容图文
我用C#中的framlework .NET 3.5开发了一个Winform应用程序.
我想允许用户从Word 2007中拖放图片.基本上用户打开docx,选择一张图片并将它们拖放到我的PictureBox中.
我已经使用桌面和Internet页面中的图片文件完成了相同的过程,但我不能解决我的Metafile问题.我做了很少的研究,但我没有找到解决我问题的任何解决方案.
以下是我在Drag& Drop事件中所做的事情:
private void PictureBox_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.MetafilePict)){
Image image = new Metafile((Stream)e.Data.GetData(DataFormats.MetafilePict));
}
}
我可以使用以下代码获取流:( Stream)e.Data.GetData(DataFormats.MetafilePict)但我不知道如何将其转换为Metafile或更好的Image对象.
如果您有任何想法或解决方案,我会很高兴看到它.
谢谢,
解决方法:
以下是Drag n Drop from Word(不适用于PowerPoint和Excel)的工作示例:
static Metafile GetMetafile(System.Windows.Forms.IDataObject obj)
{
var iobj = (System.Runtime.InteropServices.ComTypes.IDataObject)obj;
var etc = iobj.EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR.DATADIR_GET);
var pceltFetched = new int[1];
var fmtetc = new System.Runtime.InteropServices.ComTypes.FORMATETC[1];
while (0 == etc.Next(1, fmtetc, pceltFetched) && pceltFetched[0] == 1)
{
var et = fmtetc[0];
var fmt = DataFormats.GetFormat(et.cfFormat);
if (fmt.Name != "EnhancedMetafile")
{
continue;
}
System.Runtime.InteropServices.ComTypes.STGMEDIUM medium;
iobj.GetData(ref et, out medium);
return new Metafile(medium.unionmember, true);
}
return null;
}
private void Panel_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.EnhancedMetafile) & e.Data.GetDataPresent(DataFormats.MetafilePict))
{
Metafile meta = GetMetafile(e.Data);
Image image = meta;
}
}
在此之后,您可以使用image.Save保存图片,或者您可以在图片框或其他控件上使用它.
内容总结
以上是互联网集市为您收集整理的c# – 如何通过Drag’n’Droping在Winform中将图元文件转换为图像全部内容,希望文章能够帮你解决c# – 如何通过Drag’n’Droping在Winform中将图元文件转换为图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。