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;
        }
     

    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值