C# 显示从数据库检索的图像

C# 显示从数据库检索的图像,c#,asp.net,sql-server,image,C#,Asp.net,Sql Server,Image,这是我在DB的图像控件中插入和显示图像的代码: try { Byte[] imgbyte = null; if (ImageUpload.HasFile && ImageUpload.PostedFile != null) { HttpPostedFile file = ImageUpload.PostedFile; imgbyte = new Byte[file.ContentLength]; file.I

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

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();
    }
}
图像未显示的问题有一个小图标,如图所示:


谢谢

我知道您没有使用EF6,但也许您可以将其与我的服务器上发布的一种方法进行比较,并且运行良好。检查类型


你能从数据库中保存图像吗?那么有效吗?如果从浏览器中打开ashx,它会显示什么?图像的长度是多少?如果你保存它并用记事本打开它,它会显示什么?-图像保存在DB中,问题是它不会出现。我有这个图标上面。浏览器中的ashx我该怎么做-根据我选择的图像进行长度调整-我如何用notpad打开图像??谢谢在浏览器中打开url,就像在html中看到它一样。你是说什么意思????????因为它不显示ppr=…在HTML中是由代码创建的img标记。从src属性中获取url并将其输入到浏览器中。我不理解您的代码,但我认为它与mine@Meryem:他正在使用MVC和EF,这是一种更新的东西,在某些情况下可能更好。如果您想继续使用HttpHandler,那没关系。HttpHandler的开销比MVC小。
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;
        }
    }
}
 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");
            }

        }

    }