欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#在sql中存取图片image示例

程序员文章站 2024-02-23 18:05:40
(1)控制台应用程序下演示插入图片复制代码 代码如下:public void insertimg(){//将需要存储的图片读取为数据流filestream fs = new...

(1)控制台应用程序下演示插入图片

复制代码 代码如下:

public void insertimg()
{
//将需要存储的图片读取为数据流
filestream fs = new filestream(@"e:\c.jpg", filemode.open,fileaccess.read);
byte[] btye2 = new byte[fs.length];
fs.read(btye2 , 0, convert.toint32(fs.length));
fs.close();

using (sqlconnection conn = new sqlconnection(sqlconnstr))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "insert into t_img(imgfile) values(@imgfile)";
sqlparameter par = new sqlparameter("@imgfile", sqldbtype.image);
par.value = bt;
cmd.parameters.add(par);

int t=(int)(cmd.executenonquery());
if (t > 0)
{
console.writeline("插入成功");
}
conn.close();
}
}

(2)控制台应用程序下读出并生成图片到物理位置

复制代码 代码如下:

public void read()
{
byte[] mydata = new byte[0];
using (sqlconnection conn = new sqlconnection(sqlconnstr))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select * from t_img";
sqldatareader sdr = cmd.executereader();
sdr.read();
mydata = (byte[])sdr["imgfile"];//读取第一个图片的位流
int arraysize= mydata.getupperbound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限

filestream fs = new filestream(@"c:\00.jpg", filemode.openorcreate, fileaccess.write);
fs.write(mydata, 0, arraysize);
fs.close();   //-- 写入到c:\00.jpg。
conn.close();
console.writeline("读取成功");//查看硬盘上的文件
}
}

(3)web下picshow.aspx页将图片读取出来并写入到浏览器上呈现

复制代码 代码如下:

public void read()
{
byte[] mydata = new byte[0];
using (sqlconnection conn = new sqlconnection(sqlconnstr))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select * from t_img";
sqldatareader sdr = cmd.executereader();
sdr.read();
mydata = (byte[])sdr["imgfile"];
response.contenttype = "image/gif";
response.binarywrite(mydata);
conn.close();
response.write("读取成功");
}

(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例

复制代码 代码如下:

<img src="picshow.aspx" width="500" height="300" />

 (5)winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上

(6)winform下读取图片在picturebox控件中显示出来

方法一:利用memorystream 和system.drawing.image

复制代码 代码如下:

public void read()
{
byte[] mydata = new byte[0];
using (sqlconnection conn = new sqlconnection(sqlconnstr))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select * from t_img";
sqldatareader sdr = cmd.executereader();
sdr.read();
mydata = (byte[])sdr["imgfile"];

memorystream mystream = new memorystream(mydata);
//用指定的数据流来创建一个image图片
system.drawing.image img = system.drawing.image.fromstream(mystream, true);

system.windows.forms.picturebox picbox = new picturebox();
picbox.image = img;
picbox.left = 30;
picbox.top = 80;
picbox.width = 800;
picbox.height = 500;
this.controls.add(picbox);

mystream.close();
conn.close();
}
}

方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现

复制代码 代码如下:

void read()
{
using (sqlconnection conn = new sqlconnection(sqlconnstr))
{
conn.open();
sqlcommand cmd = new sqlcommand();
cmd.connection = conn;
cmd.commandtext = "select * from t_img";
sqldatareader sdr = cmd.executereader();
sdr.read();

byte[] image_img = (byte[])sdr["imgfile"];
if (image_img.length == 0)
{
return;
}
int filelength = image_img.length;
string imagename = "1.jpg";
string myurl = environment.currentdirectory + "\\" + imagename;
filestream fs = new filestream(myurl, filemode.openorcreate,fileaccess.write);
binarywriter bw = new binarywriter(fs);
bw.basestream.write(image_img, 0, filelength);
bw.flush();
bw.close();
system.windows.forms.picturebox picbox = new picturebox();

//为picbox添加图片方法一
//picbox.imagelocation = myurl;
//picbox.width = 800;
//picbox.height = 300;

 

//为picbox添加图片方法二
bitmap bitmap = new bitmap(myurl);
picbox.width = 100;//bitmap.width;
picbox.height = 80;//bitmap.height;
picbox.image = (image)bitmap;
picbox.sizemode = system.windows.forms.pictureboxsizemode.stretchimage;
picbox.left = 20;
picbox.top = 30;

this.controls.add(picbox);
conn.close();

}
}