C# 获取具有透明度的文件缩略图图像
我想获得透明的文件缩略图。C# 获取具有透明度的文件缩略图图像,c#,wpf,bitmap,bitmapimage,bitmapsource,C#,Wpf,Bitmap,Bitmapimage,Bitmapsource,我想获得透明的文件缩略图。 我有以下代码来实现它: BitmapImage GetThumbnail(string filePath) { ShellFile shellFile = ShellFile.FromFilePath(filePath); BitmapSource shellThumb = shellFile.Thumbnail.ExtraLargeBitmapSource; Bitmap bmp = new Bitmap(shellThumb.PixelW
我有以下代码来实现它:
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
转换为位图,然后将位图转换为BitmapImage
。
我很确定有一种方法可以在保存透明度时将
BitmapSource
直接转换为BitmapImage
,您是否尝试过:System.Drawing.Imaging.PixelFormat.Format32bppArgb
(不带Format32-p-Argb之间的p)
MSDN:
Format32bppArgb
->指定格式为每像素32位;每个8位用于alpha、红色、绿色和蓝色组件
Format32bppPArgb
->指定格式为每像素32位;每个8位用于alpha、红色、绿色和蓝色组件根据alpha分量,红色、绿色和蓝色分量是预乘的。您是否尝试过:系统、绘图、成像、像素格式、格式32bppargb
(不带格式32-p-Argb之间的p)
MSDN:
Format32bppArgb
->指定格式为每像素32位;每个8位用于alpha、红色、绿色和蓝色组件
Format32bppPArgb
->指定格式为每像素32位;每个8位用于alpha、红色、绿色和蓝色组件根据alpha分量,红色、绿色和蓝色分量是预乘的。您需要将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;
}
您需要将
位图源代码
编码为位图图像
,您可以在本例中选择所需的任何编码器我使用pngbitmapcoder
例如:
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;
}
工作原理相同,但这与我的问题有什么关系?我的函数确实给了我一个透明的缩略图,但是我分两步来做(正如我所说的),我很确定我可以在一步中完成同样的工作,但是它与我的问题有什么关系呢?我的函数确实给了我一个透明的缩略图,但我分两步来做(如我所说),我很确定我可以一步完成它。它可以工作,但只有当我从memoryStream中删除“using”时它才能工作,知道为什么吗?哦,我的糟糕,这是因为using处理了流,我没有测试代码:(,我会更新它工作,但它只在我从memoryStream中删除“using”时工作,知道为什么吗?哦,我的错,这是因为using处理了流,我没有测试代码:(,我会更新。)