使用Xamlcropcontrol为UI和WriteableBitmapEx裁剪图像

时间:2015-06-10 04:16:35

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

在过去的一周里,我一直在研究如何添加在Windows应用商店应用中裁剪个人资料图片的功能。到目前为止,我已经看过微软的解决方案,但已决定走另一条路。我从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);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

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