【问题标题】:Delete Storage File (Image) C# metro apps删除存储文件(图片)C# Metro 应用程序
【发布时间】:2012-06-19 04:59:14
【问题描述】:

我有一个问题,在我的应用程序中使用或显示保存在我的应用程序本地文件夹中的 Gridview 中的图像,我想通过选择它们来删除这些图像,但问题是这些图像正在使用中并且在 Windows 中当任何东西(文件或图像)打开或使用时,不应从驱动器中删除。我想从硬盘中删除图像。 m 通过BitmapImage 中的URI 路径在GridView 中使用或访问这些图像。像这样

私有 ImageSource _image = null; this._image = new BitmapImage(new Uri(new Uri("ms-appdata:///local/RecentImages/"), this._imagePath));

而我试图删除 请告诉我如何停止或处理 GridView 和 StorageFile 之间的此链接 或者如何从存储设备中删除图像??

【问题讨论】:

  • 打开图片时,读取图片数据后,关闭图片文件流。
  • 如何关闭图像流先生?
  • 这些图像在我的 Gridview 中用作最近的活动,并且每次都在我的主页上打开
  • 和 m 使用 URI 路径获取这些图像不是我的流先生。
  • 请检查我下面的代码。我不确定以下是否可以稳定地与 Metro 一起使用,但它解决了我在Newgen 上的问题。

标签: c# windows-8 microsoft-metro


【解决方案1】:

我假设没有其他应用同时加载相同的图像。 这是一种在将图像文件流加载到内存后关闭它的方法。

    /// <summary>
    /// Gets the bitmap.
    /// </summary>
    /// <param name="path">The path.</param>
    /// <returns></returns>
    public static BitmapSource GetBitmap(string path)
    {
        if (!File.Exists(path)) return null;

        MemoryStream ms = new MemoryStream();
        BitmapImage bi = new BitmapImage();
        byte[] bytArray = File.ReadAllBytes(path);
        ms.Write(bytArray, 0, bytArray.Length); ms.Position = 0;
        bi.BeginInit();
        bi.StreamSource = ms;
        bi.EndInit();
        return bi;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多