I have a problem when I try to save a BitmapSource. I alsways get an error in GDI+ or that the file is in use by another proces.
The methode to save the 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 to Bitmap conversion
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);
}
}
}
The "Thumbnail" that is used to save comes from a get-property
public BitmapSource Image
{
get { return new BitmapImage(new Uri(FilePath)); }
}
And the thumbnail of the file is also used in a view. I get it with the 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;
}
Does anyon has a solution?
Thanks Wim