I'm working on this image displaying from database. I'm converting the byte array from database to an image display it on my image tag.
Here is how I upload the image:
$("#uploadBtn").live("click", function () {
$("#uploading")
.ajaxStart(function () {
$(this).show();
})
.ajaxComplete(function () {
$(this).hide();
});
$.ajaxFileUpload
(
{
url: 'AjaxFileUploader.ashx?user=' + userId,
secureuri: false,
fileElementId: 'uploadControl',
dataType: 'json',
data: '{}',
success: function (mydata) {
alert("Image Successfully Uploaded");
$('#imgdefaultphoto').attr('src', 'ImageRetrieval.ashx?user=' + userId); //referencing the ImageRetrieval handler
hideUploadMask();
},
error: function () {
}
}
)
});
I'm having an exception:
Argument exception Parameter is not valid. at the line with (*).
ImageRetrieval.ashx:
public void ProcessRequest(HttpContext context)
{
string userid = context.Request.QueryString["user"];
DBAccess dbacc = new DBAccess();
DataTable dt = dbacc.getImage(userid);
byte[] image = ((byte[])dt.Rows[0]["UserImage"]);
System.Drawing.Image img = byteArrayToImage(image);
MemoryStream stream = new MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
stream.Dispose();
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(data);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); // exception line (*)
return returnImage;
}
Any ideas?