打印本文 打印本文 关闭窗口 关闭窗口
利用C#在SQL Server2000存取图像 For Window
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3578  更新时间:2007/11/14 11:06:26  文章录入:mintao  责任编辑:mintao
   //
   this.pictureBox1.ContextMenu = this.contextMenu1;
   this.pictureBox1.Location = new System.Drawing.Point(0, 0);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(264, 192);
   this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   //
   // menuItem2
   //
   this.menuItem2.Index = 1;
   this.menuItem2.Text = "缩小";
   //this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
   //
   // menuItem3
   //
   this.menuItem3.Index = 2;
   this.menuItem3.Text = "放大";
   //this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(648, 421);
   this.Controls.Add(this.panel2);
   this.Controls.Add(this.panel1);
   this.Controls.Add(this.splitter1);
   this.Controls.Add(this.listBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.panel1.ResumeLayout(false);
   this.panel2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }


  private void Form1_Load(object sender, System.EventArgs e)
  {
   string connString="";
   connString = @"Persist Security Info=False;User ID=sa;Initial Catalog=work;Data Source=(local)";
   this.sqlConnection1.ConnectionString = connString;
   this.FillListBox();
   if (this.listBox1.Items.Count > 0) this.listBox1.SetSelected(0,true);
  }

  private void FillListBox()
  {
   this.listBox1.SelectedIndexChanged -= new System.EventHandler(this.listBox1_SelectedIndexChanged);
   string comm = "select id,name from table1";
   System.Data.DataTable dt = new DataTable("imgTable");
   this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
   this.sqlCommand1.CommandText = comm;
   this.sqlCommand1.Connection = this.sqlConnection1 ;
   System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(this.sqlCommand1);
   this.sqlConnection1.Open();
   da.Fill(dt);
   this.sqlConnection1.Close();
   this.listBox1.DataSource = dt;
   this.listBox1.DisplayMember = "name";
   this.listBox1.ValueMember = "id";
   this.listBox1.Refresh();
   this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  }

  //将SQL server2000中保存的图像显示在Picture中
  private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   byte[] buffByte = null;
   string comm = @"select img from table1 where id = " + this.listBox1.SelectedValue ;
   this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
   this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
   this.sqlCommand1.CommandText = comm;
   this.sqlCommand1.Connection = this.sqlConnection1 ;
   this.sqlConnection1.Open();
   System.Data.SqlClient.SqlDataReader rd = this.sqlCommand1.ExecuteReader();
   while (rd.Read())
   {
    buffByte = ((byte[])rd[0]);
   }
   rd.Close();
   this.sqlConnection1.Close();
   //将图像的字节数组放入内存流
   System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
   //通过流对象建立Bitmap
   System.Drawing.Bitmap bmp = new Bitmap(ms);
   this.pictureBox1.Image = bmp;
  }
  
  //将图像保存到SQL server2000的Image字段中
  private void button2_Click_1(object sender, System.EventArgs e)
  {
   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 = buffByte;
    this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
    this.sqlCommand1.Parameters[1].Value =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();
   }
  }

//  private void menuItem1_Click(object sender, System.EventArgs e)
//  {
//   this.saveFileDialog1.FileName = ((System.Data.DataRowView)this.listBox1.SelectedItem)[1].ToString();
//   this.saveFileDialog1.Filter = "JPG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|位图(*.bmp)|*.bmp";
//   if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
/

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口