我有一个BitmapImage对象,其中包含600 X 400像素的图像。现在从我的C#代码中,我需要创建两个新的BitmapImage对象,即objA和objB,每个对象的尺寸为600 X 200,其中objA包含裁剪后的上半部分图像,objB包含原始图像的裁剪后的下半部分图像。
我有一个BitmapImage对象,其中包含600 X 400像素的图像。现在从我的C#代码中,我需要创建两个新的BitmapImage对象,即objA和objB,每个对象的尺寸为600 X 200,其中objA包含裁剪后的上半部分图像,objB包含原始图像的裁剪后的下半部分图像。
BitmapSource topHalf = new CroppedBitmap(sourceBitmap, topRect);
BitmapSource bottomHalf = new CroppedBitmap(sourceBitmap, bottomRect);
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);