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

//调用
	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#,wpf,c#,java,服务器)