簡體   English   中英

圖片框內的圖像無法保持其縮放比例

[英]Image inside Picture box not maintaining its scale/aspect ratio

我目前在主窗體上有一個圖像(1280px,1024px),用戶可以單擊一個選項在圖像周圍繪制一個框。 當他們單擊此選項時,將打開一個新窗體,其中圖像位於尺寸為526,302的圖片框內,並填充了圖片框(使用了Zoom(縮放),但這使圖像不填充框,而使用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();


    }

}

用戶主窗口

屏幕用戶在繪制圖像時獲取。我需要長寬比保持不變

這只是在黑暗中拍攝的鏡頭,但我認為您可能需要計算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希望這會有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM