[UWP]Resize, compress and get base64 string from BitmapImage

一个人想着一个人 提交于 2019-12-10 18:54:43

问题


this is my first thread! I'm rewriting my application developed in Silverlight 8 (WP8) in UWP. I have a problem obtaining a base64 encoded string from a BitmapImage. I'm working without being able to find a solution from many days :-( .

In summary i need to: -Select an image from device gallery -Cut out the selected image to 1 MegaPixel(1024* 1024) -Compress the selected image -Obtain the base64 encoded string from compressed image

My Silverlight 8 code (WORK):

Private Sub attachButtonHan_Click(sender As Object, e As EventArgs)
Dim photoChooserTaskAttach As Microsoft.Phone.Tasks.PhotoChooserTask
photoChooserTaskAttach = New Microsoft.Phone.Tasks.PhotoChooserTask With {.ShowCamera = True, .PixelHeight = 1024, .PixelWidth = 1024} ' how can i cut selected image from fileOpenPicker in UWP???
AddHandler photoChooserTaskAttach.Completed, AddressOf photoChooserTaskAttach_Completed
photoChooserTaskAttach.Show()
End Sub

Private Sub photoChooserTaskAttach_Completed(sender As Object, e As Microsoft.Phone.Tasks.PhotoResult)
If e.TaskResult = TaskResult.OK Then
Dim bmp As New System.Windows.Media.Imaging.BitmapImage
bmp.CreateOptions = BitmapCreateOptions.BackgroundCreation
bmp.CreateOptions = BitmapCreateOptions.DelayCreation
bmp.DecodePixelWidth = 1024
bmp.DecodePixelHeight = 1024
bmp.SetSource(e.ChosenPhoto)
Dim ms As New MemoryStream
Dim wbc As New System.Windows.Media.Imaging.WriteableBitmap(bmp)
wbc.SaveJpeg(ms, bmp.PixelWidth, bmp.PixelHeight, 0, 70) ' in UWP SaveJpeg Extension is missing...??
Dim result As Byte() = ms.ToArray()
Dim base64 As String = System.Convert.ToBase64String(result)
End If
End Sub

My UWP Code (doesn't work):

Private Async Sub ButtonSelectImgSmp_Click(sender As Object, e As RoutedEventArgs) Handles ButtonSelectImgSmp.Click
Dim openPicker As New FileOpenPicker()
openPicker.ViewMode = PickerViewMode.Thumbnail
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
openPicker.FileTypeFilter.Add(".jpg")
openPicker.FileTypeFilter.Add(".jpeg")
openPicker.FileTypeFilter.Add(".png")
Dim file As StorageFile = Await openPicker.PickSingleFileAsync()

how can i cut selected image to 1024*1024?

If file IsNot Nothing Then
Dim streambmp = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
Dim btmapImage = New Windows.UI.Xaml.Media.Imaging.BitmapImage()
btmapImage.DecodePixelHeight = 1024
btmapImage.DecodePixelWidth = 1024
Await btmapImage.SetSourceAsync(streambmp)
Dim ms As New MemoryStream

'  Dim wbc As New WriteableBitmap(bmp) ' Error
'  wbc.SaveJpeg(ms, bmp.PixelWidth, bmp.PixelHeight, 0, 70) 

' Error (missing savejpeg extension)   

Dim result As Byte() = ms.ToArray() 
Dim base64 As String = System.Convert.ToBase64String(result)

End If
End Sub

Thank you !!


回答1:


In UWP, you can use BitmapDecoder and BitmapTransform to crop image. Following is a simple sample about cropping selected image to 100x100.

Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim openPicker As New FileOpenPicker()
    openPicker.ViewMode = PickerViewMode.Thumbnail
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
    openPicker.FileTypeFilter.Add(".jpg")
    openPicker.FileTypeFilter.Add(".jpeg")
    openPicker.FileTypeFilter.Add(".png")
    Dim file As StorageFile = Await openPicker.PickSingleFileAsync()

    Dim stream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)

    Dim decoder As BitmapDecoder = Await BitmapDecoder.CreateAsync(stream)
    Dim transform As BitmapTransform = New BitmapTransform()
    Dim bounds As BitmapBounds = New BitmapBounds()
    bounds.X = 0
    bounds.Y = 0
    bounds.Height = 100
    bounds.Width = 100
    transform.Bounds = bounds

    Dim pix As PixelDataProvider = Await decoder.GetPixelDataAsync(
        BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Straight,
        transform,
        ExifOrientationMode.IgnoreExifOrientation,
        ColorManagementMode.ColorManageToSRgb)
    Dim pixels As Byte() = pix.DetachPixelData()

    Dim cropBmp As WriteableBitmap = New WriteableBitmap(100, 100)
    Dim pixStream As Stream = cropBmp.PixelBuffer.AsStream()
    pixStream.Write(pixels, 0, 100 * 100 * 4)

    'Image control used to display the image
    img.Source = cropBmp

    Dim base64 As String = Convert.ToBase64String(pixels)
End Sub

PixelDataProvider.DetachPixelData method returns the internally-stored pixel data. You can use it to get base64 string.

And here is a official How to crop bitmap in a Windows Store app sample on MSDN you can refer.



来源:https://stackoverflow.com/questions/33873292/uwpresize-compress-and-get-base64-string-from-bitmapimage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!