【发布时间】:2013-09-19 23:58:05
【问题描述】:
我想获得透明的文件缩略图。
我有以下代码来实现它:
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;
}
我从这里混合了代码:
Is there a good way to convert between BitmapSource and Bitmap?
和
Load a WPF BitmapImage from a System.Drawing.Bitmap
通过这种方式,我将BitmapSource 转换为Bitmap,然后将Bitmap 转换为BitmapImage。
我很确定有办法将BitmapSource 直接转换为BitmapImage 同时保存透明度。
【问题讨论】:
标签: c# wpf bitmap bitmapimage bitmapsource