在UWP中将WriteableBitmap图像转换为数组 [英] Converting a WriteableBitmap image ToArray in UWP
问题描述
我正在从azure服务上的Blob存储访问图像.我返回图像的uri
,然后使用HttpClient尝试下载它. uri
正确无误.
I am accessing an image from a blob storage on my azure service. I am returning the uri
for the image and then uses HttpClient to try and download it. The uri
is verified to be correct.
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(new Uri(((App)Application.Current).results.statsInformation.ImageBlob.ImageUri, UriKind.RelativeOrAbsolute));
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
using (var stream = await response.Content.ReadAsStreamAsync())
{
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
memStream.Seek(0, SeekOrigin.Begin);
myOnlineImage.SetSource(memStream.AsRandomAccessStream());
}
}
}
}
catch (Exception)
{
throw;
}
}
来自服务器的图像存储在变量myOnlineImage
中.然后,我想使用myOnlineImage.PixelBuffer.ToArray();
提取像素信息.这是因为未正确下载图像吗?谁能帮我解决这个问题的选项?
The image from the server is stored in the variable myOnlineImage
. I then want to extract the pixel information using myOnlineImage.PixelBuffer.ToArray();
. Is this because the image has not been downloaded correctly? Can anyone help me with options to solve this?
我收到的异常是:
例外
消息值不能为空.\ r \ n参数名称:来源"
Message "Value cannot be null.\r\nParameter name: source"
StackTrace,位于System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr源,对象目标,Int32 startIndex,Int32长度)\ r \ n,位于System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer源,UInt32 sourceIndex ,位于System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer源,UInt32源索引,Int32计数)\ r \ n,位于System.Runtime.InteropServices.WindowsRuntime的Byte []目标,Int32 destinationIndex,Int32计数) .WindowsRuntimeBufferExtensions.ToArray(IBuffer源)\ r \ n位于Stonegaard_endless_runner.MainPage.d__7.MoveNext()"
StackTrace " at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(IBuffer source, UInt32 sourceIndex, Byte[] destination, Int32 destinationIndex, Int32 count)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source, UInt32 sourceIndex, Int32 count)\r\n at System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(IBuffer source)\r\n at Stonegaard_endless_runner.MainPage.d__7.MoveNext()"
额外
我检查了我对该图像具有线程访问权限.
I have checked that I have thread access to the image.
推荐答案
在某些情况下,您不能使用System.Runtime.InteropServices
.
指针myOnlineImage.PixelBuffer
为空.
您可以使用BitmapImage
,但不能使用WriteableBitmap
,并使用
BitmapDecoder
,BitmapFrame
,PixelDataProvider
,例如msdn的示例:
In some case, you cannot use System.Runtime.InteropServices
.
The pointer myOnlineImage.PixelBuffer
is null.
You can use BitmapImage
but not WriteableBitmap
and convert to byte[]
with
BitmapDecoder
, BitmapFrame
, PixelDataProvider
like the example of the msdn:
byte[] image_array = null;
int image_array_width = 0;
int image_array_height = 0;
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(new Uri("http://www.example.com/logo.png", UriKind.RelativeOrAbsolute));
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = await (response.Content.ReadAsStreamAsync()))
{
using (IRandomAccessStream strm = stream.AsRandomAccessStream())
{
strm.Seek(0);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(strm);
BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0);
// Get the pixels
var transform = new BitmapTransform { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight };
PixelDataProvider dataProvider =
await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.ColorManageToSRgb);
await strm.FlushAsync();
image_array = dataProvider.DetachPixelData();
image_array_width = (int)decoder.PixelWidth;
image_array_height = (int)decoder.PixelHeight;
}
}
}
}
catch (Exception)
{
throw;
}
}
这篇关于在UWP中将WriteableBitmap图像转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!