【问题标题】:WPF C# Preventing a background from resizing when loaded as a background in an inkCanvasWPF C#在inkCanvas中作为背景加载时防止背景调整大小
【发布时间】:2014-04-24 16:38:38
【问题描述】:

在微软文档和论坛中寻找答案后,我不知所措。我正在加载一个 png 图像作为 inkCanvas (WPF) 的背景,它工作正常,但是,它总是调整图像大小以适合画布,尽管图像大小..

这是我最后一次尝试但没有成功:

            BitmapImage ii = new BitmapImage(new Uri(path));
            Image img = new Image();

            img.Stretch = Stretch.None;
            img.Source = ii;
            InkCanvas1.Background = new ImageBrush(ii);

这是使用 Stretch.None 和 Stretch.Fill 的样子

这是我想要实现的目标:

这个可以吗?

【问题讨论】:

  • 所以你想让 imageBrush 超越墨水画布?
  • 不一定,我希望图像的纵横比大小适合画布顶部或底部的最大宽度或最大高度和空白(取决于横向或纵向图像)。
  • 你能发布快照现在是什么样子吗?期望的输出是什么?

标签: c# wpf drawing inkcanvas


【解决方案1】:

问题是您试图在您未使用的Image 对象上设置属性,而忽略了您正在使用的ImageBrush 上的相同设置。在这种情况下,Image 只是被丢弃,ImageBrush 恰好使用相同的源图像。改为在ImageBrush 上设置Stretch 属性:

    BitmapImage ii = new BitmapImage(new Uri(path));

    ImageBrush imageBrush = new ImageBrush(ii);
    imageBrush.Stretch = Stretch.Uniform;
    InkCanvas1.Background = imageBrush;

【讨论】:

  • 完美!!谢谢你让我觉得自己像个白痴! :)
猜你喜欢
  • 2010-11-05
  • 1970-01-01
  • 2012-05-05
  • 2014-07-24
  • 2023-01-30
  • 2012-01-14
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多