繁体   English   中英

字符串到位图转换器Windows Phone

[英]String to bitmap converters windows phone

嗨,我的程序从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.Windows.ni.dll中发生类型为'System.OutOfMemoryException'的异常,但未在用户代码中处理

我认为您需要BeginInitEndInit

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

暂无
暂无

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

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