【问题标题】:Resize image for use as an image brush调整图像大小以用作图像画笔
【发布时间】:2012-02-03 07:33:58
【问题描述】:

我想要

  • 从资源中加载图片
  • 设置图像宽度,确保它按比例缩放其高度。
  • 将其用作背景画笔

它无法弄清楚“设置宽度”部分。

我目前使用的折衷方案是将其缩小到一半。这不是我理想中想要做的,我想将宽度设置为绝对值,不管原始图像大小是多少。

<BitmapImage x:Key="floorPlan"
        UriSource="/NavigationPathEditor;component/Images/CairnsFloorPlansCropped.png"/>

<TransformedBitmap x:Key="resizedFloorPlan" Source="{StaticResource floorPlan}">
    <TransformedBitmap.Transform>
        <ScaleTransform 
                CenterX="0"   CenterY="0" 
                ScaleX="0.5"  ScaleY="0.5" />
    </TransformedBitmap.Transform>
</TransformedBitmap>

<ImageBrush 
      x:Key="floorPlanBrush" 
      TileMode="None" Stretch="None" 
      AlignmentX="Left" AlignmentY="Top"
      ImageSource="{StaticResource resizedFloorPlan}" />

(顺便说一下,上面的代码在运行时工作,但在设计器中抛出错误)

【问题讨论】:

    标签: wpf xaml imagebrush


    【解决方案1】:

    最简单的方法可能是在图像本身上设置DecodePixelWidth/Height

    【讨论】:

    • 感谢您的回答。在您写答案时,我稍微改变了问题,因为我知道如何使用 TransformedBitmap 元素。我现在让问题更具体:我想将图像设置为绝对宽度而不是比例宽度。
    猜你喜欢
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2012-08-27
    • 2011-10-27
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多