none
wpf 如何获取加载的图像的大小,并且让它的Canvas的容器的大小与它一样呢? RRS feed

答案

  • 你好 二阳,

    >>"后者可以用绑定 吗?"

    答案是:可以。你只需要将Canvas的ActualHeight和ActualWidth属性与图像的PixelHeigth和PixelWidth属性绑定即可。你可以参考我的代码:

    <Canvas  Width="{Binding Path=ActualWidth, ElementName=img}">
        <Imager x:Name="img" >
    </Image>
    </Canvas>
    
    

    >>"怎么获取真实的大小呢?"

    要获取图片的真实大小,你只需要将它实例化为一个BitmapImage类型的实例,在通过PixelHeight和PixelWidth属性就能获取到。下面是我的代码:

    Dim bmp As BitmapImage
      bmp = PhotoPreview.Source
    
      MessageBox.Show(bmp.PixelHeight.ToString())
      MessageBox.Show(bmp.PixelWidth.ToString())


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月13日 7:01
    版主