程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#存取數據庫中的圖像

C#存取數據庫中的圖像

編輯:關於C#

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

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中的內容。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved