图片框内的图像不保持其比例/宽高比

时间:2014-08-18 12:49:46

标签: c# forms graphics aspect-ratio

我目前在主窗体上有一个图像(1280像素,1024像素),用户可以单击一个选项在图像周围绘制一个框。当他们单击此选项时,将打开一个新表单,其中图像位于大小为526,302的图片框内并填充图片框(仅使用了缩放,但是这会使图像不填充框并使用Stretch执行此操作但不保留宽高比。

我想要的是,当用户开始制作方框时,他们点击它将保持1280,1024px的比例,此刻,如果我点击图像的右下角,mouse.x和鼠标.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();


    }

}

Main Window for user

Screen user gets when drawing image. I need the aspect ratio to stay the same

1 个答案:

答案 0 :(得分:0)

这只是在黑暗中拍摄但我认为您可能需要计算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希望这有助于:)