简体   繁体   中英

Detect PictureBox's image change in C#

I have 2 windows, Register and User Profile. In Register, user can upload their profile picture and save to MySQL database as BLOB. After user login, he can update all personal information including his profile picture.

This is my code to update the profile picture:

void UpdateProfileClick(object sender, EventArgs e)
{
    FileStream fs;
    BinaryReader br;

    if(usernameUser.Text=="" || passwordUser.Text=="" || namaLengkapUser.Text=="" || tglLahirUser.Text=="" || pekerjaanUser.Text=="" || alamatUser.Text=="" || noHpUser.Text=="" || foto.Image==null )
    {
        MessageBox.Show("Data belum lengkap. Isilah semua data anda.", "Register Gagal", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    koneksi.Open();

    string FileName = fototext.Text;
    byte[] ImageData;
    fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
    br = new BinaryReader(fs);
    ImageData = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close();

    MySqlCommand cmd = new MySqlCommand("update user set nama=@nama, password=@password, tanggal_lahir=@tanggal_lahir, pekerjaan=@pekerjaan, alamat=@alamat, no_hp=@no_hp, image=@image where username=@username", koneksi);
    cmd.Parameters.AddWithValue(@"nama", namaLengkapUser.Text);
    cmd.Parameters.AddWithValue(@"password", passwordUser.Text);
    cmd.Parameters.AddWithValue(@"tanggal_lahir", tglLahirUser.Text);
    cmd.Parameters.AddWithValue(@"pekerjaan", pekerjaanUser.Text);
    cmd.Parameters.AddWithValue(@"alamat", alamatUser.Text);
    cmd.Parameters.AddWithValue(@"no_hp", noHpUser.Text);
    cmd.Parameters.AddWithValue(@"username", usernameUser.Text);
    cmd.Parameters.AddWithValue(@"image", ImageData);

    try{
        cmd.ExecuteNonQuery();
        MessageBox.Show("Data profil berhasil di-edit.", "Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information);
        namauser.Text = namaLengkapUser.Text;
    }
    catch (MySqlException ex){
        MessageBox.Show(ex.ToString());
    }
    koneksi.Close();    
} 

The problem is if the user updates their personal information but doesn't update the profile picture, an error appears. So, the user needs to update the profile picture in order to prevent the error.

So, this is the code that matters.

string FileName = fototext.Text;
byte[] ImageData;
fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();

I want that if PictureBox 's image change, execute the above code, else ignore the above code.

But how do I do it? Is it possible to use IF statement?

Thank you.

PictureBox has an event BackgroundImageChanged . You can try with this event.

It looks like you're just blindly creating your byte data regardless. if you check FileName or fototext.Text for emptyness you should be great. For example:

if (!string.IsNullOrWhitespace(FileName))
{
    //do your stuff
}

or if you know it should be a file, you could do

if (File.Exists(FileName))
{
    //do your stuff
}

Empty or invalid entries will not be attempted then. Although you'd probably want an exception if they did type something but didn't give the right path. if you did that check it would silently fail.

This will also require you to dynamically add the "image" bit of your SQL depending on if a new file has been presented.

An option is to return the checksum of the original image and compare it to the checksum of the current image. If the checksum has changed then the image has changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM