c#获取文件缩略图(位图),删除文件缩略图(位图)

2024-01-08 15:30:48
//调用
	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://blog.csdn.net/u014018281/article/details/135456810
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。