更新时间:2023-10-10 17:50:40
听起来这一定是时间问题或随机访问流的问题.
Sounds like this has to be an issue with timing or with random access to the stream.
你可以尝试的事情:
尝试将整个图像加载到内存数组 - MemoryStream - 然后在 SetSource 调用中使用它
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);
请报告修复它的原因.