I am creating a UWP app for the company I work for part of the process is to upload an ID Photo. When the user clicks to upload a picker displays and it loads the image to an Image Element. This works fine, the issue is when the pull an existing record that has a photo attached already. The file is stored as a byte[], the code I use on .Net web apps does not work here. I can not get it to work even after a week of reading post after post on countless sites.
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;
}
The expected result is to convert the byte[] to a BitmapImageSource that i can set to an Image UI Element. I get error after error, currently i get an error saying : 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'. The commented section tells me that i am denied access. I am stuck, please help.