i_am_jorf
i_am_jorf

Reputation: 54600

How does one get the dimensions of an image from a stream (without rendering it)?

I'm doing the standard CameraChooserTask dance. In my callback, I'd like to get the dimensions of the captured photo.

I tried using BitmapImage, as below, but since the BitmapImage isn't part of the render tree, I don't think it actually does any decoding (its ImageOpened event doesn't fire).

private void CameraCapture_Completed(object sender, PhotoResult e)
{
  if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
  {
    // This code DOES NOT work:
    // BitmapImage bi = new BitmapImage();
    // bi.SetSource(stream);
    // ... use bi.PixelHeight and bi.PixelWidth ...

Upvotes: 1

Views: 619

Answers (1)

i_am_jorf
i_am_jorf

Reputation: 54600

Ah, the trick is to force someone to use the BitmapSource. By assigning it as the source for an Image, the PixelHeight and PixelWidth properties get set.

private void CameraCapture_Completed(object sender, PhotoResult e)
{
  if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
  {
    BitmapImage bi = new BitmapImage();
    bi.SetSource(stream);

    Image image = new Image();
    image.Source = bi;

    // ... use bi.PixelHeight and bi.PixelWidth ...

Upvotes: 2

Related Questions