c# - How to convert binary data in Sql Server database back to image.used Memory Stream. It saved and now trying to retrieve from Sql Server -
private void btnsave_click(object sender, eventargs e) { sqlconnection con = new sqlconnection(connect.connectionstr); sqlcommand command = new sqlcommand(); sqldataadapter sda = new sqldataadapter(); try { con.open(); command.connection = con; if (txtfullname.text == "") { /// } else if (txtusername.text == "") { /// } else if (txtpassword.text == "") { // } else if (txtconfirm.text == "") { // } //else if (picstaffimage.image == null) //{ // messagebox.show("please upload image"); //} else if (cmbstationoptn.text == "") { // } else { image img = picstaffimage.image; byte[] arr; imageconverter converter = new imageconverter(); arr = (byte[])converter.convertto(img, typeof(byte[])); string passinsert = " insert tblusernameandpassword " + "(fullname,username,password,confirmpassword,image,stationofoperation)" + "values ('" + txtfullname.text + "', " + "'" + txtusername.text + "'," + " '" + txtpassword.text + "'," + " '" + txtconfirm.text + "' ," + " '"+ arr +"', " + " '" + cmbstationoptn.text + "' ) "; command = new sqlcommand(passinsert, con); command.executereader(); messagebox.show("username , password has been saved"); } } catch (exception ex) { messagebox.show(ex.message); } { con.close(); }
i created class this
using system.drawing.imaging; namespace sddapp { class imageconvert { public static image bytearraytoimage(byte[] bytearrayin) { using (memorystream stream = new memorystream(bytearrayin) { return image.fromstream(stream); } } } }
now reading binary data sql server database using sqldatareader.
private void btnsearch_click(object sender, eventargs e) { sqlconnection con = new sqlconnection(connect.connectionstr); sqlcommand command = new sqlcommand(); sqldatareader reader; //in try catch block try { txtconfirm.text = (string)reader["pass$$"]; picstaffimage.image = (image)reader["myimage"]; //get image imageconvert.bytearraytoimage(byte[] bytearrayin); //goes imageconvert retrieve image in byte picstaffimage.image = imageconvert.bytearraytoimage(imageconvert.bytearraytoimage(byte[] bytearrayin); //i want convert image//error best overload method has invalid argument 'bytearraytoimagebyte'has invalid arguments picstaffimage.image = (image)reader.[(picstaffimage.image)]; cmbstationoptn.text = (string)reader["stationofoperation"]; } reader.close(); command.dispose(); }
most of code inside try
block invalid. bytearraytoimage
method looks fine. change try catch this:
//in try catch block try { txtconfirm.text = (string)reader["pass$$"]; picstaffimage.image = imageconvert.bytearraytoimage((byte[])reader["myimage"]); cmbstationoptn.text = (string)reader["stationofoperation"]; } catch {}
you missing close parentheses in using line.
public static image bytearraytoimage(byte[] bytearrayin) { using (memorystream stream = new memorystream(bytearrayin)) ^ { return image.fromstream(stream); } }
Comments
Post a Comment