最后发现初始化BitmapImage可以通过byte[]进行,于是只能通过将png文件读成byte[],再进行BitmapImage的初始化,就没有问题了
// Read byte[] from png file
BinaryReader binReader = new BinaryReader(File.Open(filePath, FileMode.Open));
FileInfo fileInfo = new FileInfor(filePath);
byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
binReader.Close();
// Init bitmap
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(bytes);
// Read byte[] from png file
BinaryReader binReader = new BinaryReader(File.Open(filePath, FileMode.Open));
FileInfo fileInfo = new FileInfor(filePath);
byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
binReader.Close();
// Init bitmap
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(bytes);
bitmap.EndInit();
image.Dispose();
System.GC.SuppressFinalize(image);
BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;
本文介绍了一种通过将PNG文件转换为byte[]数组来初始化BitmapImage的方法,解决了直接加载文件路径可能导致的问题,并展示了具体的实现代码。
1万+

被折叠的 条评论
为什么被折叠?



