国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# 存取數據庫中的圖像

2019-11-18 16:36:57
字體:
來源:轉載
供稿:網友

一、數據庫中的圖像存取方法

1. 讀取image類型的數據

    讀取image類型數據的方法可分為以下幾步:

    1) 先使用無符號字節數組存放數據庫對應的數據集中表的image類型字段的值。例如:

      byte[] bytes= (byte[]) image類型字段值

    2) 使用MemoryStream類,該類創建支持存儲區為內存的流。即MemoryStream類創建的流以內存而不是磁盤或網絡連接作為支持存儲區。其構造函數為:

      public MemoryStream(byte[] buffer);

    3) 使用Bitmap類,該類封裝了GDI+位圖,此位圖由圖形圖像及其屬性的像素數據組成。Bitmap對象是用于處理由像素數據定義的圖像的對象。其構造函數為:

      public Bitmap(Stream stream);

    4) 在窗體中利用PictureBox控件對象顯示圖像。

2. 保存image類型的數據

    保存image類型數據的方法也分為以下幾步:

    1) 使用Stream類,首先從圖像文件中獲取流對象,再利用該類的Read方法從圖像文件中讀取二進制數據存入字節數組中。Read方法為:

       public abstract int Read([In, Out] byte[] buffer, int offset, int count);

    2) 將字節數組中的值存入數據庫對應的數據集中表的image字段。格式為:

         image類型字段= bytes;

    3) 更新數據庫,就可以完成保存圖像數據的功能。

二、  數據庫中的圖像存取示例

    下面通過一個例子說明如何存取SQL Server數據庫中的圖像。

   (1) 創建一個Windows應用程序,設計窗體界面如圖所示。


⑵ 添加名稱空間引用

  using System.Data;

  using System.Data.SqlClient;

  using System.IO;

⑶ 添加字段聲明

       PRivate string connString="server=localhost; integrated security=sspi; database=pubs";

       SqlConnection conn;

       SqlDataAdapter adapter;

       DataSet dataset;

⑷ 在構造函數中添加代碼

       string sqlstr="select * from pub_info";

       conn=new SqlConnection(connString);

       adapter=new SqlDataAdapter(sqlstr,conn);

       SqlCommandBuilder builder=new SqlCommandBuilder(adapter);

       adapter.UpdateCommand=builder.GetUpdateCommand();

       dataset=new DataSet();

       adapter.Fill(dataset,"pub_info");

       //將text1Box1的Text屬性綁定到dataset中的pub_info表的pr_info字段

       this.textBox1.DataBindings.Add(new Binding("Text",dataset,"pub_info.pr_info"));

       for(int i=0;i<dataset.Tables[0].Rows.Count;i++)

       {

              this.listBox1.Items.Add(dataset.Tables[0].Rows[i][0]);

       }

⑸ 添加調用的方法

       private void ShowImage()

       {

           byte[] bytes= (byte[])dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1];

              MemoryStream memStream=new MemoryStream(bytes);

              try

              {

                     Bitmap myImage = new Bitmap(memStream);

                     this.pictureBox1.Image= myImage;

              }

              catch

              {

                     this.pictureBox1.Image=null;

              }

       }

⑹ 添加“更換圖片”的Click事件代碼

private void buttonUpdateImage_Click(object sender, System.EventArgs e)

{

       OpenFileDialog openFileDialog1=new OpenFileDialog();

       openFileDialog1.ShowDialog();

       if (openFileDialog1.FileName.Trim()!="")

   {

              Stream myStream = openFileDialog1.OpenFile();

              int length=(int)myStream.Length;

              byte[] bytes=new byte[length];

              myStream.Read(bytes,0,length);

              myStream.Close();

           dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1] =bytes;

              ShowImage();

       }

}

⑺ 添加“移除圖片”的Click事件代碼

private void buttonMoveImage_Click(object sender, System.EventArgs e)

{

       byte[] bytes= System.Text.Encoding.Unicode.GetBytes("");

       dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1]=

        bytes;

       ShowImage();

}

⑻ 添加“保存更改”的Click事件代碼

private void buttonSave_Click(object sender, System.EventArgs e)

{

       adapter.Update(dataset,"pub_info");

       MessageBox.Show("保存成功");

}

⑼ 添加listBox1的SelectedIndexChanged事件代碼

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

     ShowImage();

       this.BindingContext[dataset,"pub_info"].Position

       =this.listBox1.SelectedIndex;

}

(10) 運行。  

  可以更換圖片,也可以直接修改textBox1中的內容。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 申扎县| 来凤县| 樟树市| 陵川县| 千阳县| 绿春县| 保定市| 朝阳县| 攀枝花市| 大埔县| 太保市| 平利县| 翼城县| 安溪县| 洛阳市| 永善县| 新平| 西充县| 抚顺县| 海林市| 洛扎县| 滦平县| 鄂托克前旗| 新密市| 古浪县| 吐鲁番市| 阜康市| 江西省| 禹州市| 绥宁县| 彰化市| 富源县| 宜良县| 云龙县| 天津市| 南平市| 双城市| 万全县| 株洲市| 台南市| 招远市|