获得透明度的文件缩略图图像[英] Get file Thumbnail image with transparency

本文是小编为大家收集整理的关于获得透明度的文件缩略图图像的处理方法,想解了获得透明度的文件缩略图图像的问题怎么解决?获得透明度的文件缩略图图像问题的解决办法?获得透明度的文件缩略图图像问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我想获得透明度的文件的缩略图.
我有以下代码来实现它:

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;
}

我从这里混合了代码:
是否有一个很好的方法可以在BitMapsource和BitMapsource和BitMapsource和位图?

从system.bit.bitmmap

以这种方式,我将BitmapSource转换为位图,然后将位图掩盖到BitmapImage. 我很确定有一种方法可以直接掩盖BitmapImage ,同时保存透明度.

.

推荐答案

您需要将BitmapSource编码为BitmapImage,您可以在此示例中选择所需的任何编码器,我使用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;
}

其他推荐答案

您是否尝试过:System.Drawing.Imaging.PixelFormat.Format32bppArgb(没有 format32-p-argb之间的p)

msdn:

Format32bppArgb - >指定格式为每个像素的32位; 8位用于α,红色,绿色和蓝色组件.

Format32bppPArgb - >指定格式为每个像素的32位; 8位用于α,红色,绿色和蓝色组件. 根据Alpha组件,红色,绿色和蓝色组件是的.

本文地址:https://www.itbaoku.cn/post/335378.html

问题描述

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;
}

I mixed the codes from here:
Is there a good way to convert between BitmapSource and Bitmap?
and
Load a WPF BitmapImage from a System.Drawing.Bitmap

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.

推荐答案

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

Example:

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;
}

其他推荐答案

Did you try: System.Drawing.Imaging.PixelFormat.Format32bppArgb (without the P between Format32-P-Argb)

MSDN:

Format32bppArgb -> Specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components.

Format32bppPArgb -> Specifies that the format is 32 bits per pixel; 8 bits each are used for the alpha, red, green, and blue components. The red, green, and blue components are premultiplied, according to the alpha component.

查看更多