1
我尝试保存BitmapSource时遇到问题。我也在GDI +中出现错误,或者该文件正在被另一个进程使用。保存BitmapSource时出错
的梅索德到BitmapImage的保存
protected override void Save()
{
Bitmap bitmap = Thumbnail.ToBitmap();
if (Angle % 360 == 0)
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
else if (Angle % 270 == 0)
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
else if (Angle % 180 == 0)
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
else if (Angle % 90 == 0)
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
bitmap.Save(Objects[0].FilePath);
Objects[0].RaisePropertyChanged("Thumbnail");
}
的BitmapSource为位图转换
public static Bitmap ToBitmap(this BitmapSource bitmapsource)
{
using (MemoryStream stream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(stream);
using (var tempBitmap = new Bitmap(stream))
{
return new Bitmap(tempBitmap);
}
}
}
用来保存来自一个get属性
public BitmapSource Image
{
get { return new BitmapImage(new Uri(FilePath)); }
}
“缩略图”该文件的缩略图也用于视图中。我用windows shell API获得它。
public static BitmapSource GetThumbnail(this string This, BitmapSize size = BitmapSize.Large)
{
if (ShellObject.IsPlatformSupported)
{
ShellObject shellitem = ShellObject.FromParsingName(This);
try
{
if (size == BitmapSize.Small)
return shellitem.Thumbnail.SmallBitmap.ToBitmapSource();
else if (size == BitmapSize.Medium)
return shellitem.Thumbnail.MediumBitmap.ToBitmapSource();
else if (size == BitmapSize.Large)
return shellitem.Thumbnail.LargeBitmap.ToBitmapSource();
else
return shellitem.Thumbnail.ExtraLargeBitmap.ToBitmapSource(); ;
}
catch (Exception)
{
return null;
}
}
return null;
}
是否有任何解决方案?
感谢 维姆
正是你得到这个例外? –
位图图像是文件的加载器,它将图像源保持在打开状态,如果禁用了缓存,那么它应该在加载后关闭数据源 – MikeT
该错误发生在bitmap.Save(...) –