如何显示在WPF Windows文件图标? [英] How do I display a Windows file icon in WPF?
本文介绍了如何显示在WPF Windows文件图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我通过调用的SHGetFileInfo得到一个本机的图标。于是,我就用下面的代码转换为位图。位图最终被显示在WPF的形式。
有没有更快的方式做同样的事情?
试
{
使用(图标I = Icon.FromHandle(shinfo.hIcon))
{
BMP位图= i.ToBitmap( );
MemoryStream的STRM =新的MemoryStream();
bmp.Save(STRM,System.Drawing.Imaging.ImageFormat.Png);
的BitmapImage bmpImage =新的BitmapImage();
bmpImage.BeginInit();
strm.Seek(0,SeekOrigin.Begin);
bmpImage.StreamSource = STRM;
bmpImage.EndInit();
返回bmpImage;
}
}
终于
{
Win32.DestroyIcon(hImgLarge);
}
解决方案
使用System.Windows.Interop;
...
使用(图标I = Icon.FromHandle(shinfo.hIcon))
{
ImageSource的IMG =影像.CreateBitmapSourceFromHIcon(
i.Handle,
新Int32Rect(0,0,i.Width,i.Height)
BitmapSizeOptions.FromEmptyOptions());
}
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);
}
解决方案
using System.Windows.Interop;
...
using (Icon i = Icon.FromHandle(shinfo.hIcon))
{
ImageSource img = Imaging.CreateBitmapSourceFromHIcon(
i.Handle,
new Int32Rect(0,0,i.Width, i.Height),
BitmapSizeOptions.FromEmptyOptions());
}
这篇关于如何显示在WPF Windows文件图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文