使用 Xamlcropcontrol 裁剪图像,用于 UI 和 WriteableBitmapEx

本文关键字:UI WriteableBitmapEx 用于 Xamlcropcontrol 裁剪 图像 使用 | 更新日期: 2023-09-27 17:55:08

在过去的一周里,我一直在研究如何在我的 Windows 应用商店应用中添加裁剪个人资料图像的功能。到目前为止,我已经研究了Microsoft解决方案,但决定走一条不同的路线。我从NuGet下载了一个名为XamlCropControl的控件。它非常适合 UI,甚至可以为我提供诸如原始高度/宽度、裁剪的顶部/底部/左侧/右侧/宽度/高度等信息,所有这些都在 xaml 控件内。我的问题是如何获取这些信息并使用WriteableBitmapEx裁剪图像。到目前为止,这是我的代码,我遇到了问题。

private async void ProfilePhotoImageClick(object sender, TappedRoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
        using (Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(fileStream);
            BackgroundLogo.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            PhotoUploadCropper.Opacity = 1;
            PhotoUploadCropper.ImageSource = bitmapImage;
            ProfileSetupStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            imagetoResize = bitmapImage;
        }
    }
}
BitmapImage imagetoResize;
private void AcceptPhotoImageCropClick(object sender, RoutedEventArgs e)
{
    WriteableBitmap bmp = new WriteableBitmap(0,0).FromContent(imagetoResize);
    var croppedBmp = bmp.Crop(0, 0, bmp.PixelWidth / 2, bmp.PixelHeight / 2);
    croppedBmp.SaveToMediaLibrary("ProfilePhoto.jpg");
}

PhotoUploadCropper 是 xamlcropcontrol。

这是来自 xamlcropcontrol 的信息

这是我遇到的问题

它告诉我,如果我在那里有(图像调整大小),FromContent 没有定义,但如果我删除它,我不会出错。完成所有裁剪后,它将上传到我已经设置的 azure blob 存储。

编辑:像这样工作。

private async void ProfilePhotoImageClick(object sender, TappedRoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");
        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapImage bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(fileStream);
                fileclone = file;
                BackgroundLogo.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                PhotoUploadCropper.IsEnabled = true;
                PhotoUploadCropper.Opacity = 1;
                PhotoUploadCropper.ImageSource = bitmapImage;
                ProfileSetupStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }
    }
    StorageFile fileclone;
    async Task<WriteableBitmap> LoadBitmap(StorageFile file)
    {
        int cropx = PhotoUploadCropper.CropTop;
        int cropy = PhotoUploadCropper.CropLeft;
        int cropW = PhotoUploadCropper.CropWidth;
        int cropH = PhotoUploadCropper.CropHeight;
        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
        {
            var bmp = await BitmapFactory.New(1, 1).FromStream(fileStream);
            var croppedBmp = bmp.Crop(cropy, cropx, cropW, cropH);
            var resizedcroppedBmp = croppedBmp.Resize(200, 200, WriteableBitmapExtensions.Interpolation.Bilinear);
            return resizedcroppedBmp;
        }
    }
    private async void AcceptPhotoImageCropClick(object sender, RoutedEventArgs e)
    {
        var CroppedBMP = await CropBitmap(fileclone);
        using (IRandomAccessStream fileStream = new InMemoryRandomAccessStream())
        {
            string filename = Path.GetRandomFileName() + ".JPG";
            var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                Stream pixelStream = CroppedBMP.PixelBuffer.AsStream();
                byte[] pixels = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)CroppedBMP.PixelWidth, (uint)CroppedBMP.PixelHeight, 96.0, 96.0, pixels);
                await encoder.FlushAsync();
            }
            ProfilePhotoButtonsGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            BackgroundLogo.Visibility = Windows.UI.Xaml.Visibility.Visible;
            PhotoUploadCropper.IsEnabled = false;
            PhotoUploadCropper.Opacity = 0;
            ProfileSetupStackPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
            if (fileStream != null)
            {
                UploadFile(file);
            }
        }
    }

使用 Xamlcropcontrol 裁剪图像,用于 UI 和 WriteableBitmapEx

为此,您必须使用 WBX WinRT API。看起来您正在尝试WP/Silverlight方法。

试试这个:

    async Task<WriteableBitmap> LoadBitmap(string path)
    {
        Uri imageUri = new Uri(BaseUri, path);
        var bmp = await BitmapFactory.New(1, 1).FromContent(imageUri);
        return bmp;
    }

请注意,它需要一个 URI。在您的情况下,您可以直接使用 IRandomAccessStream:

 var bmp = await BitmapFactory.New(1, 1).FromStream(fileStream);