c# - 使用用于 UI 和 WriteableBitmapEx 的 Xamlcropcontrol 裁剪图像

标签 c# windows-store-apps winrt-xaml writeablebitmapex

在过去的一周里,我一直在尽可能多地研究如何在我的 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。

This is the information from xamlcropcontrol

This is the problem im having

它告诉我,如果我在那里有 (imagetoResize),那么 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);
            }
        }
    }

最佳答案

您必须为此使用 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);

关于c# - 使用用于 UI 和 WriteableBitmapEx 的 Xamlcropcontrol 裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747008/

相关文章:

c# - 我收到错误 'EntityType has no key defined',即使我在模型和数据库中都定义了一个键

c# - StreamSocket.ConnectAsync 在使用 VPN 时抛出异常 "HRESULT: 0x8007274C"

localization - 在运行时在 Windows 应用商店应用程序中切换语言

winrt-xaml - 如何在 ContentDialog 中添加可滚动文本? [WP8.1 Win-RT]

c# - 如何仅使用 XAML 更改基于点击事件的文本 block 的可见性

c# - 为每个可能的事件定义事件处理程序背后的原因是什么,即使它们没有被使用?

c# - 关于 32 位 .exe 与 64 位 .exe

c# - 如何防止自定义控件的元素获得焦点?

c# - 如何在 lambda 方法中传递参数和异步关键字?

c# - 将 Fiddler 与 Windows 应用商店单元测试结合使用