开发者

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 Bit开发者_JAVA技巧mapImage, 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 ...


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 ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜