我正在将从数据库检索到的字节数组转换为图像。它被转换 - 我认为 - 成功但不在图像框中显示图像。这是我正在实施的代码。如果有人帮忙,请。
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Service1Client GetNameFromService = new Service1Client();
GetNameRequest NameRequest = new GetNameRequest();
NameRequest.username = e.Parameter.ToString();
GetNameResponse NameResponse = await GetNameFromService.GetNameAsync(NameRequest);
lblName.Text = NameResponse.GetNameResult.NAME;
byte[] IMG = NameResponse.GetNameResult.IMAGE;
using (InMemoryRandomAccessStream raStream = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(raStream))
{
// Write the bytes to the stream
writer.WriteBytes(IMG);
// Store the bytes to the MemoryStream
await writer.StoreAsync();
// Not necessary, but do it anyway
await writer.FlushAsync();
// Detach from the Memory stream so we don't close it
writer.DetachStream();
}
raStream.Seek(0);
BitmapImage bitMapImage = new BitmapImage();
bitMapImage.SetSource(raStream);
imgUser.Source = bitMapImage;
}
//var stream = new InMemoryRandomAccessStream();
//await stream.WriteAsync(IMG.AsBuffer());
//BitmapImage image = new BitmapImage();
//image.SetSource(stream);
//imgUser.Source = image;
ID = NameResponse.GetNameResult.id;
await GetNameFromService.CloseAsync();
}`
通过网络服务检索图片。 Web服务方法的代码如下,
public GetDetail GetDetail(string username)
{
StoryMakerConnection.Open();
GetDetail GD = new GetDetail();
SqlCommand GetIDCommand = new SqlCommand("SELECT UserId FROM LoginInfo WHERE UserName LIKE '" + username + "'", StoryMakerConnection);
SqlDataReader IDReader = GetIDCommand.ExecuteReader();
while (IDReader.Read())
{
GD.ID = Convert.ToInt16(IDReader["UserId"].ToString());
}
IDReader.Close();
SqlCommand GetNameCommand = new SqlCommand("SELECT UserFirstName, UserLastName, Picture FROM UserInfo WHERE UserId = " + GD.ID, StoryMakerConnection);
SqlDataReader NameReader = GetNameCommand.ExecuteReader();
while (NameReader.Read())
{
GD.FullName = NameReader["UserFirstName"].ToString() + " " + NameReader["UserLastName"].ToString();
GD.img = System.Text.Encoding.ASCII.GetBytes(NameReader["Picture"].ToString());
}
return GD;
}