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. 使用json格式去call外部系统

    1. 使用postman去call post方式 body填入对应的json请求 格式选json 2. 使用java 代码去call import java.io.BufferedReader; im ...

  2. Dreamweaver基本操作

    1.站点管理 在网站设计前,我们需要先建立站点,再进行网站设计. 站点 作用:用来归纳一个网站上所有的网页.素材及他们之间的联系. 站点根文件夹命名规则:由数字.字母.下划线组成,数字不能在开头. 首 ...

  3. avalon最佳实践

    最近从angular的QQ群与新浪微博拉了许多人来用我的avalon,成为第一批登上方舟,脱离DOM苦海的人.短短三个月内,5群的朋友也搞出几个切实实行的案例了.为应对粉丝们高益高涨的热情,遂放出此文 ...

  4. Spyder kernel died 错误

    Keras 2.2.4版本和 tensorflow1.2.1 版本不兼容导致的错误.降低Keras 为2.1.2版本 import keras 出现: Using TensorFlow backend ...

  5. NPM设置代理

    npm获取配置有6种方式,优先级由高到底. 命令行参数. --proxy http://server:port即将proxy的值设为http://server:port. 环境变量. 以npm_con ...

  6. go_数组

    go语言中数组是值类型 [10]int 和 [20]int 是不同类型,不能用作参数传递 调用func f(arr [10]int)会拷贝数组 go语言一般不用数组用切片slice package m ...

  7. 用java实现一个简易编译器

  8. ios规格证明

    解决的方法是在Info.plist文件中添加:ITSAppUsesNonExemptEncryption 设置为NO

  9. 让tomcat自动定位到项目

    在servelt.xml中添加粗体部分. <Host name="localhost" appBase="webapps" unpackWARs=&quo ...

  10. 关于BigDecimal小记

    昨天在写一个关于金额计算的时候,随手用了BIgDecimal结果出问题了,如下图NO.3那样,期望值是10.00,结果是10.1... 后来发现犯了一个想当然的错误,那就是两个参数的构造方法是这样的, ...