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

3 篇文章 0 订阅
本文介绍了如何使用C#中的DllImport和ShellObject等技术,实现删除文件路径对应的缩略图,并从缩略图获取自定义BitmapSource。方法涉及到DeleteObject函数和位图处理。
摘要由CSDN通过智能技术生成
//调用
	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;
    }




  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值