-4

I am using WPF to insert an image to my MySQL database. I can upload the file to image control but I don't know how to save it.

Here is what I've done so far. The emp_image is the image control that displays the photo.

private void btn_save_image_click(object sender,...)   
{   
    Mysqlconnection cn= new mysqlconnection(connectionstring);    
    byte[] imagedata;    
    imagedata=File.ReadAllBytes(emp_img);  //..here is error,it says has invalid arguments..//
    mysqlcommand= new mysqlcommand("insert into dep_table(photo)values(?data)",cn);    
    cmd.parameters.addwithvalue("?data", imagedata);    
    cn.open();    
    cmd.executeNonQuery();    
    cn.close();
}
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
codefan
  • 13
  • 6

2 Answers2

1

you need to convert the image source to byte[] :

public static byte[] ImageToArray(BitmapSource image)
{
    var encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(image));

    using (var stream = new MemoryStream())
    {
        encoder.Save(stream);
        return stream.ToArray();
    }
}

Then, call the function:

imagedata = ImageToArray((BitmapSource)emp_img.Source);
Clemens
  • 123,504
  • 12
  • 155
  • 268
Nikita Shrivastava
  • 2,978
  • 10
  • 20
-1

Seems you are not passing the file path.

Please check File::ReadAllBytes Method

Should be something like

var imagedata=File.ReadAllBytes(filepath);
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99