2012-09-26 39 views
0

我有一個任務在c#中存儲和檢索.gif文件。我已經成功地將gif存儲在數據庫中。我將它轉換爲二進制文件並使用存儲過程存儲在數據庫中。創建.gif文件

這裏是我的代碼:

public int Insert(byte[] Byte,string Name,int userid) 
{ 
    try 
    { 
     SqlCommand storedProcCommand = new SqlCommand("Insert", con); 
     storedProcCommand.CommandType = CommandType.StoredProcedure; 
     storedProcCommand.Parameters.AddWithValue("@Bytes", banner2Byte); 
     storedProcCommand.Parameters.AddWithValue("@name", bannerName); 
     storedProcCommand.Parameters.AddWithValue("@userid", userid); 
     con.Open(); 
     storedProcCommand.ExecuteNonQuery(); 
     con.Close(); 
    } 
    catch (Exception ex) 
    { 
    } 
} 

如何從數據庫中檢索字節數組,並轉換爲.gif,創造我保存在首位的文件嗎?

public int Retrieve(int id) 
{ 
    try 
    { 
     SqlCommand storedProcCommand = new SqlCommand("Retrieve", con); 
     storedProcCommand.CommandType = CommandType.StoredProcedure; 
     storedProcCommand.Parameters.AddWithValue("@id", id); 

     con.Open(); 

     SqlDataReader reader = storedProcCommand.ExecuteReader(); 
     reader.Read(); 

     byte[] arrays = reader.getBytes();    

     reader.Close(); 
     con.Close(); 
    } 
    catch (Exception ex) 
    { 
    } 
} 

回答

2

認爲你已經正確保存GIF文件(用頭和所有),您可以使用此功能從字節數組回來的圖像。

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