【发布时间】:2016-05-19 15:16:58
【问题描述】:
我有一个 WPF .xaml 文件,其中包含一个在我的 C# 代码中绑定到 BitmapSource 的图像框架元素。目前,我正在对位图的图像宽度和高度(即 512 像素)进行硬编码,并且我已指定框架元素不拉伸图像。因此,如果我的窗口大于位图,则图像似乎漂浮在窗口的边界内。但是,我想要的是位图宽度和高度自动绑定到 .xaml 窗口的宽度和高度。因此,当我调整窗口大小时,位图也会随之调整大小。我不只是希望图像被“拉伸”......相反,我希望位图的宽度和高度与包含它的窗口相匹配。我不完全确定如何执行此操作,但我会发布我目前的设置。
在我的 .xaml 文件中,我有我的 Image 框架元素(注意:渲染变换只是沿水平轴翻转图像):
<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>
我的 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()
);
}
【问题讨论】:
-
将拉伸设置为填充。无需绑定。