//调用
ImgFunction.deleteImg(gm.Path); //删除缩略图
BitmapSource thumb = ImgFunction.getImg(gm.Path); //获取缩略图
pop3grid.Background = new ImageBrush
{
ImageSource = thumb
};
//这里是方法
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
//path是文件路径
public static BitmapSource deleteImg(string path)
{
try
{
if (!File.Exists(path))
{
return new BitmapImage(new Uri("pack://application:,,,/Images/null.png"));
}
ShellObject so = ShellFileSystemFolder.FromParsingName(path);
Bitmap bm = so.Thumbnail.Bitmap;
bm.MakeTransparent(System.Drawing.Color.Black);
BitmapSource img;
IntPtr hBitmap;
hBitmap = bm.GetHbitmap();
DeleteObject(hBitmap); //删除缩略图
}
catch (Exception e)
{
}
return null;
}
public static BitmapSource getImg(string path)
{
if (!File.Exists(path))
{
return new BitmapImage(new Uri("pack://application:,,,/Images/null.png"));
}
ShellObject so = ShellFileSystemFolder.FromParsingName(path);
Bitmap bm = so.Thumbnail.Bitmap;
bm.MakeTransparent(System.Drawing.Color.Black);
BitmapSource img;
IntPtr hBitmap;
hBitmap = bm.GetHbitmap();
img = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return img;
}
c#获取文件缩略图(位图),删除文件缩略图(位图)
本文介绍了如何使用C#中的DllImport和ShellObject等技术,实现删除文件路径对应的缩略图,并从缩略图获取自定义BitmapSource。方法涉及到DeleteObject函数和位图处理。
摘要由CSDN通过智能技术生成