i give two point[(100, 100) and (500, 200)] to crate a rect with wpf streamgeometry. and i use translatetransform to translate the rect in grid center.
but draw the point(400, 400) and (2000, 800) and translate the rect to the grid center, the a right part of the rect was disapper.
How to fix this bug??
draw rect with point[(100, 100) and (500, 200)]
draw rect with point[(400, 400) and (2000, 800)]
private void DrawLineObjectTest(double _fScale = 1.0f)
{
Point ptStart = new Point(100.0f, 100.0f);
Point ptEnd = new Point(500.0f, 200.0f);
double fScale = _fScale;
Path pathDraw1 = new Path();
StreamGeometry sg1 = new StreamGeometry();
sg1.FillRule = FillRule.Nonzero;
pathDraw1.Fill = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0));
pathDraw1.Stroke = Brushes.Red;
pathDraw1.StrokeThickness = 1;
pathDraw1.Fill = Brushes.Red;
using (StreamGeometryContext ctx = sg1.Open())
{
double sx = ptStart.X * _fScale, sy = ptStart.Y * _fScale;
double ex = ptEnd.X * _fScale, ey = ptEnd.Y * _fScale;
ctx.BeginFigure(new Point(sx, (sy)), false, true);
ctx.LineTo(new Point(ex, (sy)), true, false);
ctx.LineTo(new Point(ex, (ey)), true, false);
ctx.LineTo(new Point(sx, (ey)), true, false);
ctx.LineTo(new Point(sx, (sy)), true, false);
}
sg1.Freeze();
Rect boundary = sg1.Bounds;
Point ptCenter = new Point();
ptCenter.X = (boundary.Left + boundary.Right) / 2.0f;
ptCenter.Y = (boundary.Top + boundary.Bottom) / 2.0f;
Point ScreenCenter = new Point(gridDisplay.ActualWidth / 2.0f, gridDisplay.ActualHeight / 2.0f);
TranslateTransform tr1 = new TranslateTransform(-ptCenter.X, -ptCenter.Y);
TranslateTransform tr2 = new TranslateTransform(ScreenCenter.X, ScreenCenter.Y);
TransformGroup tg = new TransformGroup();
tg.Children.Add(tr1);
tg.Children.Add(tr2);
pathDraw1.Data = sg1;
pathDraw1.RenderTransform = tg;
gridDisplay.Children.Add(pathDraw1);
}
xamlfile
<Grid x:Name="gridDisplay" Grid.Column="1" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" MouseLeftButtonDown="gridDisplay_MouseLeftButtonDown" MouseMove="gridDisplay_MouseMove" MouseLeftButtonUp="gridDisplay_MouseLeftButtonUp" Visibility="Visible" PreviewMouseWheel="gridDisplay_PreviewMouseWheel" RenderTransformOrigin="0,0">
<Grid.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#FFC1B1B1" Offset="1"/>
</LinearGradientBrush>
</Grid.OpacityMask>
</Grid>
i try the use Measure and arrange, and it does’t work
Ryan Lin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.