我有一個顯示圖像的圖像查看器。我想用鼠標在圖像上繪製一個矩形,得到矩形(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