2011-03-01 79 views
0

我沒有將圖像製作成二進制形式,但現在想從數據庫中獲取圖像數據。 所以你可以給出這樣的想法。 我已經做了類似下面插入圖片:如何從C#中的數據庫中獲取圖像?

FileDialog dialog = new OpenFileDialog(); 
dialog.InitialDirectory = @":D\"; 
dialog.Filter = "(*.jpg;*.gif;*.jpeg;*.bmp)| *.jpg; *.gif; *.jpeg; *.bmp"; 
if (dialog.ShowDialog() == DialogResult.OK) 
{ 
    imagename = dialog.FileName; 
    pictureBox1.Image = Image.FromFile(imagename); 
} 
dialog = null; 

那麼它也存儲在數據庫中,但現在我必須以檢索在明年形成圖像我該怎麼辦?

+0

圖像幾乎總是二進制形式。如果您要將圖像存儲在數據庫中,則幾乎可以確定將其存儲爲二進制文件。你的問題不是很清楚。你可以更具體一些,也許給一些代碼示例? – 2011-03-01 06:18:43

+0

圖像如何存儲在數據庫中(代碼示例更可取)? – 2011-03-01 06:18:57

+1

「我沒有把圖像變成二進制形式」 - 所以; **它是如何存儲在數據庫中的?什麼字段類型?以及您使用的是什麼數據訪問工具? – 2011-03-01 06:19:11

回答

1
int O_id =Convert.ToInt32(textBox2.Text); 

      SqlConnection cn = new SqlConnection(strCn); 
      SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData, O_id) VALUES (@BLOBData,'"+O_id+"')", cn); 
      String strBLOBFilePath = textBox1.Text;//Modify this path as needed. 


      //Read jpg into file stream, and from there into Byte array. 
      FileStream fsBLOBFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read); 
      Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; 
      fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); 
      fsBLOBFile.Close(); 

      //Create parameter for insert command and add to SqlCommand object. 
      SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 
         0, 0, null, DataRowVersion.Current, bytBLOBData); 
      cmd.Parameters.Add(prm); 

      //Open connection, execute query, and close connection. 
      cn.Open(); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Picture has been uploaded"); 
      cn.Close(); 
+0

如果您打算用更多信息更新您的問題,您可以選擇這樣做。這篇文章是否回答你的問題? – Mizipzor 2012-03-20 09:24:38

+0

是的,它適合我。 – devilsmind 2012-03-20 09:27:37

0

OK,假設你存儲在圖像數據庫中的BLOB字段,下面的代碼檢索BLOB字段的數據,創建一個內存流和內存流加載Bitmap

using (SqlConnection conn = ...) 
{ 
    conn.Open(); 

    using (SqlCommand cmd = new SqlCommand("SELECT Picture FROM <tableName> WHERE ...", conn) 
    using (SqlDataReader reader = cmd.ExecuteReader()) 
    { 
     if (reader.Read()) 
     { 
      byte[] picData= reader["Picture"] as byte[] ?? null; 

      if (picData!= null) 
      { 
       using (MemoryStream ms = new MemoryStream(picData)) 
       { 
        // Load the image from the memory stream. How you do it depends 
        // on whether you're using Windows Forms or WPF. 
        // For Windows Forms you could write: 
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms); 
       } 
      } 
     } 
    } 
} 
2
protected void butSubmit_Click(object sender, EventArgs e) 
{ 
SqlConnection connection = null; 
try 
{ 
Byte[] imgByte = null; 
    if (FileUpload1.HasFile && FileUpload1.PostedFile != null) 
{ 
HttpPostedFile File = FileUpload1.PostedFile; 
imgByte = new Byte[File.ContentLength]; 
File.InputStream.Read(imgByte, 0, File.ContentLength); 
} 
connection = new SqlConnection(ConfigurationManager.ConnectionStrings   
"ConnectionString"].ConnectionString.ToString()); 

connection.Open(); 
string sql = "INSERT INTO Table1(title,image) VALUES(@theTitle, @theImage) SELECT  
@@IDENTITY"; 
SqlCommand cmd = new SqlCommand(sql, connection); 
cmd.Parameters.AddWithValue("@theTitle", txtTitle.Text); 
cmd.Parameters.AddWithValue("@theImage", imgByte); 
int id = Convert.ToInt32(cmd.ExecuteScalar()); 
lblStatus.Text = String.Format("ID is {0}", id); 

Image1.ImageUrl = "~/DisplayImg.ashx?id=" + id; 
} 
{ 
lblStatus.Text = "There was an error"; 
} 
finally 
{ 
connection.Close(); 
} 

}

0
using System.Data.SqlClient; 


using System.Drawing; 

using System.Data; 

using System.IO; 

using System.Drawing.Imaging; 


public void Save_Image(Object sender, EventArgs e) 
{ 

    // Create a byte[] from the input file 

    int len = Upload.PostedFile.ContentLength; 
    byte[] pic = new byte[len]; 
    Upload.PostedFile.InputStream.Read (pic, 0, len); 

    // Insert the image into the database 

    SqlConnection connection = new 
SqlConnection (@"server=abc\.SQLEXPRESS;database=Storage;uid=sa;pwd=sa"); 

    try 
    { 
     connection.Open(); 
     SqlCommand cmd = new SqlCommand ("insert into Image " 
      + "(Picture) values (@pic)", connection); 

     cmd.Parameters.Add ("@pic", pic); 
     cmd.ExecuteNonQuery(); 

    } 
    finally 
    { 
     connection.Close(); 
    } 
} 

我們只存儲字節的圖像到表名稱 「圖片」 其中只包含一列。 將字節圖像存儲到數據庫中消耗大量數據庫大小 用於大圖像存儲和從數據庫檢索特定圖像 使用搜索需要較長的時間進行處理。這會導致 性能低下和存儲問題。

+1

我想下面的一個不是代碼,所以修改它.. – devilsmind 2012-03-21 05:17:02