locked
WriteableBitmap's PixelWidth and PixelHeight propery does not change after a new SetSource

    Question

  • I am not able to retrieve the width and the height of an image after I load it from a StorageFile object. Here is the code...

        FileOpenPicker^ openPicker = ref new FileOpenPicker();
        openPicker->ViewMode = PickerViewMode::Thumbnail;
        openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
        openPicker->FileTypeFilter->Append(".jpg");
        openPicker->FileTypeFilter->Append(".jpeg");
        openPicker->FileTypeFilter->Append(".png");
    
    	auto getStreamTask = create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
        {
            return file->OpenReadAsync();
        });
    	
    	getStreamTask.then(
        [this] (Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)
        {
    		WriteableBitmap^ bmp = ref new WriteabeBitmap(1, 1);
    		bmp->SetSource(stream);
    		displayImage->Source = bmp; // Displays correctly
    		int Witdh = bmp->PixelWidth; // Remains 1
    		int Height = bmp->PixelHeight; // Remains 1
        });
    

    The PixelHeight and PixelWidth propery remains (1,1) for the bitmap. I tried displaying it in an image object in XAML and the image is displayed correctly though.

    Friday, August 10, 2012 12:23 PM

Answers

  • Hi,

     

    Yes, writeableBitmap cannot calculate the height and width of that image, you can use BitmapImage class to get it.

     
    		BitmapImage^ bmp=ref new BitmapImage();
    		bmp->SetSource(stream);
    		//bm->PixelHeight
    		//WriteableBitmap^ bmp = ref new WriteabeBitmap(1, 1);
    		//bmp->SetSource(stream);
    		//displayImage->Source = bmp; // Displays correctly
    		int Witdh = bmp->PixelWidth; // Remains 1
    		int Height = bmp->PixelHeight; // Remains 1

    Best regards,

    Jesse


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

    • Marked as answer by pragos Monday, August 13, 2012 5:19 PM
    Monday, August 13, 2012 11:21 AM

All replies

  • Hi,

     

    Yes, writeableBitmap cannot calculate the height and width of that image, you can use BitmapImage class to get it.

     
    		BitmapImage^ bmp=ref new BitmapImage();
    		bmp->SetSource(stream);
    		//bm->PixelHeight
    		//WriteableBitmap^ bmp = ref new WriteabeBitmap(1, 1);
    		//bmp->SetSource(stream);
    		//displayImage->Source = bmp; // Displays correctly
    		int Witdh = bmp->PixelWidth; // Remains 1
    		int Height = bmp->PixelHeight; // Remains 1

    Best regards,

    Jesse


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

    • Marked as answer by pragos Monday, August 13, 2012 5:19 PM
    Monday, August 13, 2012 11:21 AM
  • Thanks Jesse!
    Monday, August 13, 2012 5:19 PM