4

我有一个包含 600 X 400 尺寸图像的 BitmapImage 对象。现在从后面的 C# 代码中,我需要创建两个新的 BitmapImage 对象,例如 objA 和 objB,每个对象的尺寸为 600 X 200,这样 objA 包含上半部分裁剪图像,而 objB 包含原始图像的下半部分裁剪图像。

4

1 回答 1

5
BitmapSource topHalf = new CroppedBitmap(sourceBitmap, topRect);
BitmapSource bottomHalf = new CroppedBitmap(sourceBitmap, bottomRect);

结果不是 a BitmapImage,但它仍然是有效的ImageSource,如果您只想显示它应该没问题。


编辑:实际上有一种方法可以做到,但它非常难看......您需要Image使用原始图像创建一个控件,并使用该WriteableBitmap.Render方法来渲染它。

Image imageControl = new Image();
imageControl.Source = originalImage;

// Required because the Image control is not part of the visual tree (see doc)
Size size = new Size(originalImage.PixelWidth, originalImage.PixelHeight);
imageControl.Measure(size);
Rect rect = new Rect(new Point(0, 0), size);
imageControl.Arrange(ref rect);

WriteableBitmap topHalf = new WriteableBitmap(originalImage.PixelWidth, originalImage.PixelHeight / 2);
WriteableBitmap bottomHalf = new WriteableBitmap(originalImage.PixelWidth, originalImage.PixelHeight / 2);

Transform transform = new TranslateTransform();
topHalf.Render(originalImage, transform);
transform.Y = originalImage.PixelHeight / 2;
bottomHalf.Render(originalImage, transform);

免责声明:此代码完全未经测试;)

于 2010-09-25T14:49:02.957 回答