显示从DB检索的图像

时间:2014-02-12 12:46:10

标签: c# asp.net sql-server image

这是我在DB中的Image控件中插入和显示图像的代码:

try
{
    Byte[] imgbyte = null;
    if (ImageUpload.HasFile && ImageUpload.PostedFile != null)
    {
        HttpPostedFile file = ImageUpload.PostedFile;
        imgbyte = new Byte[file.ContentLength];
        file.InputStream.Read(imgbyte, 0, file.ContentLength);
    }
         if (c.cn.State == ConnectionState.Closed)
                            {
                                c.cn.Open();
                            }

        c.cmd = c.cn.CreateCommand();
        c.cmd.CommandText = "uploadImage";
        c.cmd.CommandType = CommandType.StoredProcedure;

        c.cmd.Parameters.Add("@ppr", SqlDbType.Int);
        c.cmd.Parameters.Add("@imagename", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@imagecontent", SqlDbType.VarChar);
        c.cmd.Parameters.Add("@imagebinary", SqlDbType.Image);
        c.cmd.Parameters.Add("@TypeOperation", SqlDbType.Int);

        c.cmd.Parameters["@ppr"].Value = Session["Code"];
        c.cmd.Parameters["@imagename"].Value = ImageUpload.FileName;
        c.cmd.Parameters["@imagecontent"].Value = ImageUpload.PostedFile.ContentType;
        c.cmd.Parameters["@imagebinary"].Value = imgbyte;
        c.cmd.Parameters["@TypeOperation"].Value = 0;
        int id = c.cmd.ExecuteNonQuery();
        Label3.Text = ("id is   <br>" + id);
    Response.Write("Yosh!!!!!!!!!!");
    Image1.ImageUrl = "~/Handlerr.ashx?ppr=" + id ;

}
catch (Exception ex)
{
    Response.Write(ex.Message);
}
finally
{
    if (c.cn.State == System.Data.ConnectionState.Open)
    {
        c.cn.Close();
    }
}

这是我的班级.ashx:

public class Handlerr : IHttpHandler
{
    Connexion c = new Connexion();
    public void ProcessRequest(HttpContext context)
    {

        //if (context.Request.QueryString["ppr"] != null)
          int  ppr = Convert.ToInt32(context.Request.QueryString["ppr"]);
        //else
            //throw new ArgumentException("No param specified");

        context.Response.ContentType = "image/jpeg";
        Stream st = DisplayImage(ppr);
        byte[] buffer = new byte[4096];
        int byteseq = st.Read(buffer, 0, 4096);
        while (byteseq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteseq);
            byteseq = st.Read(buffer, 0, 4096);
        }

    }
    public Stream DisplayImage(int ppr)
    { 
            SqlConnection cc = new SqlConnection(ConfigurationManager.ConnectionStrings["CVtechConnectionString"].ToString());
        //c.cmd = c.cn.CreateCommand();
        string sql = "Select ImageBinary from ImageStoragee where ImageID=@p_pr ";
        SqlCommand cm = new SqlCommand(sql, cc);
     cm.CommandType = CommandType.Text;
         cm.Parameters.AddWithValue ("@p_pr" , ppr);
        if (c.cn.State == ConnectionState.Closed)
        {
            cc.Open(); //
        }
        cm.ExecuteReader();
        try
        {
             DataClasses1DataContext context1 = new DataClasses1DataContext();
                var r = (from a in context1.ImageStoragee where a.PPR == ppr select a).First();
                return new MemoryStream(r.ImageBinary.ToArray()); 
        }
        catch
        {
            return null;
        }
        finally
        {
            if (cc.State == ConnectionState.Open)
            {
                cc.Close();
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

图像未显示的问题有一个小图标,如图所示: enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

我知道您没有使用EF6,但也许您可以与我服务器上发布的一种方法进行比较并正常工作。检查类型......

enter image description here

 public ActionResult GetImageUser(Int64 id_User)
    {
        try
        {
            return File(db.DUser_Image.Find(id_User).file_Data, "image/jpeg", "userimage");
        }
        catch (Exception) 
        {
            return Content("0|Image not found!");
        }
    }

    public ActionResult UploadImageUser(Int64 id_User) 
    {

         ImageConverter converter = new ImageConverter();

        Image img = System.Drawing.Image.FromStream(Request.InputStream);

        try
        {
            var dUser_Image = db.DUser_Image.Find(id_User);

            dUser_Image.file_Data = (byte[])converter.ConvertTo(img, typeof(byte[]));

            var entry = db.Entry(dUser_Image);
            entry.Property(e => e.file_Data).IsModified = true;

            db.SaveChanges();

            return Content("1");

        }
        catch (Exception)
        {

            DUser_Image dUser_Image = new DUser_Image();

            try
            {

            dUser_Image.id_User_Image = id_User;
            dUser_Image.file_Data = (byte[])converter.ConvertTo(img, typeof(byte[]));

            db.DUser_Image.Add(dUser_Image);
            db.SaveChanges();

            return Content("1");

            }
            catch (Exception) 
            {
                return Content("0");
            }

        }

    }