C#保存图片到数据库并读取显示图片的方法
{
string pathName;
if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
pathName = this.openFileDialog1.FileName;
System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
this.pictureBox1.Image = img;
//将图像读入到字节数组
System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte,0,(int)fs.Length);
fs.Close();
fs = null;
//建立Command命令
string comm = @"Insert into table1(img,name) values(@img,@name)";
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
this.sqlCommand1.CommandText = comm;
this.sqlCommand1.Connection = this.sqlConnection1 ;
//创建Parameter
this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);
this.sqlCommand1.Parameters[0].Value = http://www.jb51.net/ianakin/archive/2012/02/02/buffByte;
this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
this.sqlCommand1.Parameters[1].Value =http://www.jb51.net/ianakin/archive/2012/02/02/pathName.Substring(pathName.LastIndexOf("\\")+1);
try
{
this.sqlConnection1.Open();
this.sqlCommand1.ExecuteNonQuery();
this.sqlConnection1.Close();
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message );
}
buffByte = null;
this.FillListBox();
}
读取:
从数据库读图片到picturebox
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
C#保存图片到数据库并读取显示图片的方法的更多相关文章
- C#从SQL server数据库中读取l图片和存入图片
原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...
- MySQL数据库之插入显示图片
图书馆系统项目需要用到好多图片,并且要求存入到数据库中,对这个特别感兴趣,于是上网查了资料,采用C#语言,进行了具体实现. 说明: 功能:往MySQL数据库插入并显示图片: 验证:执行插入功能后,我把 ...
- Java中显示图片的方法
最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...
- vue中通过定义的数组循环将img的src引入图片却不显示图片问题解决方法
需要前端循环图片数组将其放到页面中去. 需要将src渲染到页面中,如果单纯写src的路径会出现不显示图片的问题 因为图片路径在assets,所以需要require一下.
- QT+OpenGL读取显示图片,OpenGL在QT里的刷新问题(好几篇)
GLuint readImage(char *filename){ GLuint tex_id; GLint alignment; QImage tex, buf; buf.load ...
- Android实例-操作sqlite数据库之Grid显示图片(XE8+小米2)
结果: 1.数据库文件,记得打包到程序中(assets\internal\). 操作方法: 1.新建firemonkey mobile application①菜单->File->New- ...
- OpenCV基础篇之读取显示图片
程序及分析 /* * FileName : read.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 13 May ...
- android打开,保存图片到sd卡,显示图片
1.打开根目录下test.jpg Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getA ...
- 解决CHROME中画布中无法显示图片的方法
最终效果图如下 我按照W3SCHOOL里面的方法,代码如下 <!DOCTYPE html> <html> <body> <script type=" ...
随机推荐
- poj2083 分形(图形的递归)
题目传送门 代码有注释. #include<iostream> #include<algorithm> #include<cstdlib> #include< ...
- django模型层之多表操作 增删改查
多表操作之创建模型 这边以书为中心创建一个模型 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一的关系(on ...
- lnmp 架构
Mysql安装 tar zxf mysql-boost-5.7.17.tar.gz yum install -y gcc gcc-c++ yum install cmake-2.8.12.2-4.el ...
- C++二维数组的动态声明
int **a = new int* [m] //分配一个指针数组,将其首地址保存在a中 . for(int i = 0; i < m; i++) //为指针数组的每个元素分配一 ...
- Python中的数据类型和数据结构
一.数据类型 Python中有六个标准数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) 其中,除列表Lis ...
- Webstrom 中写Vue没有代码提示如何解决?
1. 如果你的Webstorm是2017版的,请更新到最新的2018,2018版本的webstorm自带了vue插件 找到 Help > About,查看你的Webstorm版本 2. 如果你的 ...
- 电量分析工具 Battery Historian 的配置及使用
1.Building from source code(通过各种配置后从源码构建) 官方流程看似很麻烦,但一上手,很快就搞定,让我情何以堪. ps:以下均是参考官方及网友做的 copy https:/ ...
- JS你可能还不知道的一些知识点(一)
js程序是用Unicode字符集编写的, 2.转义字符:反斜线 1 2 3 4 function Test(){ var s='you\'re right,it can\'t be a quote ...
- android Binder机制(一)架构设计
Binder 架构设计 Binder 被设计出来是解决 Android IPC(进程间通信) 问题的.Binder 将两个进程间交互的理解为 Client 向 Server 进行通信. 如下:bind ...
- Android NDK开发 字符串(四)
几个概念首先要明确: java内部是使用16bit的unicode编码(UTF-16)来表示字符串的,无论中文英文都是2字节: jni内部是使用UTF-8编码来表示字符串的,UTF-8是变长编码的un ...