【问题标题】:Crop an Image in WPF在 WPF 中裁剪图像
【发布时间】:2020-12-27 06:15:48
【问题描述】:

给一张图片:

Image tileSet = new Image();
tileSet.Source = new BitmapImage(new Uri(@".."));

如何裁剪它,定义一个矩形区域?

【问题讨论】:

标签: wpf image bitmapimage


【解决方案1】:

您可以为此使用CroppedBitmap

var fullBitmap = new BitmapImage(new Uri(@".."));
tileSet.Source = new CroppedBitmap(fullBitmap, new Int32Rect(0, 0, 100, 100));

【讨论】:

    【解决方案2】:

    要在上面 dkozl 的回答基础上再做一点,

    我发现要裁剪的原始图像的 DPI 和显示器的 DPI 与我打算裁剪原始图像的位置产生了“偏移”。

    可以通过将图像宽度和高度(以像素为单位)与屏幕的 DPI 匹配来纠正“偏移”,而不是让图像元素自动调整大小。 您可以通过将 Stretch 设置为 None,并将图像的宽度和高度设置为如下:

    <Image
        x:Name="imageToCrop"
        Stretch="None"
        Width="{Binding Source.PixelWidth,RelativeSource={RelativeSource Self}}"
        Height="{Binding Source.PixelHeight,RelativeSource={RelativeSource Self}}"/>
    

    【讨论】:

      猜你喜欢
      • 2011-01-31
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多