• 小谈c#数据库存取图片的方式


    第一种方式   文件夹与数据库配合

    /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="FUSShopURL">FileUpload对象</param>
        /// <param name="UpladURL">图片要放到的目录名称</param>
        /// <returns>如果FileUpload不为空则返回上传后的图片位置,否则返回为空字符</returns>
        public  static  string  uploadImage(FileUpload FUSShopURL, string UpladURL)
        {
            if (FUSShopURL.HasFile)
            {
                //获取当前的时间,一当作图片的名字
                string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();
                //获取图片的扩展名
                string Extent = System.IO.Path.GetExtension(FUSShopURL.PostedFile.FileName);
                //重命名图片
                fileName += Extent;
                //设置上传图片保存的文件夹
                string dir = System.Web.HttpContext.Current.Server.MapPath(UpladURL);
                //指定图片的路径及文件名
                string path = dir + "\" + fileName;
                //把上传得图片保存到指定的文件加中
                FUSShopURL.PostedFile.SaveAs(path);
                return  fileName;
            }
            else
            {
                return "";
            }
        }

    第二种方式    直接把图片的Base64String码进行存取

    //选择图片
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Title = "请选择客户端longin的图片";
                openfile.Filter = "Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
                if (DialogResult.OK == openfile.ShowDialog())
                {
                    try
                    {
                        Bitmap bmp = new Bitmap(openfile.FileName);
                        pictureBox1.Image = bmp;
                        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                        MemoryStream ms = new MemoryStream();
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                        byte[] arr = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(arr, 0, (int)ms.Length);
                        ms.Close();
                        //直接返这个值放到数据就行了
                        pic = Convert.ToBase64String(arr);
                    }
                    catch { }
                }
            }

    读取的方法也很简单, pic就是我们得到的图片字符串只要我们存储到数据库里,从下面的方法里读取就可以了

    //加载图片
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                   // pic=........这一句换成从数据库里读取就可以了
                    //判断是否为空,为空时的不执行
                    if (!string.IsNullOrEmpty(pic))
                    {
                        //直接返Base64码转成数组
                        byte[] imageBytes = Convert.FromBase64String(pic);
                        //读入MemoryStream对象
                        MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
                        memoryStream.Write(imageBytes, 0, imageBytes.Length);
                        //转成图片
                        Image image = Image.FromStream(memoryStream);
     
                        //memoryStream.Close();//不要加上这一句否则就不对了
     
                        // 将图片放置在 PictureBox 中
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                        this.pictureBox1.Image = image;
                    }
                }
                catch { }
            }

    第三种方式   读成二进制后进行存取

    private void button1_Click(object sender, EventArgs e)
           {
               OpenFileDialog openfile = new OpenFileDialog();
               openfile.Title = "请选择客户端longin的图片";
               openfile.Filter = "Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
               if (DialogResult.OK == openfile.ShowDialog())
               {
                   try
                   {
                       //读成二进制
                       byte[] bytes = File.ReadAllBytes(openfile.FileName);
                       //直接返这个存储到数据就行了cmd.Parameters.Add("@image", SqlDbType.Image).Value = bytes;
     
                       //输出二进制  在这里把数据中取到的值放在这里byte[] bytes=(byte[])model.image;
                       pictureBox1.Image = System.Drawing.Image.FromStream(new MemoryStream(bytes));
                       this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
     
                       // 如果保存成文件:
                       File.WriteAllBytes(@"d:	ext.jpg", bytes);
                   }
                   catch { }
               }
           }
  • 相关阅读:
    @configuration 和 @bean
    js中关键字 const , let , var 的用法区别
    springboot+vue项目demo地址
    快速开发框架下载地址(github)
    搭建vue项目并启动vue项目
    export default{} 和 new Vue()都是什么意思
    反向代理
    [转]python类方法
    linux中waitpid及wait的用法
    mount
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7206737.html
Copyright © 2020-2023  润新知