欢迎来到 安卓源码空间!
安卓源码空间

                                     c#把图片文件流保存到数据库



1.打开文件对话框过滤器的用法(Filter)
Filter = “图片(.jpg;.bmp)|.jpeg;.jpg;.bmp;.bmp|AllFiles(.)|.
| 前表示图片的解释,后面是过滤的图片类型
如果类型过多,可以合并一起写,要用;隔开
public partial class 保存图片到数据库 : Form
{
public 保存图片到数据库()
{
      InitializeComponent();
}
//导入图片
string pic = “”;
private void button1_Click(object sender, EventArgs e)
{
      OpenFileDialog openfile = new OpenFileDialog()
{
Title = “请选择客户端图片”,
Filter = “图片(.jpg;.bmp)|.jpeg;.jpg;.bmp|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);
            pic = Convert.ToBase64String(arr);
           //直接返这个值放到数据库就行了
           SqlParameter[] sp = new[] {
           new SqlParameter("@pic",pic)
        };
       string sql = $“insert into Pic values(@pic)”;
       bool result = DbHelper.SqlHepler.ExecuteNonQuery(sql, sp);
       MessageBox.Show(result ? “导入成功” : “导入失败!”);
    }
    catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
  }
}
//保存图片
private void button3_Click(object sender, EventArgs e)
{
    Getpic();
}
public void Getpic()
{
    string strsql = $“select pic from pic where id=1”;
    DataTable dt = DbHelper.SqlHepler.GetDataTable(strsql, null);
    if (dt.Rows.Count != 0 || dt != null)
     {
         SaveFileDialog sd = new SaveFileDialog()
     {
    Title = “保存图片”,
     Filter = "
.png|.png |.jpg|.jpg|.jpeg|*.jpeg”,//过滤条件
     FilterIndex = 2,//选中第二个过滤条件
     InitialDirectory = “C://”,//默认初始地址
     RestoreDirectory = true
 };

if (DialogResult.OK == sd.ShowDialog())
            {

                string path = sd.FileName;

                string base64String = dt.Rows[0][0].ToString();//获取图片的字符流
                byte[] Pic = Convert.FromBase64String(base64String);//将base64string 转换为字节数组                    
                FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                fs.Write(Pic, 0, Pic.Length);
                fs.Flush();
                fs.Close();
            }
        }
       

    }
    //将字符流转换为字符串
    public string StreamToString(Stream str)
    {

        StreamReader reader = new StreamReader(str);
        string text = reader.ReadToEnd();
        return text;

    }
    //将字符串转换为文件流
    public string StringToStream(string text)
    {
        byte[] array = Encoding.ASCII.GetBytes(text);
        MemoryStream ms = new MemoryStream(array);
        ms.Position = 0;
        byte[] s = new byte[ms.Length];
        ms.Read(s, 0, (int)ms.Length);
        string ss = Convert.ToBase64String(s);

        return ss;
    }
    //查看图片
    private void button2_Click(object sender, EventArgs e)
    {
      //  int id = 0;
        bool Imgid =int.TryParse(this.textBox1.Text,out int id);
        SelectPic(id);
    }
    public void SelectPic(int id)
    {
        string strsql = $"select pic from pic where id={id} ";
        DataTable dt = DbHelper.SqlHepler.GetDataTable(strsql, null);
        if (dt.Rows.Count != 0||dt!=null)
        {
          string base64String= dt.Rows[0][0].ToString();//获取图片的字符流
          byte[] Pic = Convert.FromBase64String(base64String);//将base64string 转换为字节数组
                                                              //字节数组读入字符流中
            MemoryStream ms = new MemoryStream(Pic,0,Pic.Length);
            ms.Write(Pic,0,Pic.Length);
            Image img = Image.FromStream(ms);

            this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox2.Image = img;
        }
     

    }
}


————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
                            原文链接:https://blog.csdn.net/qq_41942413/article/details/116128334

copyright@ 2020-2028  安卓源码空间网版权所有   

备案号:豫ICP备2023034476号-1号