• ViewState Base64保存图片


            protected System.Drawing.Image image = null;
            protected System.IO.MemoryStream ms = null;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) 
                {
                    try  //初始化 将图片写入隐藏域中
                    {
                        string path = Server.MapPath("~/images/tianxia.png");
                        image = System.Drawing.Image.FromFile(path);
                        ms = new System.IO.MemoryStream();
                        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        byte[] buffer = ms.ToArray();
                        string str = Convert.ToBase64String(buffer);
                        hiddenImg.Value = str;
                        ms.Dispose();
                    }                
                    finally
                    {
                        if (ms!=null)
                        {
                            ms.Dispose();
                        }
                        if (image!=null)
                        {
                            image.Dispose();
                        }
                    }
                }
                else
                {
                    try  //PostBack 回发,将编码后的字符从隐藏域中取出进行解码
                    {
                        string str = hiddenImg.Value;
                        byte[] buffer = Convert.FromBase64String(str);
                        ms = new System.IO.MemoryStream();
                        ms.Write(buffer, 0, buffer.Length);
                        image = System.Drawing.Image.FromStream(ms);
                        image.Save(Server.MapPath("~/images/1.png"));
                    }
                    finally
                    {
                        if (ms != null)
                        {
                            ms.Dispose();
                        }
                        if (image != null)
                        {
                            image.Dispose();
                        }
                    }
                }
            }
  • 相关阅读:
    Singleton patterns 单件(创建型模式)
    JS的运算问题……
    Java 新手学习日记一
    pycharm远程调试配置
    MATLAB2010安装方法
    人生三境界
    SAS数据步与过程步,数据步语句
    Google Chrome浏览器调试功能介绍
    认识Java标识符
    java多态和继承
  • 原文地址:https://www.cnblogs.com/lztkdr/p/ViewState.html
Copyright © 2020-2023  润新知