在 Windows Phone 7 上从独立存储打开 JPEG 时出现问题 [英] Problem opening JPEG from Isolated Storage on Windows Phone 7

查看:27
本文介绍了在 Windows Phone 7 上从独立存储打开 JPEG 时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景

  1. 应用打开
  2. 检查独立存储中是否存在背景图像
  3. 如果没有,从网络下载,并将其保存到独立存储
  4. 从独立存储加载图像并将其设置为全景控件上的背景

问题

图像未在 GUI 中加载.. 当我检查从独立存储接收的字节数组时,它包含的字节数与最初写入的字节数相同,但图像未显示.

The image is not loaded in GUI.. When I'm inspecting the byte-array received from isolated storage, it contains the same amount of bytes as was written initially, but the image doesn't show up.

这是我目前用来尝试找出问题的一些测试代码:

Here's some test-code I'm currently using to try and figure out the problem:

using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!appStorage.FileExists(@"default.jpg"))
                {
                    BitmapImage bmp = sender as BitmapImage;
                    byte[] bytes = bmp.ConvertToBytes();
                    using (var inputfile = appStorage.CreateFile(@"default.jpg"))
                    {
                        inputfile.Write(bytes, 0, bytes.Length);
                    }
                }
                using (var isfs = appStorage.OpenFile(@"default.jpg", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(isfs);
                    MainPanorama.Background = new ImageBrush { Opacity = 0.4, Stretch = Stretch.None, ImageSource = bmp };
                }
            }

其中 sender 是从其他来源加载的图像我已经尝试将发件人设置为 MainPanorama 控件上的背景图像,并且效果很好.所以问题在于从隔离存储加载.

Where sender is a loaded image from some other source I've tried setting the sender as backgroundimage on the MainPanorama-control, and that works just fine. So the problem is in the loading from Isolated Storage.

有什么想法吗?

推荐答案

听起来这一定是时间问题或随机访问流的问题.

Sounds like this has to be an issue with timing or with random access to the stream.

你可以尝试的事情:

  1. 尝试将整个图像加载到内存数组 - MemoryStream - 然后在 SetSource 调用中使用它

  1. Try loading the entire image into an in memory array - a MemoryStream - and then use that in the SetSource call

尝试删除未使用的代码 - .ImageOpened 委托和 img = new Image() 调用

Try removing the unused code - the .ImageOpened delegate and the img = new Image() call

如果这些事情没有帮助,那么尝试在字节级别检查两个流.

if those things don't help then try checking the two streams at the byte level.

有关 1 的更多信息,请参阅如何从独立存储加载图像并在设备上显示它? - 请注意,这是 Microsoft 的官方支持示例,它将图像加载到内存中的 MemoryStream 中,然后在屏幕上的图像中使用它.

For more info on 1, see How Do I Load an Image from Isolated Storage and Display it on the Device? - note that this is Microsoft's support official sample and it loads the image into an in memory MemoryStream before using it in the on-screen Image.

微软的代码:

// The image will be read from isolated storage into the following byte array
        byte [] data;

        // Read the entire image in one go into a byte array
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            // Open the file - error handling omitted for brevity
                                 // Note: If the image does not exist in isolated storage the following exception will be generated:
            // System.IO.IsolatedStorage.IsolatedStorageException was unhandled
            // Message=Operation not permitted on IsolatedStorageFileStream
            using (IsolatedStorageFileStream isfs = isf.OpenFile("WP7Logo.png", 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);

        // Create an image UI element – Note: this could be declared in the XAML instead
        Image image = new Image();

        // Set size of image to bitmap size for this demonstration
        image.Height = bi.PixelHeight;
        image.Width = bi.PixelWidth;

        // Assign the bitmap image to the image’s source
        image.Source = bi;

        // Add the image to the grid in order to display the bit map
        ContentPanel.Children.Add(image);

请报告修复它的原因.

这篇关于在 Windows Phone 7 上从独立存储打开 JPEG 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆