Ich habe ein Problem, wenn ich versuche, eine BitmapSource zu speichern. Ich bekomme immer einen Fehler in GDI + oder dass die Datei von einem anderen Prozess verwendet wird.Fehler beim Speichern von BitmapSource
Die Methode, um die Bitmap
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");
}
Bitmap zu Bitmap Umwandlung
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);
}
}
}
Die "Thumbnail" zu speichern, die stammt aus einer get-Eigenschaft verwendet wird
public BitmapSource Image
{
get { return new BitmapImage(new Uri(FilePath)); }
}
zu sparen Und das Miniaturbild der Datei wird auch in einer Ansicht verwendet. Ich bekomme es mit der 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;
}
Hat anyon eine Lösung?
Dank Wim
auf welcher Linie verwenden, genau Sie diese Ausnahme erhalten? –
Ein Bitmap Image ist ein Loader für die Datei, es verwaltet die Bildquelle in einem geöffneten Zustand. Wenn Sie das Caching deaktivieren, sollte es die Datenquelle nach dem Laden schließen – MikeT
Der Fehler tritt in der Bitmap.Save (...) auf –