gpt4 book ai didi

vb.net - [UWP]调整BitmapImage的大小、压缩并获取base64字符串

转载 作者:行者123 更新时间:2023-12-02 00:57:12 25 4
gpt4 key购买 nike

这是我的第一个帖子!我正在 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

谢谢!!

最佳答案

在 UWP 中,您可以使用 BitmapDecoderBitmapTransform裁剪图像。以下是将所选图像裁剪为 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字符串。

这是官方 How to crop bitmap in a Windows Store app MSDN 上的示例您可以引用。

关于vb.net - [UWP]调整BitmapImage的大小、压缩并获取base64字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33873292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com