none
CroppedBitmap Class

    Question

  • I don't see a CroppedBitmap Class in the Preview bits? I see some Cropping properties in the CameraCaptureUIPhotoCaptureSettings, but no general class which was in .NET 4. Here is a link to the class description:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.croppedbitmap.aspx

    Maybe, someone from Microosft could chime in if there will be support for this class in the Beta.

    My plan for the preview bits is to leverage writeablebitmap and create my own cropping method using a BitmapImage, but if there is a simplier way I am missing, please chime in.

     

    thanx

    rob

     

    Thursday, December 29, 2011 6:53 PM

Answers

  • I wrote a prototype cropping user control, which supports touch and mouse input. The xaml uses a "clipping" rectangle, and code behind uses a writeable bitmap.

    Here is my working crop method to get you jump started: (I'm waiting until after the beta release to formalize a control in case Microsoft has more support at that time, but the prototype is working in my app.)

     async private void Crop()
            {
                // BUGBUG: how to create a BitmapSource from an ImageSource ? BitmapImage : BitmapSource : Image Source - an Image.Source = Image Source
                WriteableBitmap baseWriteBitmap = new WriteableBitmap(_bi);
                Stream stream = baseWriteBitmap.PixelBuffer.AsStream();
                byte[] pixels = new byte[(uint)stream.Length];
                await stream.ReadAsync(pixels, 0, pixels.Length);
    
                //
                // create mapping rectangle
                //
                Rect rect = new Rect(100, 100, 100, 100);
    
                //
                // copy appropriate pixes into cropped bitmap
                //
                InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);
    
                // Create Buffer
                byte[] pixelArray = new byte[(uint)img.Height * (uint)img.Width * 4];
                int offset;
    
                // compute base scale factors
                // there are two scales occuring - the Image control is scaling and the ScalingTransform
                double baseScale = baseWriteBitmap.PixelWidth / _BaseImage.ActualWidth; // inverse scale for Image control
                baseScale *= (1.0 / _ScaleTransform.ScaleX); // inverse scale for ScaleTranform
    
                // computer translate factors
                // There are two translates occuring - the offset of the crop rectangle control and the TranslateTransform
                double baseTransX = 125.0; // BUGBUG: rectangle is hardcoded for now
                baseTransX -= _TranslateTransform.X;
    
                double baseTransY = 0.0; // BUGBUG: rectangle is hardcoded for now
                baseTransY -= _TranslateTransform.Y;
    
                for (int row = 0; row < (uint)img.Height; row++)
                {
                    for (int col = 0; col < (uint)img.Width; col++)
                    {
                        // map the row\col to the base image which has been scaled in two places - image control and ScaleTransform
                        int baseRow = (int)(((double)row + baseTransY) * baseScale);
                        int baseCol = (int)(((double)col + baseTransX) * baseScale);
                        int baseOffset = (baseRow * (int)baseWriteBitmap.PixelWidth * 4) + (baseCol * 4);
    
                        // offset for cropped image
                        offset = (row * (int)img.Width * 4) + (col * 4);
    
                        // swizzle the base picture which is in BGRA format
                        pixelArray[offset] = pixels[baseOffset + 2];      // Red 2
                        pixelArray[offset + 1] = pixels[baseOffset + 1];    // Green 1
                        pixelArray[offset + 2] = pixels[baseOffset + 0];    // Blue 0
                        pixelArray[offset + 3] = pixels[baseOffset + 3];    // Alpha 3
                    }
                }
    
                // Write pixel data
                enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)img.Width, (uint)img.Height, 96, 96, pixelArray);
    
                // Flush (all data is in "ras")
                await enc.FlushAsync();
    
                // Set to the image
                BitmapImage bImg = new BitmapImage();
                bImg.SetSource(ras);
                img.Source = bImg;
    
                //
                // show cropped bitmap via Image element
                //
    
                //
                // save cropped bitmap to file
                //
    
    
    
            }
    

    Thursday, January 12, 2012 7:21 PM

All replies

  • Hi Rob,

    Sorry, we cannot comment about what may or may not be in the Beta.

    -Jeff


    Jeff Sanders (MSFT)
    Friday, December 30, 2011 1:52 PM
  • I don't see a CroppedBitmap Class in the Preview bits? I see some Cropping properties in the CameraCaptureUIPhotoCaptureSettings, but no general class which was in .NET 4. Here is a link to the class description:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.croppedbitmap.aspx

    Maybe, someone from Microosft could chime in if there will be support for this class in the Beta.

    My plan for the preview bits is to leverage writeablebitmap and create my own cropping method using a BitmapImage, but if there is a simplier way I am missing, please chime in.

     

    thanx

    rob

     


    I met the same issue, let me know when you found a replacement.
    Thanks.
    • Edited by Tintin- Thursday, January 12, 2012 12:42 PM
    Thursday, January 12, 2012 12:42 PM
  • Hi Rob,

    Sorry, we cannot comment about what may or may not be in the Beta.

    -Jeff


    Jeff Sanders (MSFT)

    Then can you let me know if I would like to create an image based on part of another image, what should I use?
    Thanks.
    Thursday, January 12, 2012 12:43 PM
  • I wrote a prototype cropping user control, which supports touch and mouse input. The xaml uses a "clipping" rectangle, and code behind uses a writeable bitmap.

    Here is my working crop method to get you jump started: (I'm waiting until after the beta release to formalize a control in case Microsoft has more support at that time, but the prototype is working in my app.)

     async private void Crop()
            {
                // BUGBUG: how to create a BitmapSource from an ImageSource ? BitmapImage : BitmapSource : Image Source - an Image.Source = Image Source
                WriteableBitmap baseWriteBitmap = new WriteableBitmap(_bi);
                Stream stream = baseWriteBitmap.PixelBuffer.AsStream();
                byte[] pixels = new byte[(uint)stream.Length];
                await stream.ReadAsync(pixels, 0, pixels.Length);
    
                //
                // create mapping rectangle
                //
                Rect rect = new Rect(100, 100, 100, 100);
    
                //
                // copy appropriate pixes into cropped bitmap
                //
                InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);
    
                // Create Buffer
                byte[] pixelArray = new byte[(uint)img.Height * (uint)img.Width * 4];
                int offset;
    
                // compute base scale factors
                // there are two scales occuring - the Image control is scaling and the ScalingTransform
                double baseScale = baseWriteBitmap.PixelWidth / _BaseImage.ActualWidth; // inverse scale for Image control
                baseScale *= (1.0 / _ScaleTransform.ScaleX); // inverse scale for ScaleTranform
    
                // computer translate factors
                // There are two translates occuring - the offset of the crop rectangle control and the TranslateTransform
                double baseTransX = 125.0; // BUGBUG: rectangle is hardcoded for now
                baseTransX -= _TranslateTransform.X;
    
                double baseTransY = 0.0; // BUGBUG: rectangle is hardcoded for now
                baseTransY -= _TranslateTransform.Y;
    
                for (int row = 0; row < (uint)img.Height; row++)
                {
                    for (int col = 0; col < (uint)img.Width; col++)
                    {
                        // map the row\col to the base image which has been scaled in two places - image control and ScaleTransform
                        int baseRow = (int)(((double)row + baseTransY) * baseScale);
                        int baseCol = (int)(((double)col + baseTransX) * baseScale);
                        int baseOffset = (baseRow * (int)baseWriteBitmap.PixelWidth * 4) + (baseCol * 4);
    
                        // offset for cropped image
                        offset = (row * (int)img.Width * 4) + (col * 4);
    
                        // swizzle the base picture which is in BGRA format
                        pixelArray[offset] = pixels[baseOffset + 2];      // Red 2
                        pixelArray[offset + 1] = pixels[baseOffset + 1];    // Green 1
                        pixelArray[offset + 2] = pixels[baseOffset + 0];    // Blue 0
                        pixelArray[offset + 3] = pixels[baseOffset + 3];    // Alpha 3
                    }
                }
    
                // Write pixel data
                enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)img.Width, (uint)img.Height, 96, 96, pixelArray);
    
                // Flush (all data is in "ras")
                await enc.FlushAsync();
    
                // Set to the image
                BitmapImage bImg = new BitmapImage();
                bImg.SetSource(ras);
                img.Source = bImg;
    
                //
                // show cropped bitmap via Image element
                //
    
                //
                // save cropped bitmap to file
                //
    
    
    
            }
    

    Thursday, January 12, 2012 7:21 PM
  • Thanks Rob, I'll try it.

    Thanks.

    Friday, February 10, 2012 6:53 PM
  • Hi Rob,

    Have you had a look into this property?

    Bounds

    Read/write Specifies the bounding rectangle that is used to crop the bitmap

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.imaging.bitmaptransform


    Thanks.

    Monday, February 13, 2012 8:54 PM
  • I posted some code using BitmapTransform on the BitmapTransform thread:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/490b9c01-db4b-434f-8aff-d5c495e67e55


    • Edited by RobHogue Wednesday, February 15, 2012 9:30 PM I got the code fully working.
    • Proposed as answer by Tintin- Wednesday, February 22, 2012 5:53 AM
    Wednesday, February 15, 2012 9:18 PM