简体   繁体   English

使用Zoom SizeMode从缩放的图片框中获取正确的坐标

[英]Getting correct coordinates from scaled picture box with Zoom SizeMode

  1. I loaded the jpg to picture box (in SizeMode as Zoom). 我将jpg加载到图片框(在SizeMode中为Zoom)。
  2. I drew a rectangle on the picture box and took the coordinate. 我在图片框上画了一个矩形并取得了坐标。
  3. I opened the jpg in paint and observed the coordinate (where I drew the rectangle on the picture box). 我打开涂料中的jpg并观察坐标(在图片框上绘制矩形的位置)。

When I compared the rectangle coordinates (x and y) with paint coordinates, they were not the same. 当我将矩形坐标(x和y)与绘制坐标进行比较时,它们并不相同。

I changed the SizeMode to Normal and observed that the coordinates became the same, but the image size was too large so it was display partially, so I want to use Zoom SizeMode property. 我将SizeMode更改为Normal,观察到坐标变为相同,但是图像大小太大,因此只能部分显示,因此我想使用Zoom SizeMode属性。

Say image with size 2825x3538 and keep the picture box size mode as Normal, the image shows partially in picture box. 假设尺寸为2825x3538的图片,并保持图片框大小模式为“正常”,则图片部分显示在图片框中。 So I changed picture box mode to Zoom (to fit the System screen resolution), and the coordinates missmatched when comparing it with Normal mode with SizeMode. 因此,我将图片框模式更改为“缩放”(以适合系统屏幕分辨率),并且将其与带有SizeMode的“普通”模式进行比较时坐标不匹配。

How can I achieve the same coordinates? 如何获得相同的坐标?

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFD.FileName = "";
    OpenFD.Title = "open image";
    OpenFD.InitialDirectory = "C";
    OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
    if (OpenFD.ShowDialog() == DialogResult.OK)
    {
        file = OpenFD.FileName;
        image = Image.FromFile(file);
        pictureBox1.Image = image;
        svc = Screen.PrimaryScreen;
        pictureBox1.Width = svc.Bounds.Width;
        pictureBox1.Height = svc.Bounds.Height - 100;
        mybitmap1 = new Bitmap(pictureBox1.Image);
        mybitmap1.SetResolution(300, 300);
        pictureBox1.Image = mybitmap1;
    }
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (mybitmap == null)
    {
        mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        mybitmap.SetResolution(300, 300);
    }
 }

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (g = Graphics.FromImage(mybitmap))
    {
        using (Pen pen = new Pen(Color.Green, m))
        {
            e.Graphics.DrawRectangle(pen, r);
            e.Graphics.DrawString(lab[c].ToString(), new Font(lab[c].ToString(), 8F), new SolidBrush(label1.ForeColor), r);
        }
    }
}

您可以在实际图像和图片框之间使用两个比例因子,一个比例因子用于高度,另一个比例因子用于宽度。

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

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