Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void btnChangeImage_Click(object sender, EventArgs e)
- {
- using (var openFileDialogForImgUser = new OpenFileDialog())
- {
- string location = null;
- string fileName = null;
- openFileDialogForImgUser.Filter = "Image Files (*.jpg, *.png, *.gif, *.bmp)|*.jpg; *.png; *.gif; *.bmp|All Files (*.*)|*.*"; // filtering only picture file types
- var openFileResult = openFileDialogForImgUser.ShowDialog(); // show the file open dialog box
- if (openFileResult == DialogResult.OK)
- {
- using (var formSaveImg = new FormSave())
- {
- var saveResult = formSaveImg.ShowDialog();
- if (saveResult == DialogResult.Yes)
- {
- imgUser.Image = new Bitmap(openFileDialogForImgUser.FileName); //showing the image opened in the picturebox
- location = openFileDialogForImgUser.FileName;
- fileName = openFileDialogForImgUser.SafeFileName;
- FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read); //Creating a filestream to open the image file
- int fileLength = (int)fs.Length; // getting the length of the file in bytes
- byte[] rawdata = new byte[fileLength]; // creating an array to store the image as bytes
- fs.Read(rawdata, 0, (int)fileLength); // using the filestream and converting the image to bits and storing it in an array
- MySQLOperations MySQLOperationsObj = new MySQLOperations("localhost", "root", "myPass");
- MySQLOperationsObj.saveImage(rawdata);
- fs.Close();
- }
- else
- openFileDialogForImgUser.Dispose();
- }
- }
- }
- }
- public void saveImage(byte[] rawdata)
- {
- try
- {
- string myConnectionString = "Data Source = " + server + "; User = " + user + "; Port = 3306; Password = " + password + ";";
- MySqlConnection myConnection = new MySqlConnection(myConnectionString);
- string currentUser = FormLogin.userID;
- string useDataBaseCommand = "USE " + dbName + ";";
- string updateTableCommand = "UPDATE tblUsers SET UserImage = @file WHERE Username = '" + currentUser + "';";
- MySqlCommand myCommand = new MySqlCommand(useDataBaseCommand + updateTableCommand, myConnection);
- myCommand.Parameters.AddWithValue("@file", rawdata);
- myConnection.Open();
- myCommand.ExecuteNonQuery();
- myConnection.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- public MySQLOperations(string server, string user, string password)
- {
- this.server = server;
- this.user = user;
- this.password = password;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement