【发布时间】:2010-07-03 09:54:54
【问题描述】:
我的应用程序中有一个数据类,它维护一组表示 JPEG 图像的字节数组。定义为:
private ArrayList FrameList = new ArrayList();
我的 Image 对象在渲染一个空白页面时遇到了一些麻烦(并且也花了不少时间来完成它)。当我插入具有 2K 内存字节数组 (byte x[] = { lots of hex values };) 的空白图像时:
FrameList.Insert(currFrame, x);
然后在其上导入 JPEG 文件:
byte[] bytes = File.ReadAllBytes(fspec);
FrameList[currFrame] = bytes;
数组被正确读入内存并存储在 ArrayList 中(由调试器确认)。
但是,我有一个函数来获取图像:
public BitmapImage getCurrPicture()
{
MemoryStream strm;
BitmapImage bmp = new BitmapImage();
strm = new MemoryStream((byte[])FrameList[currFrame-1]);
bmp.CacheOption = BitmapCacheOption.None;
bmp.BeginInit();
bmp.StreamSource = strm;
bmp.EndInit();
strm.Close();
return bmp;
}
被称为:
imgPicB.Source = data.getCurrPicture();
它并不总是呈现。
imgPicB 在我的 XAML 中定义为:
<Image x:Name="imgPicB"
Width="400"
Height="300"
Stretch="Fill"
VerticalAlignment="Top" />
有趣的是,如果我使用完全相同的 JPEG 设置源并直接将源设置为文件 URI,它会呈现良好。
在 WPF 中使用内存中的 JPEG 图像有什么问题吗?从文件加载时是否执行了一些额外的智能操作(例如自动检测图像类型)?
【问题讨论】: