Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit Image into PictureBox

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.

like image 823
Karlx Swanovski Avatar asked May 29 '13 19:05

Karlx Swanovski


People also ask

How to fit the image in PictureBox in c#?

Try setting the PictureBox. SizeMode Property (System. Windows. Forms) | Microsoft Docs[^] to StretchImage instead of AutoSize - it sizes the image to the container, instead of the container to the image.

How do you make a picture fit into a box?

If you use the Windows Forms PictureBox control on a form, you can set the SizeMode property on it to: Align the picture's upper left corner with the control's upper left corner. Center the picture within the control. Adjust the size of the control to fit the picture it displays.

How do I resize an image in Visual Studio?

Hold down the Shift key and drag a sizing handle until the image is the right size.


2 Answers

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.

like image 130
David C Avatar answered Oct 19 '22 08:10

David C


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

pictureBox1.ImageLocation = @"C:\Users\Desktop\mypicture.jpg"; pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage; 
like image 20
Chandima Avatar answered Oct 19 '22 08:10

Chandima