2012-07-12 126 views
0

我正在处理从数据库显示的此图像。我将字节数组从数据库转换为图像显示在我的图像标签上。从数据库中显示图像

这里是我上传的图片:

$("#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() { 

      } 
     } 
    ) 
    }); 

我有一个例外:

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; 
    } 

任何想法?

+0

您是否尝试过自定义触发选项选项? – TheVillageIdiot 2012-07-12 16:22:44

+0

不,我不知道这一点。你有样品吗? – ljpv14 2012-07-12 16:23:16

回答

0

根据此处的文档:http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx,如果提供的流为null(即检查代码,将不会)或不包含有效图像,则Image.FromStream将抛出参数异常。我怀疑后者是这种情况。

这是我想尝试:你在你的代码的字节数组。只需在后:

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

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