//调用
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;
}
文章来源地址https://www.toymoban.com/news/detail-810453.html
文章来源:https://www.toymoban.com/news/detail-810453.html
到了这里,关于c#获取文件缩略图(位图),删除文件缩略图(位图)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!