C# about the solution of the picture displayed by the Picturebox control cannot adapt to the size of the control

C# about the solution of the picture displayed by the Picturebox control cannot adapt to the size of the control

Open the folder through the Click event of a Button button and select a picture to insert into the PictueBox. The code is as follows:

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
    
    

                drawBox.Load(dlg.FileName);
            }
        }

After running, you will find that the displayed picture size will exceed the size of the control. The first thing that comes to mind is to modify the BackgroundImageLayout of the Picturebox control property, but the modification does not solve the problem. Think about it carefully, BackgroundImageLayout modifies the layout of the background image of the control, and the inserted image is not the background image of the Picturebox control. In this case, it is useless to modify the BackgroundImageLayout.
By consulting the information, it can be solved by modifying the SizeMode of the Picturebox control property. According to your needs at the time, you can change the SizeMode to StretchImage.

Guess you like

Origin blog.csdn.net/Kevin_Sun777/article/details/108514116