简体   繁体   English

WPF绑定图像的宽度和高度以及相关性

[英]WPF Binding Image Width and Height with Dependency Properties

I have a WPF .xaml file which contains an image framework element which is bound to a BitmapSource in my C# code. 我有一个WPF .xaml文件,其中包含一个图像框架元素,该图像框架元素在我的C#代码中绑定到BitmapSource。 At the moment, I'm hard coding the image width and height of my bitmap (ie. 512px) and I've specified the framework element not to stretch the image. 此刻,我正在对位图的图像宽度和高度(即512像素)进行硬编码,并且指定了不拉伸图像的框架元素。 So, if my window is bigger than the bitmap, the image seems to float inside the bounds of the window. 因此,如果我的窗口大于位图,则图像似乎漂浮在窗口的边界内。 However, what I'd like is that the bitmap width and height is automatically bound to the width and height of the .xaml window. 但是,我想要的是位图的宽度和高度自动绑定到.xaml窗口的宽度和高度。 So, when I resize the window, the bitmap gets resized along with it. 因此,当我调整窗口大小时,位图也会随之调整大小。 I don't simply want the image to be 'stretched'... rather, I want the bitmap width and height to match the window that contains it. 我不只是希望图像被“拉伸” ...而是希望位图的宽度和高度与包含它的窗口相匹配。 I'm not entirely sure how to do this, but I'll post the setup that I have so far. 我不确定如何执行此操作,但是我将发布到目前为止的设置。

In my .xaml file, I have my Image framework element (Note: the render transform merely flips the image along the horizontal axis): 在我的.xaml文件中,我有我的Image框架元素(注意:render变换仅沿水平轴翻转图像):

<Image Source="{Binding WpfPreview}" Name="VectorPreview" Stretch="None" Width="{Binding Xres}" Height="{Binding Yres}" RenderTransformOrigin="0.5,0.5">
    <Image.RenderTransform>
        <ScaleTransform ScaleY="-1"/>
    </Image.RenderTransform>
</Image>

My C# code then has the following code in it: 然后,我的C#代码中包含以下代码:

protected BitmapSource wpfPreview;
public BitmapSource WpfPreview
{
    get { return wpfPreview; }
    set
    {
        wpfPreview = value;
        NotifyPropertyChanged("WpfPreview");
    }
}

protected static int xres = 512;
public int Xres
{
    get { return xres; }
    set
    {
        xres = value;
        NotifyPropertyChanged("Xres");
        UpdateBitmapPreview();
    }
}

protected static int yres = 512;
public int Yres
{
    get { return yres; }
    set
    {
       yres = value;
       NotifyPropertyChanged("Yres");
       UpdateBitmapPreview();
    }
}

protected GDI.Bitmap gdiPreviewImage = new GDI.Bitmap(xres, yres);

public void UpdateBitmapPreview()
{
    if(gdiPreviewImage.Width != xres || gdiPreviewImage.Height != yres) gdiPreviewSurface = new GDI.Bitmap(xres, yres);
    using (var graphics = GDI.Graphics.FromImage(gdiPreviewImage))
    {
        graphics.Clear(Color.White);
        graphics.ResetTransform();

        currentSlicer.DrawBitmapPreview(graphics, PreviewOptions);
     }

     WpfPreview = Imaging.CreateBitmapSourceFromHBitmap(
            gdiPreviewImage.GetHbitmap(), 
            IntPtr.Zero, 
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions()
            );
}

You basically want to keep aspect ratio while resizing? 您基本上想在调整大小时保持宽高比吗? I think these properties can fix this up. 我认为这些属性可以解决此问题。 set ClipToBounds to true and Stretch to Uniform . ClipToBounds设置为true并将 StretchUniform

<Image Stretch="Uniform" ...

However, what I'd like is that the bitmap width and height is automatically bound to the width and height of the .xaml window. 但是,我想要的是位图的宽度和高度自动绑定到.xaml窗口的宽度和高度。

And so, wouldn't be simpler to bind the Width and the Height directly to the Width and the Height of Window, at this point? 因此,此时将Width和Height直接绑定到Window的Width和Heights会更简单吗?

<Image Source="{Binding WpfPreview}"
       Stretch="None" 
       Width="{Binding Width, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
       Height="{Binding Height, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM