配色: 字号:
blob读照片
2013-01-06 | 阅:  转:  |  分享 
  
usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

usingSystem.Configuration;

usingSystem.IO;



namespace_5555

{

publicpartialclassForm1:Form

{

publicForm1()

{

InitializeComponent();

}



privatevoidbutton1_Click(objectsender,EventArgse)

{

SqlConnectionconnection=newSqlConnection();

stringconnstr=ConfigurationManager.ConnectionStrings["DataBase"].ToString();

connection.ConnectionString=connstr;

connection.Open();

SqlCommandcmd=newSqlCommand("selectphotofromStudentwhereID=000020",connection);

SqlDataAdapterda=newSqlDataAdapter(cmd);

DataSetds=newDataSet();

da.Fill(ds,"Student");

intc=ds.Tables["Student"].Rows.Count;



if(c>0)

{//BLOBisreadintoBytearray,thenusedtoconstructMemoryStream,

//thenpassedtoPictureBox.

Byte[]byteBLOBData=newByte[0];

byteBLOBData=(Byte[])(ds.Tables["Student"].Rows[c-1]["photo"]);

MemoryStreamstmBLOBData=newMemoryStream(byteBLOBData);

pictureBox1.Image=Image.FromStream(stmBLOBData);

}

connection.Close();

}

}

}









privatevoidbtnDiaplay_Click(objectsender,EventArgse)

{

//if(txtFileName.Text!="")

{

using(SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString))

{

FileStreamfile=null;

BinaryWriterbw=null;

intbuffsize=100;

byte[]outbyte=newbyte[buffsize];

longretval;

longstartindex;

saveImageName="G:\\1.jpg";

conn.Open();

using(SqlCommandcomm=newSqlCommand())

{

comm.Connection=conn;

comm.CommandText="SelectPhotoFromStudentWhereID=1";

SqlDataReaderread=comm.ExecuteReader(CommandBehavior.SequentialAccess);

while(read.Read())

{

file=newFileStream(saveImageName,FileMode.OpenOrCreate,FileAccess.Write);

bw=newBinaryWriter(file);

startindex=0;

retval=read.GetBytes(0,startindex,outbyte,0,buffsize);

while(retval==buffsize)

{

bw.Write(outbyte);

bw.Flush();

startindex+=buffsize;

retval=read.GetBytes(0,startindex,outbyte,0,buffsize);

}

bw.Write(outbyte,0,(int)retval);

bw.Flush();

bw.Close();

file.Close();

}

curImage=Image.FromFile(saveImageName);

pictureBox1.Image=curImage;

pictureBox1.Invalidate();

//Imageimage=this.pictureBox1.Image;//获取图片资源的引用

//this.pictureBox1.Image=null;

//image.Dispose();//释放该图片资源

//File.Delete("G:\\1.jpg");//ok,因为此时该图片资源已经被释放



//System.IO.FileInfofile1=newSystem.IO.FileInfo("G:\\1.jpg");

//if(file1.Exists)

//{

//file1.Delete();

//}

}

}

}

//else

//{

//MessageBox.Show("请设置路径");

//}

}
献花(0)
+1
(本文系巭奥特曼首藏)