我有一个显示图像的图像查看器。我想用鼠标在图像上绘制一个矩形,得到矩形(X1,X2,Y1和Y2)的x和y坐标。我将使用这些坐标来创建一个搜索区域,并找到一个数组中的最大值和最小值,这些值的精确像素数与两个坐标轴中的图像一致。
任何人都可以引导我开始一个方向吗?使用矩形在图像上创建搜索区域
3
A
回答
2
您应该使用画布来显示图像并在其上绘制矩形。
例子:
MainWindow.xaml:
<Window x:Class="CanvasRectangleSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<Canvas x:Name="SampleImageCanvas"
MouseMove="SampleImageCanvas_MouseMove"
MouseDown="SampleImageCanvas_MouseDown"
Width="512" Height="389">
<Canvas.Background>
<!--Here you set the image to display -> You probably want to bind it to something. -->
<ImageBrush x:Name="SampleImage" Stretch="Uniform" ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg">
</ImageBrush>
</Canvas.Background>
<!-- Here you draw whatever you want on the canvas. -->
<!-- You'll probably want to bind its width and height to something too. -->
<Rectangle x:Name="ROI" Stroke="#FFF1133E" Width="50" Height="50"/>
</Canvas>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
namespace CanvasRectangleSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = this;
InitializeComponent();
}
// Handling the redrawing of the rectangle according to mouse location
private void SampleImageCanvas_MouseMove(object sender, MouseEventArgs e)
{
//get mouse location relative to the canvas
Point pt = e.MouseDevice.GetPosition(sender as Canvas);
//here you set the rectangle loction relative to the canvas
Canvas.SetLeft(ROI, pt.X - (int)(ROI.Width/2));
Canvas.SetTop(ROI, pt.Y - (int)(ROI.Height/2));
}
private void SampleImageCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
//Here you should handle saving the rectangle location
//don't forget to calculate the proportion between Canvas's size and real Image's size.
}
}
}
如果你愿意,你可以限制矩形搬迁到画布区域使用if表情检查画布区域是否包含鼠标位置
1
感谢这个指针ters和帮助: 这是我完成的代码,它的工作原理。将鼠标放在画布上的任意位置,按住鼠标并拖动以创建矩形。它可以使用更多的改进来在任何方向拖拽和创建矩形。
XAML:
<Canvas Name="ImageCanvas"
MouseMove="ImageCanvas_MouseMove"
MouseDown="ImageCanvas_MouseDown"
Height="240" Width="320"
HorizontalAlignment="Left"
Margin="87,514,0,0" VerticalAlignment="Top" MouseLeftButtonUp="ImageCanvas_MouseLeftButtonUp">
<Canvas.Background>
<ImageBrush x:Name="Image" Stretch="Uniform" ImageSource="C:\image.bmp">
</ImageBrush>
</Canvas.Background>
<Rectangle x:Name="ROI" Stroke="#FFF1133E" Width="20" Height="20" Canvas.Left="155" Canvas.Top="115" />
</Canvas>
代码:
double topLeftX = 0;
double topLeftY = 0;
double bottomRightX = 0;
double bottomrigthY = 0;
bool setRect = false;
private void ImageCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
topLeftY = topLeftX = bottomrigthY = bottomRightX = 0;
setRect = true;
System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
topLeftX = pt.X; topLeftY = pt.Y;
}
private void ImageCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (setRect == true)
{
//get mouse location relative to the canvas
System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
Canvas.SetLeft(ROI, topLeftX);
Canvas.SetTop(ROI, topLeftY);
ROI.Width = System.Math.Abs((int)(pt.X - topLeftX));
ROI.Height = System.Math.Abs((int)(pt.Y - topLeftY));
commandReturnTB.Text = (Convert.ToString(pt.X) + "," + Convert.ToString(pt.Y))+"\n";
}
}
private void ImageCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas);
bottomRightX = pt.X;
bottomrigthY = pt.Y;
ROI.Width = System.Math.Abs((int)(bottomRightX - topLeftX));
ROI.Height = System.Math.Abs((int)(bottomrigthY - topLeftY));
Canvas.SetLeft(ROI, topLeftX);
Canvas.SetTop(ROI, topLeftY);
setRect = false;
commandReturnTB.Text = topLeftX + "," + topLeftY + "--" + bottomRightX + "," + bottomrigthY;
}
相关问题
- 1. 使用OpenCV在图像上创建一个矩形区域蒙版
- 2. 对非矩形图像区域使用滑动图像
- 3. 如何使用图像创建矩形?
- 4. 创建图形区域
- 5. 识别图像中的矩形区域
- 6. 谷歌或bing地图中的矩形区域搜索
- 7. 在Google地图上搜索矩形
- 8. 使用mouseDown在图像上创建矩形C++/CLI
- 9. 在图框上绘制矩形 - 如何限制矩形区域?
- 10. 谷歌的radarSearch搜索矩形而不是圆形区域?
- 11. 使用Kinetic.js创建一个矩形的拖放区域
- 12. 使用wxWidget创建一个带有浮雕的矩形区域
- 13. 在emacs的矩形区域内搜索和替换
- 14. 使用ROI(OpenCV)识别区域后在图像上绘制矩形
- 15. 使用OpenCv检测图像中的矩形明亮区域
- 16. 使用选定区域的矩形框来裁剪图像?
- 17. 使用imagemagick从图像裁剪矩形区域
- 18. 使用SDL在C上创建矩形
- 19. 更新图形中的矩形区域
- 20. 在桌面上选择区域矩形
- 21. 在圆形JLabel图像周围创建可点击区域
- 22. 如何在区域形状上使用onMouseOver更改图像?
- 23. 如何使用JavaScript在网页上的图像上创建可点击区域?
- 24. 从图像中提取一个矩形像素区域
- 25. 创建单像素矩形
- 26. WPF在DataBinding的图像上创建矩形标签
- 27. 使用Pygame创建矩形
- 28. C#图形绘制区域上方的透明矩形
- 29. 在图像/矩阵中创建随机白色矩形/阵列
- 30. 如何使用Oxyplot创建方形图区域
嗨用户,我很高兴你找到了你的解决方案。我看到我的回答很有帮助,并且您的解决方案基于此,请将我的答案标记为此主题的答案。 – Seffix 2012-01-07 18:33:28