1.读取本地图片到PictureBox

 public void InageShow(PictureBox PB)
{
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 = ;
ms.Read(arr, , (int)ms.Length);
ms.Close();
//直接返这个值放到数据就行了
string ee = Convert.ToBase64String(arr);
}
catch { }
}
}

2.根据图片路径将本地图片存入数据库

        private void button2_Click(object sender, EventArgs e)
{
//获取用户打开的路径然转换成二进制存入数据库
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP"; if (ofd.ShowDialog() == DialogResult.OK)
{
string filePath = ofd.FileName;//图片路径
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] imageBytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流 string strSql = string.Format("insert into [PointSchool].[dbo].[Table_Image] ([image]) values (@image)");
int count = Write(strSql, imageBytes); if (count > )
{
MessageBox.Show("成功!");
}
else
{
MessageBox.Show("失败!");
}
} }
        private int Write(string strSql, byte[] imageBytes)
{
string connStr = "server=.;database=PointSchool;User =sa; pwd =123"; using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(strSql, conn))
{
try
{
conn.Open();
SqlParameter sqlParameter = new SqlParameter("@image", SqlDbType.Image);
sqlParameter.Value = imageBytes;
cmd.Parameters.Add(sqlParameter);
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (Exception e)
{
throw;
}
}
}
}

3.picturebox的Image 转换成二进制存入数据库

    public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
//将Image转换成流数据,并保存为byte[]
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] byData = new Byte[mstream.Length];
mstream.Position = ;
mstream.Read(byData, , byData.Length);
mstream.Close();
return byData;
}
  string strSql = string.Format("insert into [PointSchool].[dbo].[Table_Image] ([image]) values (@image)");
int count = Write(strSql, BBBB);
if (count > )
{
MessageBox.Show("成功!");
}
else
{
MessageBox.Show("失败!");
}

注:添加方法在上面

4.读取二进制转换成图片

        public void PicboxShow(PictureBox pictureBox2)
{
byte[] imagebytes = null; //打开数据库 SqlConnection con = new SqlConnection("server=.;database=PointSchool;User =sa; pwd =123"); con.Open(); SqlCommand com = new SqlCommand("select top 1* from Table_Image", con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read())
{ imagebytes = (byte[])dr.GetValue(); } dr.Close(); com.Clone(); con.Close(); MemoryStream ms = new MemoryStream(imagebytes); Bitmap bmpt = new Bitmap(ms); pictureBox2.Image = bmpt; } pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;

界面图

C# winfrom 存取图片到数据库(二进制,image)的更多相关文章

  1. sqlite3的图片的(二进制数据)存取操作

    sqlite3的图片的(二进制数据)存取操作   前言 上篇介绍了sqlite3的一些常用插入操作方法和注意事项,在实际项目中遇到了图片缓存的问题,由于服务器不是很稳定,且受到外界环境的干扰(例如断电 ...

  2. 将数据库二进制图片导出显示到EPPlus Excel2007中

    1.EPPlus Excel 控件可以参考我的另一篇博客:http://blog.163.com/pei_huiping/blog/static/206573067201281810549984/ 这 ...

  3. C#图片转换成二进制流并且保存到sql server数据库

    注意:我要存储文件二进制流的列的类型是text,不是image类型. 我已经实现了从数据库中读取text类型的二进制流,,现在就是不知道怎么存进去. 我的部分关键代码: StreamReader sr ...

  4. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  5. [转载]mongoDB学习笔记——存取图片(C#)

    作为一个NoSql数据库的代表,存取多媒体数据,应该是强项吧?那么,图片在mongoDB里是如何存取的呢?(其实,关系型数据库存取图片也一点问题没有,所以我看NoSql的强项不在于是否存储多媒体,而在 ...

  6. mongoDB学习笔记——存取图片(C#)

    作为一个NoSql数据库的代表,存取多媒体数据,应该是强项吧?那么,图片在mongoDB里是如何存取的呢?(其实,关系型数据库存取图片也一点问题没有,所以我看NoSql的强项不在于是否存储多媒体,而在 ...

  7. C#图片存入数据库及其读出显示

    <1>将图片转换成二进制插入数据库 FileStream fs = new FileStream("D:\\Add.ico",FileMode.Open); byte[ ...

  8. [转]C#将image中的显示的图片转换成二进制

    本文转自:http://www.cnblogs.com/shuang121/archive/2012/07/09/2582654.html .将Image图像文件存入到数据库中 我们知道数据库里的Im ...

  9. C#将image中的显示的图片转换成二进制

    原文:C#将image中的显示的图片转换成二进制 1.将Image图像文件存入到数据库中 我们知道数据库里的Image类型的数据是"二进制数据",因此必须将图像文件转换成字节数组才 ...

随机推荐

  1. 「小程序JAVA实战」小程序模板在外部页面引用(20)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-20/ 不知道老铁还有印象吗?当时讲模板的时候,是在当前的页面进行模板的应用,如何外部的方式引用模板 ...

  2. **三元运算和lambda

    三元运算 在说三元运算之前,我们看一段代码: a = 1 if a == 1: name = 'Hello' else: name = 'World' print(name) 输出:Hello 对于这 ...

  3. c++builder自定义控件

    c++builder自定义控件 http://docwiki.embarcadero.com/CodeExamples/XE8/en/RegisterComponents_%28C%2B%2B%29 ...

  4. JDBC中,如何动态的设置查询条件

    今天看JDBC,发现有段代码,可以减少重复的编写查询方法,如下: public List<Goddess> query(List<Map<String, Object>& ...

  5. 清除html中的标记,只留下文字

    /// <summary>/// 清除html中的标记,只留下文字./// </summary>/// <param name="HTML">& ...

  6. Linux实战教学笔记41:企业级SVN版本管理与大型代码上线方案

    第1章 SVN服务实战应用指南 1.1 SVN介绍 1.1.1 什么是SVN(Subversion)? Svn(subversion)是近年来崛起的非常优秀的版本管理工具,与CVS管理工具一样,SVN ...

  7. 75. Sort Colors (Array)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. Tp3.1 文件上传到七牛云

    TP3.1 中不支持Composer 就无法用composer 安装 下载历史的SDK https://github.com/qiniu/php-sdk/releases/tag/v7.0.8 下载下 ...

  9. Opencv 发现轮廓 findContours

    vector<vector<Point>> vec_p; vector<Vec4i> vec_4f; findContours(img_canny1, vec_p, ...

  10. Result Grouping / Field Collapsing-结果分组

    WiKi:http://wiki.apache.org/solr/FieldCollapsing Introduction 字段折叠和结果分组是考虑相同solr功能的两种不同的方式. 字段折叠折叠一组 ...