【问题标题】:String to bitmap converters windows phone字符串到位图转换器 windows phone
【发布时间】:2014-05-23 17:19:45
【问题描述】:

大家好,我的程序从IsolatedStorage 文件中获取图像,然后将其显示在我的仪表板上。它工作正常,直到它到达仪表板上的第四张图片,然后它抛出一个OutOfMemoryException。下面是代码:

public class StringToBitmapConverter : IValueConverter
{
    public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
    {
        byte[] data;

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isf.FileExists(value.ToString()))
            {
                using (IsolatedStorageFileStream isfs = isf.OpenFile(value.ToString(), FileMode.Open, FileAccess.Read))
                {
                    // Allocate an array large enough for the entire file 
                    data = new byte[isfs.Length];
                    // Read the entire file and then close it 
                    isfs.Read(data, 0, data.Length);
                    isfs.Close();
                }


                // Create memory stream and bitmap 
                MemoryStream ms = new MemoryStream(data);
                BitmapImage bi = new BitmapImage();

                // Set bitmap source to memory stream 

                bi.SetSource(ms);

                ms.Dispose();

                return bi;


            }

        }

        return null;
    }

    public object ConvertBack(object value, Type TargetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

请你告诉我代码有什么问题,以便我可以解决他的问题?

这是我在应用崩溃时遇到的错误:

“System.OutOfMemoryException”类型的异常发生在 System.Windows.ni.dll 但未在用户代码中处理

【问题讨论】:

    标签: c# exception memory bitmap converter


    【解决方案1】:

    我认为你需要BeginInitEndInit

    using (MemoryStream ms = new MemoryStream(data))
    {
        BitmapImage bi = new BitmapImage();
        bi.BeginInit();
        bi.StreamSource = ms;
        bi.EndInit();
    }
    

    【讨论】:

    • 我需要命名空间还是在任何地方声明该方法,因为它给了我一个语法错误。
    • 糟糕,我认为是 StreamSource 而不是 SetSource(已更新)。
    • 当我使用它时,这是我得到的错误:错误 1 ​​'System.Windows.Media.Imaging.BitmapImage' 不包含 'BeginInit' 的定义并且没有扩展方法 'BeginInit' 接受可以找到“System.Windows.Media.Imaging.BitmapImage”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
    • 谢谢,我只是不知道为什么它不起作用。但是 tanx 虽然。我会更深入地研究它,也许我做错了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2010-11-24
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多