【发布时间】:2017-06-22 16:33:34
【问题描述】:
这是我的第一个主题!我正在重写我在 UWP 中使用 Silverlight 8 (WP8) 开发的应用程序。我从 BitmapImage 获取 base64 编码字符串时遇到问题。我工作了很多天都找不到解决方案:-( .
总之,我需要: - 从设备库中选择图像 - 将所选图像剪切为 1 兆像素(1024* 1024) - 压缩所选图像 -从压缩图像中获取base64编码字符串
我的 Silverlight 8 代码(工作):
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
我的 UWP 代码(不起作用):
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()
如何将所选图像剪切为 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
谢谢!!
【问题讨论】: