如何使用x和y坐标在c#wpf画布中绘制一个简单点?
我正在绘制一个wpf画布中的五角形,它工作正常,但我真的不知道如何在画布中绘制一个简单的点/点。我问过谷歌,我找不到合适/简单的答案。如何使用x和y坐标在c#wpf画布中绘制一个简单点?
private void Button_Click(object sender, RoutedEventArgs e)
{
string x = xCoo.Text;
string y = yCoo.Text;
commandText = "Select f.p_ID, f.bezeichnung from figure05 f " +
"where SDO_CONTAINS(f.shape, " +
"SDO_GEOMETRY(2001, NULL, " +
"SDO_POINT_TYPE("+Convert.ToInt32(x)+", "+Convert.ToInt32(y)+", NULL), NULL, NULL" +
")) = 'TRUE';";
using (OleDbConnection conn = new OleDbConnection(cs))
{
try
{
conn.Open();
MessageBox.Show("Connection open!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
cmd = new OleDbCommand(commandText, conn);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("Point is in Pentagon");
//Drawing point here
}
}
}
我的画布:
<Canvas Name="myCanvas" Background="LightBlue" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="318"/>
感谢任何形式的帮助!
你可以只绘制一个椭圆喜欢这里解释:
也看看以下内容:
编辑: 就像在这里的评论中提到的一个例子如何在位置50,50的椭圆对象画布上画一个像素。
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Name="drawButton" Content="Draw!" Click="DrawButton_OnClick" />
<Canvas Grid.Column="0" Grid.Row="1" Name="myCanvas" Background="LightBlue" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DrawButton_OnClick(object sender, RoutedEventArgs e)
{
var brush = new SolidColorBrush(Colors.Black);
var ellipse = new Ellipse
{
Stroke = brush,
Fill = brush,
StrokeThickness = 1,
Height = 1,
Width = 1,
};
Canvas.SetLeft(ellipse, 50);
Canvas.SetTop(ellipse, 50);
myCanvas.Children.Add(ellipse);
}
}
}
是不是有一个简单的方法来绘制一个点?像点p =新点(x,y)并将其添加到我的画布?我在互联网上找不到任何东西。或者我必须绘制一个椭圆? –
你也可以用一条线画出一个像素。这里是另一个正则表达式:[WPF Canvas - Single Pixel Grid](https://*.com/questions/2916496/wpf-canvas-single-pixel-grid) – ChW
可在画一条线具有零长度,即相同的开始和结束点,一个适当的StrokeThickness,并将StrokeStartLineCap和StrokeEndLineCap设置为Square或Round。 – Clemens