这是我的功能:
private byte[] GetByteArray(IPhotoObject photo)
{
_addCanvas.RenderTransformOrigin = new Point(0.5, 0.5);
_addCanvas.RenderTransform = new RotateTransform(90.0);
BinaryImageConverter converter = new BinaryImageConverter();
Image i = new Image();
BitmapSource source = (BitmapImage)converter.Convert(photo.ImageBytes, typeof (BitmapSource), null, null);
i.Stretch = Stretch.None;
i.Width = source.PixelWidth;
i.Height = source.PixelHeight;
i.SetValue(Image.SourceProperty,source);
var width = source.PixelWidth;
var height = source.PixelHeight;
Canvas canvas = new Canvas();
canvas.Width = width;
canvas.Height = height;
canvas.Children.Add(i);
canvas.Children.Add(_addCanvas);
var size = new Size(width, height);
var rect = new Rect(size);
canvas.Measure(size);
canvas.Arrange(rect);
RenderTargetBitmap bmp = new RenderTargetBitmap(
Convert.ToInt32(width),
Convert.ToInt32(height),
96.0,
96.0,
PixelFormats.Default);
bmp.Render(canvas);
return XImage.GetJpegByteArrayFromWritableBitmap(new WriteableBitmap(bmp));
}
我的问题是_addCanvas。它没有被绘制到位图上。如果我取出旋转线,_addCanvas 将被绘制到位图,但我需要旋转 _addCanvas。
_addCanvas 具有包含简单形状(正方形、圆形、线条)和文本框的子项。
我已经尝试在 _addCanvas 上调用 Measure、Arrange 和 UpdateLayout 无济于事。如果旋转,它不会进入位图中。