c# - How to save an image into a database? -


i using wpf insert image mysql database. can upload file image control don't know how save it.

here i've done far. emp_image image control displays photo.

private void btn_save_image_click(object sender,...)    {        mysqlconnection cn= new mysqlconnection(connectionstring);         byte[] imagedata;         imagedata=file.readallbytes(emp_img);  //..here error,it says has invalid arguments..//     mysqlcommand= new mysqlcommand("insert dep_table(photo)values(?data)",cn);         cmd.parameters.addwithvalue("?data", imagedata);         cn.open();         cmd.executenonquery();         cn.close(); } 

you need convert image source 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 function:

imagedata = imagetoarray((bitmapsource)emp_img.source); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -