![](/img/trans.png)
[英]Get base64 string from a big StorageFile and store it in another StorageFile
[英][UWP]Resize, compress and get base64 string from BitmapImage
這是我的第一個帖子! 我正在重寫我在UWP中使用Silverlight 8(WP8)開發的應用程序。 我從BitmapImage獲取base64編碼的字符串時遇到問題。 我工作的很多天都沒有找到解決方案:-(。
總之,我需要: - 從設備庫中選擇一個圖像 - 將所選圖像切換為1兆像素(1024 * 1024) - 壓縮所選圖像 - 從壓縮圖像中獲取base64編碼的字符串
我的Silverlight 8代碼(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
我的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
謝謝 !!
在UWP中,您可以使用BitmapDecoder
和BitmapTransform
來裁剪圖像。 以下是關於將所選圖像裁剪為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
方法返回內部存儲的像素數據。 您可以使用它來獲取base64字符串。
這里有一個官方如何在MSDN上的Windows應用商店應用示例中裁剪位圖,您可以參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.