我正在为我工作的公司创建UWP应用,其中一部分工作是上载证件照片。当用户单击以上传选择器时,将显示选择器,它将图像加载到图像元素。这可以正常工作,问题是当提取具有附加照片的现有记录时。该文件存储为byte [],我在.Net Web应用程序上使用的代码在这里不起作用。即使在无数网站上逐个阅读一周后,我也无法正常工作。
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
photo.ID = reader.GetGuid(0);
photo.AttachmentType = reader.GetString(1);
photo.AttachmentSize = reader.GetInt32(2);
photo.Attachment = (byte[])reader.GetValue(3);
}
}
RemoveBtn.Visibility = Visibility.Visible;
}
}
}
}
}
catch (Exception eSql)
{
var err = eSql.Message.ToString();
}
if (photo.Attachment != null && photo.Attachment.Length > 0)
{
//StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
//StorageFile file = await folder.CreateFileAsync($"tempImg{photo.AttachmentType}");
//string path = @"D:\DOT2";
//string fileName = "tempImg.png";
//string pathStr = Path.Combine(path, fileName);
using (Stream stream = new MemoryStream())
{
await stream.WriteAsync(photo.Attachment, 0, photo.Attachment.Length);
using (IRandomAccessStream fileStream = stream.AsRandomAccessStream())
{
BitmapImage img = new BitmapImage();
await img.SetSourceAsync(fileStream); //Error Occurs here.
CIPhoto.Source = img;
}
}
UploadBtn.Visibility = Visibility.Collapsed;
RemoveBtn.Visibility = Visibility.Visible;
}
预期结果是将byte []转换为可以设置为Image UI元素的BitmapImageSource。我遇到一个又一个错误,当前我收到一条错误消息:“试图读取或写入受保护的内存。这通常表明其他内存已损坏。评论部分告诉我,我被拒绝访问。我被卡住了,请帮忙。