简体   繁体   English

在 wpf C# 中的图像内的特定坐标中设置 TextBlock

[英]TextBlock Set in a specific Co ordinate inside an image in wpf C#

I'm stuck in a problem .我陷入了一个问题。 I can calculate distance from 2 points .我可以从 2 个点计算距离。 And I want to set the TextBlock after I Draw Line .我想在绘制 Line 后设置 TextBlock 。 Please not that I don't use any Graphics property to write inside image .请注意,我不使用任何 Graphics 属性来写入内部 image 。 I use TextBlock because that is not write inside image .我使用 TextBlock 因为那不是写在 image 里面。 Here is my XAML code ..这是我的 XAML 代码..

 <Canvas Margin="10" IsEnabled="{Binding IsEnableCanvas}"  Visibility="{Binding Path=ISCanvasVisible, Converter={StaticResource Converter}}">
                <!--<Canvas Margin="10,10,0,10" IsEnabled="{Binding IsEnableCanvas}" Background="Gray"  Visibility="{Binding Path=ISCanvasVisible, Converter={StaticResource Converter}}" HorizontalAlignment="Left" Width="150"  >-->
                <Image Source="{Binding DIIMG_For_Folder}"    cal:Message.Attach="[Event MouseDown] = [Action MDownCalCulateDistance_Right($source, $eventArgs)];
                [Event MouseUp] = [Action MUpCalCulateDistance_Right($source, $eventArgs)];
                [Event MouseMove] = [Action MMoveCalCulateDistance_Right($source, $eventArgs)]" Stretch="Uniform" />
                <Line Visibility="{Binding Path=ISLineDistanceVisible, Converter={StaticResource Converter}}"  IsHitTestVisible="False"  X1="{Binding FirstPoint_Right.X}" Y1="{Binding FirstPoint_Right.Y}"
                  X2="{Binding SecondPoint_Right.X}" Y2="{Binding SecondPoint_Right.Y}"
                  Stroke="Red" StrokeThickness="3">
                </Line>
                <TextBlock Text="{Binding Path=DisTanceInMM_Right, Mode=OneWay}" Margin="15" FontSize="20" Foreground="Red" ></TextBlock>
            </Canvas>

As you can See I use property to connect two point and show the Line.如您所见,我使用属性连接两点并显示线。 But You also see The TextBlock I set a fixed point that is Margin 15 .但是你也看到了 TextBlock 我设置了一个固定点,即 Margin 15 。 I want to set when I draw the line after the secondpoint.x and Secondpoint.Y.我想在 secondpoint.x 和 Secondpoint.Y 之后画线时设置。 Here is my C# code这是我的 C# 代码

            try
            {
                if (_firstPointRight.X == 0 && _firstPointRight.Y == 0)
                {
                    System.Windows.Point px1 = e.GetPosition((System.Windows.Controls.Image)sender);

                    _firstPointRight = px1;
                }
                else
                //if (_secondPointRight.X == 0 && _secondPointRight.Y == 0)
                {
                    System.Windows.Point px2 = e.GetPosition((System.Windows.Controls.Image)sender);
                    _secondPointRight = px2;
                    var geometry = new FrameGeometry(DicomDataSet);
                    var patientCoord1 = geometry.TransformImagePointToPatient(new Point2(Convert.ToInt32(_firstPointRight.X), Convert.ToInt32(_firstPointRight.Y)));
                    var patientCoord2 = geometry.TransformImagePointToPatient(new Point2(Convert.ToInt32(_secondPointRight.X), Convert.ToInt32(_secondPointRight.Y)));
                    double distanceInMM = patientCoord1.Distance(patientCoord2);
                    DisTanceInMM_Right = Convert.ToDouble(distanceInMM.ToString("N3"));
                }
            }
            catch (DicomDataException)
            {
                MessageBox.Show("This Dicom File Not Suported", "Alert", MessageBoxButton.OK, MessageBoxImage.Error);
            }

The DisTanceInMM_Righ is the property I set the distance between two points and show the distance using TextBlock. DisTanceInMM_Righ 是我设置两点之间距离并使用 TextBlock 显示距离的属性。 I want to set after Secondpoint can it possible .我想在Secondpoint之后设置是否有可能。 I don't want to use as Margin property or Fixed place TextBlock use.我不想用作 Margin 属性或固定位置 TextBlock 使用。 Please Note I use Caliburn.Micro Framework .请注意我使用 Caliburn.Micro Framework 。

As far as I understood you want your TextBlock to appear after second line point.据我了解,您希望 TextBlock 出现在第二行点之后。 If so you can use Canvas.Left and Canvas.Top attached properties on TextBlock:如果是这样,您可以在 TextBlock 上使用Canvas.LeftCanvas.Top附加属性:

<TextBlock Canvas.Left="{Binding SecondPoint_Right.X}" Canvas.Top="{Binding SecondPoint_Right.Y}" Text="{Binding Path=DisTanceInMM_Right, Mode=OneWay}" FontSize="20" Foreground="Red"></TextBlock>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM