Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
The Code Below when the Button is Clicked grabs jpg or png file etc, and then
places the image in the pictureBox1.

I need help with adding code to the Button Code below that sizes the images to
whatever file size it needs to be when inserted to the database.

For example, If I select a file that is a HD which is 1080 x 1000, but need it
to be resized to let's say 500 x 300 before for inserting into database table.

I need code to auto size images to the size I want to submit to the database.


private void BtnBrowse_Click(object sender, EventArgs e)
{

OpenFileDialog opf = new OpenFileDialog()
opf.Filter = "Please Choose Image(*.jpg; *.png; *.gif)|*jpg; *.png; *.gif";

if (opf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opf.FileName);
}
}





Below is the insert code to database.





//Code to grab image jpeg in pictureBox1
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();


String insertQuery = "INSERT INTO tblPerrisImages(ID, Name, Image) VALUES(@ID,@Name,@img)";

connection.Open();

command = new MySqlCommand(insertQuery, connection);

command.Parameters.Add("@id", MySqlDbType.VarChar, 20);
command.Parameters.Add("@Name", MySqlDbType.VarChar, 200);
command.Parameters.Add("@img", MySqlDbType.Blob);

command.Parameters["@id"].Value = labelid.Text;
command.Parameters["@Name"].Value = txtProductName.Text;
command.Parameters["@img"].Value = img;

if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Data Inserted");

What I have tried:

I tried inserting different formats.
Posted
Updated 20-Dec-17 13:46pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900