Fit Image into PictureBox

187,970

Solution 1

First off, in order to have any image "resize" to fit a picturebox, you can set the PictureBox.SizeMode = PictureBoxSizeMode.StretchImage

If you want to do clipping of the image beforehand (i.e. cut off sides or top and bottom), then you need to clearly define what behavior you want (start at top, fill the height of the pciturebox and crop the rest, or start at the bottom, fill the height of the picturebox to the top, etc), and it should be fairly simple to use the Height / Width properties of both the picturebox and the image to clip the image and get the effect you are looking for.

Solution 2

Use the following lines of codes and you will find the solution...

pictureBox1.ImageLocation = @"C:\Users\Desktop\mypicture.jpg";
pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;

Solution 3

Have a look at the sizemode property of the picturebox.

pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;

Solution 4

Imam Mahdi aj SizeMode Change in properties

You can use the properties section

Solution 5

You can set picturebox's SizeMode property to PictureSizeMode.Zoom, this will increase the size of smaller images or decrease the size of larger images to fill the PictureBox

Share:
187,970
Karlx Swanovski
Author by

Karlx Swanovski

Updated on October 09, 2021

Comments

  • Karlx Swanovski
    Karlx Swanovski over 2 years
    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
        myDatabaseConnection.Open();
        using (SqlCommand SqlCommand = new SqlCommand("Select Photo from Employee where EmpID LIKE '%' + @EmpID + '%' ", myDatabaseConnection))
        {
            SqlCommand.Parameters.AddWithValue("@EmpID", textBox1.Text);
            var DS = new DataSet();
            var adapter = new SqlDataAdapter(SqlCommand);
            adapter.Fill(DS, "Images");
    
            var imagesTable = DS.Tables["Images"];
            var imagesRows = imagesTable.Rows;
            var count = imagesRows.Count;
    
            if (count <= 0) return;
    
            var imageColumnValue =
                imagesRows[count - 1]["Image"];
            if (imageColumnValue == DBNull.Value)
                return;
    
            var data = (Byte[])imageColumnValue;
            using (var stream = new MemoryStream(data))
            {
                pictureBox1.Image = Image.FromStream(stream);
            }
    
        }
    }
    

    If the image is too large for the picturebox to fit. What is the code to make the image fit in the picturebox?

    My picturebox is squared, if the image is rectangular how to it crop and show it in the picturebox like this, the lower part of the picture will be removed.

  • ToolmakerSteve
    ToolmakerSteve about 3 years
    BTW, I encountered a strange bug with Zoom. Even though it was set to Zoom in Visual Studio, after setting Image field in code, I had to set SizeMode to Zoom again in code. Otherwise, it didn't shrink a large image. (I verified with a breakpoint that it was indeed Zoom when it set the Image, but it didn't shrink until I ran again, with the line of code that set Zoom again. Tested multiple times over two days, to make sure this really was happening.)