Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. private void btnChangeImage_Click(object sender, EventArgs e)
  2. {
  3. using (var openFileDialogForImgUser = new OpenFileDialog())
  4. {
  5. string location = null;
  6. string fileName = null;
  7. openFileDialogForImgUser.Filter = "Image Files (*.jpg, *.png, *.gif, *.bmp)|*.jpg; *.png; *.gif; *.bmp|All Files (*.*)|*.*"; // filtering only picture file types
  8. var openFileResult = openFileDialogForImgUser.ShowDialog(); // show the file open dialog box
  9. if (openFileResult == DialogResult.OK)
  10. {
  11. using (var formSaveImg = new FormSave())
  12. {
  13. var saveResult = formSaveImg.ShowDialog();
  14. if (saveResult == DialogResult.Yes)
  15. {
  16. imgUser.Image = new Bitmap(openFileDialogForImgUser.FileName); //showing the image opened in the picturebox
  17. location = openFileDialogForImgUser.FileName;
  18. fileName = openFileDialogForImgUser.SafeFileName;
  19.  
  20. FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read); //Creating a filestream to open the image file
  21. int fileLength = (int)fs.Length; // getting the length of the file in bytes
  22. byte[] rawdata = new byte[fileLength]; // creating an array to store the image as bytes
  23. fs.Read(rawdata, 0, (int)fileLength); // using the filestream and converting the image to bits and storing it in an array
  24.  
  25. MySQLOperations MySQLOperationsObj = new MySQLOperations("localhost", "root", "myPass");
  26. MySQLOperationsObj.saveImage(rawdata);
  27. fs.Close();
  28. }
  29. else
  30. openFileDialogForImgUser.Dispose();
  31. }
  32. }
  33. }
  34. }
  35.  
  36. public void saveImage(byte[] rawdata)
  37. {
  38. try
  39. {
  40. string myConnectionString = "Data Source = " + server + "; User = " + user + "; Port = 3306; Password = " + password + ";";
  41. MySqlConnection myConnection = new MySqlConnection(myConnectionString);
  42. string currentUser = FormLogin.userID;
  43. string useDataBaseCommand = "USE " + dbName + ";";
  44. string updateTableCommand = "UPDATE tblUsers SET UserImage = @file WHERE Username = '" + currentUser + "';";
  45. MySqlCommand myCommand = new MySqlCommand(useDataBaseCommand + updateTableCommand, myConnection);
  46. myCommand.Parameters.AddWithValue("@file", rawdata);
  47. myConnection.Open();
  48. myCommand.ExecuteNonQuery();
  49. myConnection.Close();
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  54. }
  55. }
  56.  
  57. public MySQLOperations(string server, string user, string password)
  58. {
  59. this.server = server;
  60. this.user = user;
  61. this.password = password;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement