首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从独立存储中读取和绑定图像时的System.OutOfMemoryException

从独立存储中读取和绑定图像时的System.OutOfMemoryException
EN

Stack Overflow用户
提问于 2013-04-24 15:08:58
回答 4查看 755关注 0票数 8

这是我用来在XAML中绑定图像的代码

代码语言:javascript
运行
复制
               <Border  toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3">
                        <Grid  Height="350" Width="400" Margin="70,0,70,0" x:Name="Container1">
                            <Grid.Background>
                                <ImageBrush ImageSource="{Binding ImageCollection[0]}" Stretch="Uniform" AlignmentX="Left" AlignmentY="Center"/>
                            </Grid.Background>
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <i:InvokeCommandAction Command="{Binding ImageTapCommand}" CommandParameter="CONTAINER0"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Grid>
                    </Border>

同样,我使用4边框来显示我最近的图片。

在我的ViewModel中,我使用下面的方法从独立存储中读取图像。

代码语言:javascript
运行
复制
 public Stream GetFileStream(string filename, ImageLocation location)
    {
        try
        {
            lock (SyncLock)
            {
                if (location == ImageLocation.RecentImage)
                {
                    filename = Constants.IsoRecentImage + @"\" + filename;
                }
                using (var iSf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!iSf.FileExists(filename)) return null;
                    var fs = iSf.OpenFile(filename, FileMode.Open, FileAccess.Read);
                    return fs;
                }
            }

        }
        catch (Exception ex)
        {
                 return null;
        }

    }

在获得流之后,我将使用下面写的方法四来构建UI绑定的WritableBitmap

代码语言:javascript
运行
复制
     private WriteableBitmap BuildImage(Stream imageStream)
    {
        using (imageStream)
        {
            var image = new BitmapImage();
            image.SetSource(imageStream);
            return new WriteableBitmap(image);
        }
    }

在这种情况下,我的问题是在导航到我的页面两三次之后。当我使用“image.SetSource(imageStream);”方法时,应用程序在BuildImage()方法上崩溃。我尝试了许多替代方案,但都失败了。我得到的异常是"System.OutOfMemoryException“

我尝试了图像控件,而不是图像画笔。

我尝试了位图,而不是WritableBitmap等,但结果是一样的。

如果我使用小图片,应用程序的崩溃率将会降低。但通过摄像头拍摄的图像崩溃率很高。

在过去的一周里,我一直在尝试解决这个问题,但没有找到任何替代方案来解决这个问题。

我找到了一个讨论类似问题的link,但没有得到很多人来解决这个问题

EN

Stack Overflow用户

发布于 2013-05-06 20:01:09

在使用IsolatedStorageFileStreamIsolatedStorageFile之后,您是否正在对它们进行重置/处置?

票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16185557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档