.net - C# Displaying Image from Database -
i having issues displaying image sql server database in .net application using c#. i've got save part of image working , storing image series of byes in database, running issues trying display it. here have:
using system; using system.configuration; using system.web; using system.io; using system.data; using system.data.sqlclient; public class showimage : ihttphandler { public void processrequest(httpcontext context) { int32 empno; if (context.request.querystring["id"] != null) empno = convert.toint32(context.request.querystring["id"]); else throw new argumentexception("no parameter specified"); context.response.contenttype = "image/jpeg"; stream strm = showempimage(empno); byte[] buffer = new byte[4096]; int byteseq = strm.read(buffer, 0, 4096); while (byteseq > 0) { context.response.outputstream.write(buffer, 0, byteseq); byteseq = strm.read(buffer, 0, 4096); } //context.response.binarywrite(buffer); } public stream showempimage(int empno) { string conn = codprobs.main.getdsn(); sqlconnection connection = new sqlconnection(conn); string sql = "select coverphoto galleries galleryid = @galleryid"; sqlcommand cmd = new sqlcommand(sql, connection); cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@galleryid", empno); connection.open(); byte[] img = system.text.encoding.unicode.getbytes(convert.tostring(cmd.executescalar())); try { return new memorystream((byte[])img); } catch { return null; } { connection.close(); } } public bool isreusable { { return false; } }
this not resulting in syntax errors , seems should working. after stepping through debugger, can see grabbing proper data database. however, receive error of: "the image ... cannot displayed because contains errors."
any ideas on issue here?
update storing image
public static int addgallery(galleryds galleryds) { datarow gallery = galleryds.tables[0].rows[0]; int result = 0; string sql = @"insert galleries (title, description, gallerycategoryid, createdate, createdby, coverphoto) values (@title, @description, @gallerycategoryid, @createdate, @createdby, @coverphoto) select scope_identity()"; using (sqlconnection conn = new sqlconnection(main.getdsn())) { sqlcommand command = new sqlcommand(sql, conn); command.parameters.addwithvalue("@title", gallery["title"]); command.parameters.addwithvalue("@description", gallery["description"]); command.parameters.addwithvalue("@gallerycategoryid", 0); command.parameters.addwithvalue("@createdate", datetime.now); command.parameters.addwithvalue("@createdby", gallery["createdby"]); command.parameters.add("@coverphoto", sqldbtype.varbinary, int32.maxvalue); command.parameters["@coverphoto"].value = gallery["coverphoto"]; conn.open(); result = convert.toint32(command.executescalar()); conn.close(); } return result; }
the showempimage method shouldn't convert stream , write it. that's waste of time.
change definition to:
public byte[] showempimage(int empno) { string sql = "select coverphoto galleries galleryid = @galleryid"; byte[] result = null; using (sqlconnection conn = new sqlconnection(codprobs.main.getdsn())) { using(sqlcommand cmd = new sqlcommand(sql, conn)) { cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@galleryid", empno); conn.open(); result = (byte[])cmd.executescalar(); } } return result; }
to call use following:
byte[] empimage = null; empimage = showempimage(empno); context.response.buffer = true; context.response.clear(); context.response.contenttype = "image/jpeg"; context.response.expires = 0; context.response.addheader("content-disposition", "attachment;filename=yourimagename.jpg"); context.response.addheader("content-length", empimage.length.tostring()); context.response.binarywrite(empimage);
side note: wrap unmanaged objects using
clause. cleans after , practice. database connections.
Comments
Post a Comment