简体   繁体   English

从数据库检索显示图像

[英]Display Image retrieving from DB

This is my code to Insert and display an image in an Image control from DB: 这是我的代码,用于在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();
    }
}

and this is my class .ashx : 这是我的课程.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;
        }
    }
}

the problem that image is not displayed there is a little Icon as shown in the picture : 图片未显示的问题是有一个小图标,如图所示: 在此处输入图片说明

Thank you 谢谢

I know you are not using EF6, but maybe you can compare with one method released on my server and working fine. 我知道您没有使用EF6,但是也许您可以将其与服务器上发布的一种方法进行比较,并且工作正常。 Check the types... 检查类型...

在此处输入图片说明

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

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM