【问题标题】:Image inside Picture box not maintaining its scale/aspect ratio图片框内的图像未保持其比例/纵横比
【发布时间】:2014-10-11 09:22:55
【问题描述】:

我目前在主窗体上有一个图像 (1280px,1024px),用户可以单击一个选项在图像周围绘制一个框。当他们单击此选项时,会打开一个新表单,其中包含大小为 526,302 的图片框内的图像并填充图片框(我使用了缩放,但这会使图像无法填充框,使用 Stretch 会这样做但不会保持纵横比。

我想要的是,当用户开始制作方形框时,他们点击的地方将保持 1280,1024px 的比例,此刻,如果我点击图像的右下角,mouse.x 和 mouse .y 出现在 400,500 左右,而不是 1200、1000 左右的预期值。

谁能明白为什么会这样?

这是我的代码。

namespace Valo.CustomDraw
{

/// <summary>
/// Class which controls what happens when the user selected to create a custom view to focus on 
/// with the camera. It creates a form which the user draws a rectabnge on. It's height, width and 
/// starting mouse point are calcualted and sent the main window class to be processed appropriately. 
/// </summary>
public partial class bitmap_Square : Form
{

    #region instance variabls

    public Point p1;                    //starting point of rectangle.
    public Point p2;                    //ending point of rectangle.
    MainForm mainApp;                   //reference to the MainForm which called this form.
    Bitmap bmp;                         //bitmap of the camera imag (1280px, 1024px)
    int [] rectDim = new int [2];       //dimentions of the rectangle (width, height).
    #endregion

    /// <summary>
    /// Constuctor that starts the application. 
    /// It creates the form with bitmap image in and applies the mouse listeners
    /// and pain functionality to the picture box as well as controlling the response
    /// from the 'apply button'.
    /// </summary>
    public bitmap_Square(Bitmap b, MainForm ma)
    {
        //initialses the form object.
        InitializeComponent();

        //assigns the passed in MainForm paramater to the local instance.
        mainApp = ma;
        DoubleBuffered = true;

        //assigns the passed in bitmap from the MainForm to a local instance.
        this.bmp = b;

        MessageBox.Show("" + bmp.Width);
        MessageBox.Show("" + bmp.Height);

        //pb_bitmapImage.Image = scaledBMP;
        pb_bitmapImage.Image = bmp;
        pb_bitmapImage.SizeMode = PictureBoxSizeMode.Zoom;
        //invalidates the picture box so it can be drawn on.
        pb_bitmapImage.Invalidate();
        MessageBox.Show("" + bmp.Width);
        MessageBox.Show("" + bmp.Height);

        //adding the MouseEventHandler and PaintEventHandler.
        pb_bitmapImage.Paint += new System.Windows.Forms.PaintEventHandler(this.Bitmap_Square_Paint);
        pb_bitmapImage.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Bitmap_Square_MouseDown);
        pb_bitmapImage.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Bitmap_Square_MouseMove);
        pb_bitmapImage.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Bitmap_Square_MouseUp);
    }

    /// <summary>
    /// Mouse_Down event handler which deals with what happens when the mouse is pressed down
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Bitmap_Square_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
            p1 = e.Location;
    }

    /// <summary>
    /// Mouse_Down event handler which deals with what happens when the mouse is moved
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Bitmap_Square_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            p2 = e.Location;
            pb_bitmapImage.Invalidate();
        }
    }

    /// <summary>
    /// Mouse_Down event handler which deals with what happens when the mouse is released
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Bitmap_Square_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            p2 = e.Location;
            pb_bitmapImage.Invalidate();
        }
    }

    /// <summary>
    /// Manages the paint method when the mouse is moved and ensures the lines are drawn
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Bitmap_Square_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        //calcuates what lines to draw when tou move your mouse accross the screen and 
        //draws them onto the bitmap inside the picture box.
        if (p1.X > 0 && p1.Y > 0 && p2.X > 0 && p2.Y > 0)
            g.DrawRectangle(Pens.Red, new Rectangle(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y));
    }

    /// <summary>
    /// When pressed, it then calls the method from the MainForm which sets the Custom View and changes
    /// labels etc in on the main form to refect this as well as the calculation to focus the camera.
    /// It works out the width and heigh before calling this method as these values are part of the 
    /// parameter list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_rectApply_Click(object sender, EventArgs e)
    {
        //Rectangle Height
        rectDim[0] = (p2.X - p1.X);
        //Rectangle Width
        rectDim[1] = (p2.Y - p1.Y);

        MessageBox.Show("" + p1.X);
        MessageBox.Show("" + p1.Y);
        MessageBox.Show("" + p2.X);
        MessageBox.Show("" + p2.Y);
        MessageBox.Show("" + rectDim[0]);
        MessageBox.Show("" + rectDim[1]);


        //method call from the MainForm to initiate the changes to the Form to reflect the newly 
        //selected area.);
        mainApp.ApplyScreenSelection("Current View: Custom", p1.X, p1.Y, rectDim[0], rectDim[1]);
        this.Close();


    }

}

【问题讨论】:

    标签: c# forms graphics aspect-ratio


    【解决方案1】:

    这只是在黑暗中拍摄,但我认为您可能需要计算 x 和 y 坐标作为百分比,然后将百分比应用于实际图像尺寸。因此,如果您的选择区域为 526 x 302,并且您选择 x = 450 和 y = 250,则与选择区域相关的百分比坐标为 x = 450 / 526 * 100 = 85.55 和 y = 250 / 302 * 100 = 82.78 .现在我们有 x 和 y 作为百分比,我们可以计算与全尺寸图像相关的选择点,所以 x = 85.55 / 100 * 1280 = 1095.04 和 y = 82.78 / 100 * 1024 = 847.67 希望这会有所帮助:)

    【讨论】:

    • 干杯迈克尔,即使我们不在大学,你仍然是一个很大的帮助:D我今天会试试这个:)
    • 太好了。很高兴帮助:)
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    相关资源
    最近更新 更多