有没有办法通过指定源矩形的子区域来实现 DrawingContext.DrawImage?

Is there a way to do DrawingContext.DrawImage by specifying a subregion of the source rectangle?

在 WPF 中,DrawingContext.DrawImage 如下所示:

public abstract void DrawImage(
    ImageSource imageSource,
    Rect rectangle
)

矩形记录为 "The region in which to draw BitmapSource."

我需要一种方法将源矩形的子矩形绘制到目标矩形中。

在 WinForms 中,您可以很容易地做到这一点,因为 Graphics.DrawImage 需要两个矩形。例如,

public void DrawImage(
    Image image,
    Rectangle destRect,
    int srcX,
    int srcY,
    int srcWidth,
    int srcHeight,
    GraphicsUnit srcUnit
)

在 WPF 中,我没有看到任何等效项。

谁有干净高效的方法来做到这一点?

谢谢!

你可以画一个CroppedBitmap:

ImageSource source = ...
var sourceRect = new Int32Rect(...);
var croppedBitmap = new CroppedBitmap(source, sourceRect);
...
drawingContext.DrawImage(croppedBitmap, new Rect(...));