Get a new Image by cropping

0

I'm trying to convert my cropped image (and/or Grid) to new Normal Image , but it returns as it cropped , in same position, how to fix it, there is how works my program after cropping

enter image description here

 private async void Save(object sender, RoutedEventArgs e)
        {
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(GGrid, 4096 , 2448);
           // IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
            IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            FileSavePicker fileSavePicker = new FileSavePicker();
            fileSavePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            fileSavePicker.FileTypeChoices.Add("JPEG files", new List<string>() {  ".png"});
            fileSavePicker.SuggestedFileName = "image";

            var outputFile = await fileSavePicker.PickSaveFileAsync();

            if (outputFile == null)
            {
                // The user cancelled the picking operation
                return;
            }
            using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
            {

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    pixelBuffer.ToArray());
                try
                {

                    await encoder.FlushAsync();
                }
                catch (Exception err)
                {
                    switch (err.HResult)
                    {
                        case unchecked((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION
                                                         // If the encoder does not support writing a thumbnail, then try again
                                                         // but disable thumbnail generation.
                            encoder.IsThumbnailGenerated = false;
                            break;
                        default:
                            throw err;
                    }
                }

                //if (encoder.IsThumbnailGenerated == false)
                //{
                //    await encoder.FlushAsync();
                //}


            }
        }
c#
uwp
crop
asked on Stack Overflow Oct 13, 2017 by Հայկ • edited Oct 13, 2017 by Nikolaj Dam Larsen

1 Answer

0

enter image description here

You need to translate and crop the bitmap.

double x,y,w,h;

GGrid.RenderTransform = new TranslateTransform
{
    X = -x, // translate back to origin (0,0)
    Y = -y
};

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(GGrid, w, h); // use crop size
answered on Stack Overflow Oct 14, 2017 by Laith

User contributions licensed under CC BY-SA 3.0