I have a BufferedGraphics painted in the panel. The image is zoomed in and zoomed out. From this buffered graphic images how can i find the mouse position in the image not in the panel.
private void panel2_paint(object sender, PaintEventArgs e)
{
if (bitmap != null)
{
float widthZoomed = panel2.Width / Zoom;
float heigthZoomed = panel2.Height / Zoom;
if (widthZoomed > 30000.0f)
{
Zoom = panel2.Width / 30000.0f;
widthZoomed = 30000.0f;
}
if (heigthZoomed > 30000.0f)
{
Zoom = panel2.Height / 30000.0f;
heigthZoomed = 30000.0f;
}
if (widthZoomed < 2.0f)
{
Zoom = panel2.Width / 2.0f;
widthZoomed = 2.0f;
}
if (heigthZoomed < 2.0f)
{
Zoom = panel2.Height / 2.0f;
heigthZoomed = 2.0f;
}
float wz2 = widthZoomed / 2.0f;
float hz2 = heigthZoomed / 2.0f;
Rectangle drawRect = new Rectangle(
(int)(viewPortCenter.X - wz2),
(int)(viewPortCenter.Y - hz2),
(int)(widthZoomed),
(int)(heigthZoomed));
drawrecX = drawRect.X;
drawrecY = drawRect.Y;
dispwidth = (int)(widthZoomed);
dispheight = (int)(heigthZoomed);
myBuffer.Graphics.Clear(Color.White); //Clear the Back buffer
Console.WriteLine(this.panel2.DisplayRectangle.Width);
Console.WriteLine(this.panel2.DisplayRectangle.Height);
myBuffer.Graphics.DrawImage(bitmap, this.panel2.DisplayRectangle, drawRect, GraphicsUnit.Pixel);
//pictureBox1.Image =
myBuffer.Render(this.panel2.CreateGraphics());
//this.toolStripStatusLabel1.Text = "Zoom: " + ((int)(this.Zoom * 100)).ToString() + "%";
}
}
the above code is the paint part. To find the location of the mouse in the image i tried like this
private void panel2_DoubleClick(object sender, EventArgs e)
{
var mouseArgs = (MouseEventArgs)e;
double Pic_width = dispwidth / panel2.Width;//to find the relative position
double Pic_height = dispheight / panel2.Height;
int xpoint = (int)Pic_width * mouseArgs.X + drawrecX;//drawrecX is the X coordinate from the drawing image
int ypoint = (int)Pic_height * mouseArgs.Y + drawrecY;
}
but this code does not give me the exact position. any idea?
Try this:
private void panel2_DoubleClick(object sender, EventArgs e)
{
var mouseArgs = (MouseEventArgs)e;
int x = mouseArgs.X - drawrecX;
int y = mouseArgs.Y - drawrecY;
var size = ImageSizeWithoutZoom;
var zoomSize = ImageSizeWithZoom;
double xPoint = x * size.X / zoomSize.X;
double yPoint = y * size.Y / zoomSize.Y;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.