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.
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.
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.
Hold down the Shift key and drag a sizing handle until the image is the right size.
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.
Use the following lines of codes and you will find the solution...
pictureBox1.ImageLocation = @"C:\Users\Desktop\mypicture.jpg"; pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With