如何在 WPF 中显示 Windows 文件图标?
目前,我通过调用 SHGetFileInfo 获取本机图标。然后,我使用以下代码将其转换为位图。位图最终以 WPF 表单显示。
有没有更快的方法来做同样的事情?
try
{
using (Icon i = Icon.FromHandle(shinfo.hIcon))
{
Bitmap bmp = i.ToBitmap();
MemoryStream strm = new MemoryStream();
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bmpImage = new BitmapImage();
bmpImage.BeginInit();
strm.Seek(0, SeekOrigin.Begin);
bmpImage.StreamSource = strm;
bmpImage.EndInit();
return bmpImage;
}
}
finally
{
Win32.DestroyIcon(hImgLarge);
}
Currently I'm getting a native icon by calling SHGetFileInfo. Then, I'm converting it to a bitmap using the following code. The Bitmap eventually gets displayed in the WPF form.
Is there a faster way to do the same thing?
try
{
using (Icon i = Icon.FromHandle(shinfo.hIcon))
{
Bitmap bmp = i.ToBitmap();
MemoryStream strm = new MemoryStream();
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bmpImage = new BitmapImage();
bmpImage.BeginInit();
strm.Seek(0, SeekOrigin.Begin);
bmpImage.StreamSource = strm;
bmpImage.EndInit();
return bmpImage;
}
}
finally
{
Win32.DestroyIcon(hImgLarge);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
怎么样:
How about something like:
合并Krzysztof Kowalczyk 答案通过一些谷歌搜索,我编了这个:
方法:
和互操作类:
来源
Combining Krzysztof Kowalczyk answer with some googling, i made up this:
Method:
and Interop class:
source
托马斯的代码还可以进一步简化。这是带有额外错误检查的完整代码:
区别在于:
Thomas's code could be simplified even more. Here's the full code with additional error checking:
The difference is:
我相信有更简单(更易于管理)的方法来解决这里强调的问题。
http://www.pchenry.com/Home/tabid/ 36/EntryID/193/Default.aspx
解决方案的关键就在这里。
I belive there's simpler (more managed) way of solving this hilighted here.
http://www.pchenry.com/Home/tabid/36/EntryID/193/Default.aspx
The crux is of the solution is here.