首页 新闻 搜索 专区 学院

图片转化为二进制,二进制转化为图片

0
悬赏园豆:20 [已解决问题] 解决于 2012-04-13 14:48

目前想写个程序将上传的图片文件转换为二进制形式的字符串,让后将他存储在一个隐藏字段中,用的时候去提取,用完后再将它保存到数据库中去,那位大哥大姐们有这方面的程序啊,贴出来参考参考

二进制转换成图片是不要单独写个文件,然后让图片控件的url连接这个文件地址啊?

错误的过客的主页 错误的过客 | 初学一级 | 园豆:183
提问于:2012-04-13 13:25
< >
分享
最佳答案
1

            if (size.Equals("PicSmall"))
                strsql = "update product set PicSmall=@Pic where barcode = '" + barcode + "'";
            else
                strsql = "update product set PicBig=@Pic where barcode = '" + barcode + "'";

            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["Release"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);             

   SqlCommand cmd = new SqlCommand(strsql, conn);
                byte[] imagebyte = new byte[file.PostedFile.InputStream.Length];
                file.PostedFile.InputStream.Read(imagebyte, 0, imagebyte.Length);

                cmd.Parameters.Add("@Pic", SqlDbType.Image).Value = imagebyte;
                conn.Open();
                cmd.ExecuteNonQuery();

收获园豆:20
無限遐想 | 老鸟四级 |园豆:3740 | 2012-04-13 13:34

从数据库中取出,然后在页面显示,怎么在转化回来了呢?是不是要一个单独的文件啊?将用src连接这个单独的文件?

错误的过客 | 园豆:183 (初学一级) | 2012-04-13 13:40

@错误的过客:     public partial class Picture : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strPicField = Request.QueryString["type"] != "1" ? "PicSmall" : "PicBig";

            byte[] myData = new ProductDal().GetProductPic(Request["image"],strPicField);
            Response.ContentType = "image/jpg";
            if (myData.Length > 0)
                Response.OutputStream.Write(myData, 0, myData.Length);
              //Response.BinaryWrite(myData);

            Response.End();
        }
    }

定義一個Picture.aspx文件。或則ashx文件

<asp:ImageButton runat="server" ImageUrl='<%# "~/Modules/Web/Picture.aspx?image="+ Eval("barcode")%>'

應該很清楚了吧。

無限遐想 | 园豆:3740 (老鸟四级) | 2012-04-13 13:44
其他回答(1)
0

public ActionResult SaveStaffImage(string flag)
        {
            string StaffID = Request["staffID"].ToString();
            string func = Request["func"].ToString();
            ISpecification<Staff> condition = new DirectSpecification<Staff>(x => x.ID == StaffID);
            Staff model = _staffSrv.GetByCondition(condition);
            if (model != null)
            {
                foreach (string upload in Request.Files)
                {
                    if (!(Request.Files[upload].ContentLength > 0))
                        continue;
                    Stream fileStream = Request.Files[upload].InputStream;
                    int fileLength = Request.Files[upload].ContentLength;
                    byte[] fileData = new byte[fileLength];
                    fileStream.Read(fileData, 0, fileLength);

                    if (flag == "0")
                    {
                        model.ImageUP = fileData;
                    }
                    else
                    {
                        model.ImageDown = fileData;
                    }
                    int i = _staffSrv.Modify(model);
                }
            }
           return View("EditStaffImage",model);
        }

 

 public FileContentResult GetStaffImage( )
        {
            string StaffID = Request["staffId"].ToString();
            string flag = Request["flag"].ToString();
            byte[] fileContent = null;
            string mimeType = "";
            string fileName = "";

            ISpecification<Staff> condition = new DirectSpecification<Staff>(x => x.ID == StaffID);
            Staff model = _staffSrv.GetByCondition(condition);
            if (model != null)
            {
                if (flag == "0")
                {
                    fileContent = model.ImageUP;
                }
                else
                {
                    fileContent = model.ImageDown;
                }
                mimeType = "image/pjpeg";//没有在数据中添加这个字段  已定义死了
                fileName = "tempImageName";

            }
            return File(fileContent, mimeType, fileName);

        }

 

页面 图片<img src="/EMP/Staff/GetStaffImage?staffId=<%= model.ID %>&flag=0" alt="" />

江—城—子 | 园豆:204 (菜鸟二级) | 2013-07-10 17:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册