簡體   English   中英

使用Zoom SizeMode從縮放的圖片框中獲取正確的坐標

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

  1. 我將jpg加載到圖片框(在SizeMode中為Zoom)。
  2. 我在圖片框上畫了一個矩形並取得了坐標。
  3. 我打開塗料中的jpg並觀察坐標(在圖片框上繪制矩形的位置)。

當我將矩形坐標(x和y)與繪制坐標進行比較時,它們並不相同。

我將SizeMode更改為Normal,觀察到坐標變為相同,但是圖像大小太大,因此只能部分顯示,因此我想使用Zoom SizeMode屬性。

假設尺寸為2825x3538的圖片,並保持圖片框大小模式為“正常”,則圖片部分顯示在圖片框中。 因此,我將圖片框模式更改為“縮放”(以適合系統屏幕分辨率),並且將其與帶有SizeMode的“普通”模式進行比較時坐標不匹配。

如何獲得相同的坐標?

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