0

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?

4

1 回答 1

0

根据此处的文档:http: //msdn.microsoft.com/en-us/library/93z9ee4x.aspx,如果提供的流为空,Image.FromStream 将引发参数异常(检查您的代码,它不会t be) 或不包含有效图像。我怀疑是后者。

这是我要尝试的:就在您在这里的代码中获取字节数组之后:

byte[] image = ((byte[])dt.Rows[0]["UserImage"]);

将图像数据写入本地文件并检查它 - 我怀疑你会发现它不是图像。如果是这种情况,那么您的问题就变成了“为什么没有上传有效的图像?”。

于 2013-05-13T21:19:01.847 回答