本文介绍了我想显示多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码来显示数据库中的图像.我想显示数据库中的所有图像,但问题是我仅从数据库中获取了最后插入的图像.

I have this code to display image from database. I want to display all image from database but the problem is i get only the last inserted image from the database.

//presentation layer code
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                FileUpload img = (FileUpload)fuDBImage;
                Byte[] imgByte = null;
                if (img.HasFile && img.PostedFile != null)
                {
                    //To create a PostedFile
                    HttpPostedFile File = fuDBImage.PostedFile;
                    //Create byte Array with file len
                    imgByte = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgByte, 0, File.ContentLength);
                }
                // Insert the employee name and image into db
                BusinessLogic.images obji = new BusinessLogic.images(0,imgByte);

                obji.Add_Image();

            }
            catch (Exception ex)
            {
                lblerror.Text = ex.Message;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Image3.ImageUrl = "showimages.ashx?id=0";
        } 





//handler class to return images
public class showimages : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Int32 id;
            if (context.Request.QueryString["id"] != null)
                id = Convert.ToInt32(context.Request.QueryString["id"]);
            else
                throw new ArgumentException("No parameter specified");

            context.Response.ContentType = "image/jpeg";
            System.IO.Stream strm = ShowImage(id);
            byte[] buffer = new byte[strm.Length];
            int byteSeq = strm.Read(buffer, 0, buffer.Length);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, buffer.Length);
            }
            strm.Close();
        }

        public System.IO.Stream ShowImage(int id)
        {
            BusinessLogic.images obji = new BusinessLogic.images();
            int i = 0;
            obji.id = id;
            obji = obji.Get_Image_By_Id();

            try
            {
                    return new System.IO.MemoryStream((byte[])obji.picture);
            }
            catch
            {
                return null;
            }
        }

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





//business logic code
public class images
    {
        public Int32 id { get; set; }
        public Byte[] picture { get; set; }
        public FileStyleUriParser me;

        public images(Int32 _id, byte[] _picture)
        {
            id = _id;
            picture = _picture;
        }


        public images()
        {
            id = 0;
            picture = null;
        }

        public void Add_Image()
        {
            csDAL objdal = new csDAL();
            List<csparameterlisttype> objparlist = new List<csparameterlisttype>();

            objparlist.Add(new csParameterListType("@pic",System.Data.SqlDbType.Image,picture));

            objdal.executespreturnnd("add_v", objparlist);
        }

        public images Get_Image_By_Id()
        {
            csDAL objdal = new csDAL();
            List<csparameterlisttype> objparlist = new List<csparameterlisttype>();
            images objimages = new images();
            objparlist.Add(new csParameterListType("@id", System.Data.SqlDbType.Int, id));
            using (System.Data.IDataReader dr = objdal.executespreturndr("gett", objparlist))
            {
                while (dr.Read())
                {
                    populate_reader(dr, objimages);
                }
            }
            return objimages;
        }

        private void populate_reader(System.Data.IDataReader dr, images obji)
        {
            obji.id = dr.GetInt32(0);
            obji.picture = (Byte[])dr.GetValue(1);
        }

    }
</csparameterlisttype></csparameterlisttype></csparameterlisttype></csparameterlisttype>

推荐答案


这篇关于我想显示多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 15:22
查看更多