2011-12-09 30 views
0

我有一個非常基本的形式,只有一個圖片框,一個文本框和一個按鈕。我在Oracle中創建了一個表來存儲一個blob,我想在c#中將該圖像加載到一個圖片框中,一旦點擊該按鈕。這是我迄今爲止所寫的內容。使用Oracle Blob在C#中加載Picturebox

我的圖片框名稱=「picBoxXray」。我的文本框名稱=「txtXrayId」。我的按鈕名稱=「btnGetXrayID」。我的表名是「XRay」。

private void btnGetXrayID_Click(object sender, EventArgs e) 
    { 


     if (txtXrayId.Text == "") 
     { 
      MessageBox.Show("XrayID be entered"); 
     } 

     if (picBoxXray.Image != null)   
     { 
      picBoxXray.Image.Dispose();   
     } 

     string connectionString = GetConnectionString(); 
     using (OracleConnection connection = new OracleConnection()) 
     { 
      connection.ConnectionString = connectionString; 
      connection.Open(); 
      Console.WriteLine("State: {0}", connection.State); 
      Console.WriteLine("ConnectionString: {0}", 
           connection.ConnectionString); 

      OracleCommand command = connection.CreateCommand(); 

      string sql = "SELECT * FROM XRay WHERE XrayID =" + txtXrayId.Text; 

      OracleCommand cmd = new OracleCommand(sql, connection); 
      OracleDataReader dr = cmd.ExecuteReader(); 
      cmd.CommandType = CommandType.Text; 
      dr = cmd.ExecuteReader(); 

      while (dr.Read()) 
      { 
       // Obtain the image 
       //Need code 

      } 

      command.CommandText = sql; 
      command.ExecuteNonQuery(); 
      connection.Close(); 

    } 

任何幫助將大大aprreciated, 感謝

回答

0

你必須從流創建的圖像,例如:

public Image ByteArrayToImage(byte[] byteArrayIn) 
{ 
    MemoryStream ms = new MemoryStream(byteArrayIn); 
    Image returnImage = Image.FromStream(ms); 
    return returnImage; 
} 

然後將圖像aassign到圖片框。 byteArray in是從db讀取的blob。當然圖像必須以已知的格式存儲(例如png/jpeg)