获取透明度文件缩略图

获取透明度文件缩略图

本文介绍了获取透明度文件缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得文件的透明度缩略图。

我有以下的code来实现它:

I want to get file's thumbnail with transparency.
I have the following code to achieve it:

BitmapImage GetThumbnail(string filePath)
{
    ShellFile shellFile = ShellFile.FromFilePath(filePath);
    BitmapSource shellThumb = shellFile.Thumbnail.ExtraLargeBitmapSource;

    Bitmap bmp = new Bitmap(shellThumb.PixelWidth, shellThumb.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
    shellThumb.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
    bmp.UnlockBits(data);

    MemoryStream ms = new MemoryStream();
    bmp.Save(ms, ImageFormat.Png);
    ms.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.CacheOption = BitmapCacheOption.None;
    bi.EndInit();

    return bi;
}

我是混合codeS从这里:

Is有一个好办法的BitmapSource和位图之间的转换?



Load一个WPF BitmapImage的从System.Drawing.Bitmap

通过这种方式,我转换的BitmapSource 为位图,然后我的隐蔽位图的BitmapImage
我是pretty肯定有一个方法来变相的BitmapSource 直接的BitmapImage 同时节省了透明的

With this way, I convert BitmapSource to Bitmap, then I covert the Bitmap to BitmapImage.I am pretty sure there's a way to covert BitmapSource directly to BitmapImage while saving the transparency.

推荐答案

您将需要连接code中的的BitmapSource 的BitmapImage ,你可以选择你在这个例子中,我想使用的任何连接codeR PngBitmapEn codeR

You will need to encode the BitmapSource to a BitmapImage, you can choose any encoder you want in this example I use PngBitmapEncoder

例如:

    private BitmapImage GetThumbnail(string filePath)
    {
        ShellFile shellFile = ShellFile.FromFilePath(filePath);
        BitmapSource shellThumb = shellFile.Thumbnail.ExtraLargeBitmapSource;

        BitmapImage bImg = new BitmapImage();
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        var memoryStream = new MemoryStream();
        encoder.Frames.Add(BitmapFrame.Create(shellThumb));
        encoder.Save(memoryStream);
        bImg.BeginInit();
        bImg.StreamSource = memoryStream;
        bImg.EndInit();
        return bImg;
    }

这篇关于获取透明度文件缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:38