这是我用于在XAML中绑定图像的代码
<Border toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3">
<Grid Height="350" Width="400" Margin="70,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中,我使用以下方法从隔离存储中读取图像.
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
private WriteableBitmap BuildImage(Stream imagestream)
{
using (imagestream)
{
var image = new BitmapImage();
image.SetSource(imagestream);
return new WriteableBitmap(image);
}
}
在这种情况下,我的问题是在我的页面导航和导出两到三次之后.应用程序在BuildImage()方法崩溃,我正在使用“image.SetSource(imagestream);”方法.我尝试了很多替代品,但失败了我得到的例外是“System.OutOfMemoryException”
我尝试使用Image控件而不是Image画笔.
我尝试过Bitmap而不是WritableBitmap等,但结果是一样的.
如果我使用小图片,应用程序崩溃率将降低.但是通过相机拍摄的图像的崩溃率很高.
我正在尝试解决这个问题的最后一周,但没有找到任何替代方案来解决这个问题.
我找到了一个谈论类似问题的link但是没有多少人来解决这个问题
Try this,var bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); bitmapImage.CreateOptions = BitmapCreateOptions.None; var bmp = new WriteableBitmap((BitmapSource) bitmapImage); bitmapImage.UriSource = (Uri) null; return bmp;