DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

Image Cropping In Windows 8 Metro Applications

Denzel D. user avatar by
Denzel D.
·
May. 12, 12 · Interview
Like (0)
Save
Tweet
Share
9.84K Views

Join the DZone community and get the full member experience.

Join For Free

Among other API changed in WinRT, major changes were introduced to the image manipulation capabilities available in the .NET Framework out-of-the-box. For one of my projects - a tile editor, I needed to uniformly crop an image given specific pre-defined conditions. In WPF there was CroppedBitmap that allowed me to do that. In WinRT this class is no longer available. I was considering relying on low-level APIs when I realized that there is, in fact, one managed workaround, that might be acceptable in specific cases.

Here is the situation - you are loading am image from a local (or remote) file:

var filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.ViewMode = PickerViewMode.Thumbnail;
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

var file = await filePicker.PickSingleFileAsync();

var inputStream = await file.OpenAsync(FileAccessMode.Read);

To get an actual working image, you might use the BitmapImage class:

BitmapImage image = new BitmapImage();
image.SetSource(inputStream);

Here is the catch. From here you will need to work with an Image control and rely on its properties and workings. One of them - the image scale. By default, the Image control will scale your image to the height of the container, and that might cause cropping problems, since you will be selecting the scaled area instead of the sector that is actually needed. There are two ways to avoid it:

1. Set the Stretch property for the Image control to None - that way, the image will not be stretched.

imgTest.Source = image;
imgTest.Stretch = Stretch.None;

2. Use a BitmapDecoder to get the proper size of the image and resize the Image control.

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inputStream);
imgTest.Source = image;
imgTest.Height = decoder.PixelHeight;
imgTest.Width = decoder.PixelWidth;

Once this is completed, it is possible to call the Clip method to get the region that should be extracted:

imgTest.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, 64, 48) };

Congratulations. You can now use WriteableBitmap to either capture the region or keep the image clipped as it is.

application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Common Firewall Misconfigurations and How to Address Them
  • Test Design Guidelines for Your CI/CD Pipeline
  • Debezium vs DBConvert Streams: Which Offers Superior Performance in Data Streaming?
  • Collaborative Development of New Features With Microservices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: