locked
How to crop image in metro app using c++?

    Question

  • Hi

    How can I crop an image and save to picture library in metro app using c++ 

    I have the image opened using OpenAsync() call.

    I need to save the cropped image to a file or set to an image control using

    BitmapImage->SetSource() 

    Thanks in advance

    _Sujith
    Thursday, July 12, 2012 9:55 AM

Answers

  • It sounds like you are starting with an image file containing some flavor of bitmap image (jpg, png, etc.). Load the stream from that StorageFile into a BitmapDecoder and read the cropped image data by passing a BitmapTransform with the desired crop Bounds to BitmapDecoder.GetPixelDataAsync. You can then save the cropped image to a file by passing it to a BitmapEncoder or you can set it into an Image control by copying the PixelData into a WriteableBitmap's PixelBuffer (which you can access by querying for its IBufferByteAccess interface).

    As has been mentioned, you can do similar transformations with WIC or D2D.

    The .Net methods mentioned are for WPF apps and are not relevant to Metro style app programming. Metro style apps written in .Net languages use the same Windows.Ui.Xaml classes that you call in a Metro style app written in C++/Cx.

    --Rob

    • Proposed as answer by Jesse Jiang Wednesday, July 18, 2012 2:58 AM
    • Marked as answer by Jesse Jiang Thursday, August 02, 2012 7:24 AM
    Friday, July 13, 2012 7:07 PM
    Owner

All replies

  • Depending on whether you're using the .NET library, you can either use http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.croppedbitmap.aspx or manually use the Windows Imaging Component to create a IWICBitmapClipper on a IWICBitmapSource.
    Thursday, July 12, 2012 11:57 AM
  • Hi Sujith,

    You can crop the image by using Direct2D also.

    Direct2D provides rich set of built-in effects for image processing.

    http://msdn.microsoft.com/en-us/library/windows/desktop/hh706316(v=vs.85).aspx#__crop_effect

    for cropping you can use CLSID_D2D1Crop.

    In order to set the image to the control after applying effect.

    You can use IRandomAccessStream.

    Take the output from Direct2D as IRandomAccessStream and set it to the control.

    thanks,

    Bhash

    Thursday, July 12, 2012 4:12 PM
  • You can follow these codes to copy the image into new bitmap image

     
    	String^ filename;
    	auto pictures = KnownFolders::PicturesLibrary;
    	create_task(pictures->GetFileAsync(filename)).then([this](StorageFile^ file){
    		BitmapImage^ bitmapiamge= ref new BitmapImage();
    		create_task(file->OpenAsync(FileAccessMode::Read)).then([this,&bitmapiamge](IRandomAccessStream^ ras){
    			bitmapiamge->SetSource(ras);
    		});
    	});

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 13, 2012 7:43 AM
  • From there you probably just need to do something like

    auto croppedBitmap = ref new CroppedBitmap(sourceBitmapImage, new Int32Rect(x, y, width, height));

    This CroppedBitmap inherits from BitmapImage so you can then use it to set on an image control.

    Friday, July 13, 2012 12:20 PM
  • It sounds like you are starting with an image file containing some flavor of bitmap image (jpg, png, etc.). Load the stream from that StorageFile into a BitmapDecoder and read the cropped image data by passing a BitmapTransform with the desired crop Bounds to BitmapDecoder.GetPixelDataAsync. You can then save the cropped image to a file by passing it to a BitmapEncoder or you can set it into an Image control by copying the PixelData into a WriteableBitmap's PixelBuffer (which you can access by querying for its IBufferByteAccess interface).

    As has been mentioned, you can do similar transformations with WIC or D2D.

    The .Net methods mentioned are for WPF apps and are not relevant to Metro style app programming. Metro style apps written in .Net languages use the same Windows.Ui.Xaml classes that you call in a Metro style app written in C++/Cx.

    --Rob

    • Proposed as answer by Jesse Jiang Wednesday, July 18, 2012 2:58 AM
    • Marked as answer by Jesse Jiang Thursday, August 02, 2012 7:24 AM
    Friday, July 13, 2012 7:07 PM
    Owner