IT

(一)

写入:

Stream   ms;   
byte   []picbyte;    
openFileDialog1.Filter   =   SystemConst.IMAGE_FILE_FILTER;    
if   (this.openFileDialog1.ShowDialog()   ==   DialogResult.OK)  
{   
if   ((ms   =   openFileDialog1.OpenFile())   !=   null)   
{  
picbyte   =   new   byte[ms.Length];   
ms.Position   =   0;  
ms.Read(picbyte,   0,   Convert.ToInt32(ms.Length));   
ms.Dispose();                                      
}  
}  
得到picbyte[]   后就可以插入数据库了。对应数据库的Image类型  
读取:   
if   (dt.Rows[0]["Photo"]   !=   DBNull.Value)  
{  
byte[]   mybyte   =   (byte[])dt.Rows[0]["Photo"];   
MemoryStream   ms   =   new   MemoryStream(mybyte   );   
this.pb_Employee.Image   =   Image.FromStream(ms,   true);  
}

 

(二)

存:   
SqlConnection   con   =   new   SqlConnection("Server=Darkover;uid=<username>;pwd=<strong   password>;database=northwind");  
SqlDataAdapter   da   =   new   SqlDataAdapter("Select   *   From   MyImages",   con);  
SqlCommandBuilder   MyCB   =   new   SqlCommandBuilder(da);  
DataSet   ds   =   new   DataSet("MyImages");  
da.MissingSchemaAction   =   MissingSchemaAction.AddWithKey;  
FileStream   fs   =   new   FileStream(@"C:\winnt\Gone   Fishing.BMP",   FileMode.OpenOrCreate,   FileAccess.Read);  
byte[]   MyData=   new   byte[fs.Length];  
fs.Read(MyData,   0,   System.Convert.ToInt32(fs.Length));  
fs.Close();  
da.Fill(ds,"MyImages");  
DataRow   myRow;  
myRow=ds.Tables["MyImages"].NewRow();  
myRow["Description"]   =   "This   would   be   description   text";  
myRow["imgField"]   =   MyData;  
ds.Tables["MyImages"].Rows.Add(myRow);  
da.Update(ds,   "MyImages");  
con.Close();   
取:   
SqlConnection   con   =   new   SqlConnection("Server=Darkover;uid=<username>;pwd=<strong   password>;database=northwind");  
SqlDataAdapter   da   =   new   SqlDataAdapter("Select   *   From   MyImages",   con);  
SqlCommandBuilder   MyCB   =   new   SqlCommandBuilder(da);  
DataSet   ds   =   new   DataSet("MyImages");  
byte[]   MyData=   new   byte[0];  
da.Fill(ds,   "MyImages");  
DataRow   myRow;  
myRow=ds.Tables["MyImages"].Rows[0];  
MyData   =     (byte[])myRow["imgField"];  
int   ArraySize   =   new   int();  
ArraySize   =   MyData.GetUpperBound(0);    
FileStream   fs   =   new   FileStream(@"C:\winnt\Gone   Fishing2.BMP",   FileMode.OpenOrCreate,   FileAccess.Write);  
fs.Write(MyData,   0,ArraySize);  
fs.Close();  

 

(三)

添加图片   
OleDbConnection   mycnn=new   OleDbConnection("provider=Microsoft.jet.oledb.4.0;data   source=f:\\dazhu.mdb");  
mycnn.Open();  
OleDbCommand   mycmd=new   OleDbCommand("update   info   set   picture=@a",mycnn   );  
FileStream   mystream=new   FileStream("f:\\1.jpg",FileMode.Open,FileAccess.Read);  
long   len=mystream.Length;  
mycmd.Parameters.Add("@a",OleDbType.Binary,(int)len,"picture");   
mycmd.Parameters["@a"].Direction=System.Data.ParameterDirection.Input;  
byte   []box=new   byte[len];    
mystream.Read(box,0,(int)len);  
mycmd.Parameters["@a"].Value=box;  
//更新  
mycmd.ExecuteNonQuery();  
MessageBox.Show("ok");  
mystream.Close();   
mycnn.Close(); 

 读取图片  
OleDbConnection   mycnn=new   OleDbConnection("provider=Microsoft.jet.oledb.4.0;data   source=f:\\dazhu.mdb");  
mycnn.Open();  
MessageBox.Show("ok.mycnn.open");  
OleDbCommand   mycmd=new   OleDbCommand("select   *   from   info",mycnn   );  
OleDbDataReader   myrd=mycmd.ExecuteReader();  
if(myrd.Read())  
{  
byte   []box=(byte   [])myrd["picture"];  
Stream   stream1=new   MemoryStream(box);   
this.pictureBox2.Image=System.Drawing.Image.FromStream(stream1);   
stream1.Close();  
}  
mycnn.Close();  

 

(四)

//选择图片,并显示  
  private   void   menuItem4_Click(object   sender,   System.EventArgs   e)  
  {  
  dlgAddPic.Filter="JPEG   图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg|Windows   位图文件(*.bmp)|*.bmp|GIF   计算机服务(*.gif)|*.gif|所有文件(*.*)|*.*";  
  if   (dlgAddPic.ShowDialog()==DialogResult.OK)  
  {  
  if   (picSmall.Image   !=   null)  
  {  
  picSmall.Image.Dispose();  
  picBig.Image.Dispose();  
  picSmall.Image=null;  
  picBig.Image=null;  
  }  
  picSmall.Image=Image.FromFile(dlgAddPic.FileName);  
  picBig.Image=Image.FromFile(dlgAddPic.FileName);  
  txtPicName.Text=dlgAddPic.FileName.Substring(dlgAddPic.FileName.LastIndexOf(@"\",dlgAddPic.FileName.Length,dlgAddPic.FileName.Length)+1);  
  }  
  }  
  //保存,添加到数据库  
  private   void   menuItem15_Click(object   sender,   System.EventArgs   e)  
  {  
  if   (cboPicType.Text=="")    
  {  
  MessageBox.Show("请确定图片类别!");  
  return;  
  }   
  MemoryStream   mStream   =new   MemoryStream();  
  try  
  {  
  if   (txtPicName.Text.Length<5)   return;  
  switch(txtPicName.Text.Substring(txtPicName.Text.Length-3).ToUpper())  
  {  
  case   "JPG":  
  case   "PEG":  
  picBig.Image.Save(mStream,ImageFormat.Jpeg);  
  picBig.Image.Save()  
  break;  
  case   "GIF":  
  picBig.Image.Save(mStream,ImageFormat.Gif);  
  break;  
  case   "BMP":  
  picBig.Image.Save(mStream,ImageFormat.Bmp);  
  break;  
  }  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  byte[]   picData=new   byte[mStream.Length];  
  mStream.Position=0;  
  mStream.Read(picData,0,Convert.ToInt32(mStream.Length));  
  mStream.Close();  
  mStream=null;  
  string   strInsertSQL="insert   into   "+cboPicType.Text+"   (NAME   ,   PIC)   values('"+txtPicName.Text+picData.Length.ToString()+"'   ,@pic)";  
  cmd.CommandText=strInsertSQL;  
  OleDbParameter   paramPic=new   OleDbParameter("@pic",OleDbType.VarBinary);  
  paramPic.SourceColumn="PIC";  
  paramPic.Value=picData;

  cmd.Parameters.Clear();  
  cmd.Parameters.Add(paramPic);  
  try  
  {  
  cmd.ExecuteNonQuery();   
  MessageBox.Show("添加成功!");  
  string   strSQL="select   top   1   ID   from   "+cboPicType.Text+"   order   by   ID   desc";  
  cmd.CommandText=strSQL;  
  cboPicID.Items.Add(cmd.ExecuteScalar().ToString());  
  }  
  catch(Exception   ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  

 

(五)

//照片輸入數據庫  
  try  
  {  
  if(path!="")  
  {  
  FileInfo     fi=new   FileInfo(path);  
  int   imgdatalen=Convert.ToInt32(fi.Length);  
  byte[]   imadata=new   byte[imgdatalen];  
  Stream   imgdatastream=fi.OpenRead();    
  imgdatastream.Read(imadata,0,imgdatalen);  
  emp.ZP=imadata;    
  }  
  }  
  catch(Exception   ee)  
  {  
  MessageBox.Show(ee.Message.ToString());  
  }  
點擊picturebox瀏覽圖片:  
  private   void   pictureBox1_Click(object   sender,   System.EventArgs   e)  
  {  
  try  
  {  
  Bitmap   MyImage;  
  OpenFileDialog   open=new   OpenFileDialog();  
  open.ShowDialog();  
  path=open.FileName;  
  if(path=="")  
  {  
  return;  
  }  
  pictureBox1.SizeMode   =   PictureBoxSizeMode.StretchImage   ;  
  MyImage   =   new   Bitmap(path);  
  pictureBox1.ClientSize   =   new   Size(pictureBox1.Width,pictureBox1.Height);  
  pictureBox1.Image   =   (Image)   MyImage   ;  
  }  
  catch(Exception   ee)  
  {  
  MessageBox.Show(ee.Message.ToString());  
  }  

posted on 2010-06-28 12:34  liufei  阅读(817)  评论(0编辑  收藏  举报