Detect PictureBox's image change in C#

时间:2017-07-10 15:26:35

标签: 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.

3 个答案:

答案 0 :(得分:1)

PictureBox有一个事件BackgroundImageChanged。你可以尝试这个活动。

答案 1 :(得分:1)

看起来你只是盲目地创建你的字节数据。如果您检查FileNamefototext.Text是否空虚,那么您应该很棒。例如:

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

或者如果你知道它应该是一个文件,你可以做

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

然后不会尝试空或无效的条目。虽然你可能想要一个例外,如果他们确实输入了一些东西但没有给出正确的路径。如果你做了那个检查,它会默默地失败。

这还需要您动态添加SQL的“图像”位,具体取决于是否已显示新文件。

答案 2 :(得分:0)

选项是返回原始图像的校验和,并将其与当前图像的校验和进行比较。如果校验和已更改,则图像已更改。

相关问题