当前位置: 首页 > 编程语言 > C#

在Windows Phone 8.1中加载,显示,转换来自字节数组(数据库)的图像分享

时间:2023-04-10 19:53:44 C#

C#学习教程:在WindowsPhone8.1中从字节数组(数据库)中加载、显示、转换图像WindowsPhone8.1中的数据库。图像数据作为字节数组加载(检查-加载正常)。通过指定urisource显示图像效果很好。图片img=newImage();img.Source=newBitmapImage(){UriSource=newUri("http://www.example.com/1.jpg")};rootgrid.Children.Add(img);但是当字节数组(图像)转换为BitmapImage时-不显示任何内容。到目前为止我发现的唯一无异常示例是:publicBitmapImageConvertToBitmapImage(byte[]image){varbitmapImage=newBitmapImage();varmemoryStream=newMemoryStream(image);memoryStream.CopyToAsync(ras.AsStreamForWrite());bitmapImage.SetSourceAsync(ras);返回位图图像;}图片img=newImage();img.Source=ConvertToBitmapImage(picturebytearray);rootgrid.Children.Add(img);.Microsoft的文档仅包括通过从内部存储打开文件获得的流内图像加载示例。但我需要加载图像,即保存在sqlite数据库中。图像数据为jpeg格式。编辑:基于freshbm解决方案的工作代码:publicasyncTaskConvertToBitmapImage(byte[]image){BitmapImagebitmapimage=null;使用(InMemoryRandomAccessStreamms=newInMemoryRandomAccessStream()){使用(DataWriterwriter=newDataWriter(ms.GetOutputStreamAt(0))){writer.WriteBytes((byte[])image);等待writer.StoreAsync();}bitmapimage=newBitmapImage();位图图像.SetSource(ms);}返回位图图像;然后在构造函数中,你可以使用:img.Source=ConvertToBitmapImage(imagebytearray).Result;否则img.Source=awaitConvertToBitmapImage(imagebytearray);您可以尝试这样将byte[]转换为BitmapImage:)文件字节);writer.StoreAsync().GetResults();}varimage=newBitmapImage();图片.SetSource(ms);在这里找到:http://www.codeproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an我正在在使用它从sqlite数据库读取byte[]并将其绑定到页面上的图像之后对于您的代码,尝试为异步函数添加一个等待:publicasyncTaskConvertToBitmapImage(byte[]image){InMemoryRandomAccessStreamras=newInMemoryRandomAccessStream();varbitmapImage=newBitmapImage();varmemoryStream=newMemoryStream(image);等待memoryStream.CopyToAsync(ras.AsStreamForWrite());等待bitmapImage.SetSourceAsync(ras);返回位图图像;();img.Source=awaitConvertToBitmapImage(picturebytearray);rootgrid.Children.Add(img);我不擅长异步编程,但我认为这会起作用。以上就是C#学习教程:在WindowsPhone8.1中从字节数组(数据库)加载、显示、转换图片共享。如果对大家有用,需要进一步了解C#学习教程,还望大家多加关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: