将bitmapImage转换为Windows store的字节数组

时间:2015-04-25 20:53:30

标签: c# vb.net windows-store-apps windows-store

我需要知道如何将位图图像转换为Windows应用程序的字节数组。 这是我尝试过但它不存储bitmapImage

    Dim file As StorageFile = Await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync()
    Dim stream As IRandomAccessStream = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
    Dim decoder As BitmapDecoder = Await BitmapDecoder.CreateAsync(stream)
    Dim pixelData As PixelDataProvider = Await decoder.GetPixelDataAsync()
    Return pixelData.DetachPixelData()

2 个答案:

答案 0 :(得分:0)

使用MemoryStream将位图保存到Bitmap.Save(),然后保存MemoryStream.ToArray()。也许不是最优雅的方法,但有效。 我稍后可以发布一个工作代码。

答案 1 :(得分:0)

我发现了:D

Async Function bitmapTObyte(ByVal bitmapimage1 As StorageFile) As Task(Of Byte())
    Dim fileBytes As Byte() = Nothing
    Using stream As IRandomAccessStreamWithContentType = Await bitmapimage1.OpenReadAsync()
        fileBytes = New Byte(stream.Size - 1) {}
        Using reader As New DataReader(stream)
            Await reader.LoadAsync(CUInt(stream.Size))
            reader.ReadBytes(fileBytes)
            Return fileBytes
        End Using
    End Using