简体   繁体   English

在C#中检测PictureBox的图像更改

[英]Detect PictureBox's image change in C#

I have 2 windows, Register and User Profile. 我有2个窗口,注册和用户个人资料。 In Register, user can upload their profile picture and save to MySQL database as BLOB. 在注册中,用户可以上传个人资料图片并以BLOB格式保存到MySQL数据库。 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. 我希望如果PictureBox的图像发生变化,请执行上述代码,否则请忽略上述代码。

But how do I do it? 但是我该怎么办呢? Is it possible to use IF statement? 是否可以使用IF语句?

Thank you. 谢谢。

PictureBox has an event BackgroundImageChanged . PictureBox有一个事件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. 如果检查FileNamefototext.Text是否为空,那应该很棒。 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. 这也将要求您根据是否提供了新文件来动态添加SQL的“图像”位。

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. 如果校验和已更改,则图像已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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