Here is my function:
这是我的功能:
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));
}
My problem is _addCanvas. It's not getting drawn to the bitmap. If I take out the lines for the rotate, _addCanvas will be drawn to the bitmap, but I need for _addCanvas to be rotated.
我的问题是_addCanvas。它没有被绘制到位图。如果我取出旋转的线条,_addCanvas将被绘制到位图,但我需要旋转_addCanvas。
_addCanvas has children that include simple shapes (squares, circles, lines) and TextBoxes.
_addCanvas包含包含简单形状(正方形,圆形,直线)和TextBoxes的子项。
I've already tried calling Measure, and Arrange, and UpdateLayout on _addCanvas to no avail. It doesn't get in the bitmap if it's rotated.
我已经尝试在_addCanvas上调用Measure,Arrange和UpdateLayout无济于事。如果它旋转,它不会进入位图。
1 个解决方案
#1
0
The solution to my problem was to add the _addCanvas as a child of canvas after I had called canvas.Measure and canvas.Arrange.
我的问题的解决方案是在调用canvas.Measure和canvas.Arrange之后将_addCanvas添加为canvas的子项。
I'm not 100% on why this was necessary, but here is why I think this worked: While debugging, I noticed that the canvas.ActualWidth and canvas.ActualHeight are zero before the calls to measure and arrange. I guess that in this scenario (having a Canvas that's a child of a Canvas), the RenderTransformOrigin for _addCanvas was being set relative to canvas. Since the ActualWidth and ActualHeight of canvas were both zero when I put _addCanvas in its list of children, _addCanvas was getting rotated about the origin, instead of about its center.
我不是百分之百为什么这是必要的,但这就是为什么我认为这有效:在调试时,我注意到canvas.ActualWidth和canvas.ActualHeight在调用和排列调用之前为零。我想在这种情况下(有一个画布是Canvas的孩子),_addCanvas的RenderTransformOrigin是相对于画布设置的。由于当我将_addCanvas放在其子列表中时,canvas的ActualWidth和ActualHeight都为零,因此_addCanvas会围绕原点旋转,而不是围绕它的中心旋转。
If you insert _addCanvas into the list of children after canvas has been measured and arranged, canvas will have non-zero values for ActualWidth and ActualHeight, and _addCanvas will be rotated about its center as desired.
如果在测量和排列画布后将_addCanvas插入子画面列表中,画布将为ActualWidth和ActualHeight设置非零值,并且_addCanvas将根据需要围绕其中心旋转。
#1
0
The solution to my problem was to add the _addCanvas as a child of canvas after I had called canvas.Measure and canvas.Arrange.
我的问题的解决方案是在调用canvas.Measure和canvas.Arrange之后将_addCanvas添加为canvas的子项。
I'm not 100% on why this was necessary, but here is why I think this worked: While debugging, I noticed that the canvas.ActualWidth and canvas.ActualHeight are zero before the calls to measure and arrange. I guess that in this scenario (having a Canvas that's a child of a Canvas), the RenderTransformOrigin for _addCanvas was being set relative to canvas. Since the ActualWidth and ActualHeight of canvas were both zero when I put _addCanvas in its list of children, _addCanvas was getting rotated about the origin, instead of about its center.
我不是百分之百为什么这是必要的,但这就是为什么我认为这有效:在调试时,我注意到canvas.ActualWidth和canvas.ActualHeight在调用和排列调用之前为零。我想在这种情况下(有一个画布是Canvas的孩子),_addCanvas的RenderTransformOrigin是相对于画布设置的。由于当我将_addCanvas放在其子列表中时,canvas的ActualWidth和ActualHeight都为零,因此_addCanvas会围绕原点旋转,而不是围绕它的中心旋转。
If you insert _addCanvas into the list of children after canvas has been measured and arranged, canvas will have non-zero values for ActualWidth and ActualHeight, and _addCanvas will be rotated about its center as desired.
如果在测量和排列画布后将_addCanvas插入子画面列表中,画布将为ActualWidth和ActualHeight设置非零值,并且_addCanvas将根据需要围绕其中心旋转。