简体   繁体   English

保存BitmapSource时出错

[英]Error when saving BitmapSource

I have a problem when I try to save a BitmapSource. 我尝试保存BitmapSource时遇到问题。 I alsways get an error in GDI+ or that the file is in use by another proces. 我总是在GDI +中遇到错误,或者该文件正由另一个进程使用。

The methode to save the bitmapimage 保存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 BitmapSource到Bitmap的转换

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 用于保存的“缩略图”来自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. 我用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 谢谢Wim

尝试使用Application.current.dispatcher

Application.current.dispatcher(()=>Save());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM