2013-04-16 49 views
1

我想你幫我解決一些誤解錯誤。我實現了一個從活動目錄獲取所有內容的代碼。問題是我不知道我是否正確實施它,特別是圖像數據。如何從數據庫讀取圖像數據並進行打印?

public System.Drawing.Image GetUserPicture(string userName) { 
    try { 
    var directoryEntry = new DirectoryEntry("LDAP://DC=darcairo,DC=com"); 
    var directorySearcher = new DirectorySearcher(directoryEntry); 
    directorySearcher.Filter = ("(SamAccountName=" + userName + ")"); 
    var user = directorySearcher.FindOne(); 
    var bytes = user.Properties["thumbnailPhoto"][0] as byte[]; 
    using(var ms = new MemoryStream(bytes)) { 
     return Bitmap.FromStream(ms); 
    } 
    } catch (Exception e) { 
    return null; 
    } 
} 

然後我的目的是將這些數據像用戶名等插入到數據庫中。

protected void Button1_Click(object sender, EventArgs e) { 
    try { 
    char[] delimiters = new char[] { 
     '\\' 
    }; 
    string domainuser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split(delimiters)[1]; 
    profile dir = new profile(); 
    string fname = dir.get_activeDiretory_DisplayName(domainuser); 
    string mail = dir.get_activeDiretory_Email(domainuser); 
    string department = dir.get_activeDiretory_Department(domainuser); 
    string photo = Convert.ToString(dir.GetUserPicture(domainuser)); 
    string sql = string.Empty; 
    string atc = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
    sql = "INSERT INTO Profile (uid, uname, ufname, uemail, udepartment, uphoto) "; 
    sql += "VALUES(@uid, @uname, @ufname, @uemail, @udepartment, @uphoto); set identity_insert Profile off"; 
    using(SqlConnection c = new SqlConnection(atc)) { 
     using(SqlCommand cmd = new SqlCommand(sql, c)) { 
     //cmd.Parameters.AddWithValue("OrderID", 1); 
     cmd.Parameters.AddWithValue("uid", 1); 
     cmd.Parameters.AddWithValue("uname", domainuser); 
     cmd.Parameters.AddWithValue("ufname", fname); 
     cmd.Parameters.AddWithValue("uemail", mail); 
     cmd.Parameters.AddWithValue("udepartment", department); 
     cmd.Parameters.AddWithValue("uphoto", photo); 
     c.Open(); 
     cmd.ExecuteNonQuery(); 
     c.Close(); 
     Label2.Text = "Record Inserted"; 
     } 
    } 
    Label2.Text = "inserted"; 
    } catch (Exception s) { 
    Label2.Text = "not inserted " + "Error= " + s.Message; 
    } 
    string str = ""; 
    foreach(GridViewRow g in GridView1.Rows) { 
    str = str + g.Cells[0].Text + g.Cells[1].Text + g.Cells[2].Text + g.Cells[3].Text + g.Cells[4].Text + Convert.ToByte(g.Cells[5]); 
    } 
    GridView1.Enabled = true; 
} 

但是,sql中的圖像數據是「Varbinary(max)」。

這裏是動態網格視圖:

<asp:GridView 
    ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="False" 
    BackColor="White" 
    BorderColor="#336666" 
    BorderStyle="Double" 
    BorderWidth="3px" 
    CellPadding="4" 
    DataKeyNames="UID" 
    DataSourceID="SqlDataSourceProf" 
    GridLines="Horizontal" 
    Width="433px" 
    Height="472px"> 
    <Columns> 
    <asp:BoundField DataField="UID" HeaderText="UID" InsertVisible="False" ReadOnly="True" SortExpression="UID" /> 
    <asp:BoundField DataField="UName" HeaderText="UName" SortExpression="UName" /> 
    <asp:BoundField DataField="UFName" HeaderText="UFName" SortExpression="UFName" /> 
    <asp:BoundField DataField="UEmail" HeaderText="UEmail" SortExpression="UEmail" /> 
    <asp:BoundField DataField="UDepartment" HeaderText="UDepartment" SortExpression="UDepartment" /> 
    <asp:TemplateField> 
     <ItemTemplate> 
     <asp:Image ID="Image1" Height="100" Width="100" runat="server" ImageUrl='' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
    <FooterStyle BackColor="White" ForeColor="#333333" /> 
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="White" ForeColor="#333333" /> 
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /> 
    <SortedAscendingCellStyle BackColor="#F7F7F7" /> 
    <SortedAscendingHeaderStyle BackColor="#487575" /> 
    <SortedDescendingCellStyle BackColor="#E5E5E5" /> 
    <SortedDescendingHeaderStyle BackColor="#275353" /> 
</asp:GridView> 
<asp:SqlDataSource 
    ID="SqlDataSourceProf" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Profile]"> 
</asp:SqlDataSource> 

除了圖像的所有作品。你能幫忙或提供我的提示嗎? 謝謝。

+0

而不是插手filestreams爲什麼不把圖片保存到您的服務器,然後只存儲數據庫中的路徑? –

+0

將圖像轉換爲字節數據,然後將塊添加到命令, –

回答

0

寫入圖像數據庫

爲位圖對象

Bitmap mImage = new Bitmap(strPathForImage); 
0

您可以使用通用的處理程序這一點。

將用戶標識作爲查詢字符串參數傳遞給通用處理程序,將處理程序響應類型設置爲image/jpg並將圖像數據寫入響應。然後將該處理程序分配爲ImageUrl

例如

public class ghCompanyLogoImage : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     Stream objLogoStream = null; 

     objLogoStream = GetCompanyLogo(context); 

     if (objLogoStream != null && objLogoStream.Length > 0) 
     { 
      // Set up the response settings 
      context.Response.ContentType = "image/jpeg"; 
      context.Response.BufferOutput = false; 

      const int buffersize = 1024 * 16; 
      byte[] buffer = new byte[buffersize]; 
      int count = objLogoStream.Read(buffer, 0, buffersize); 
      while (count > 0) 
      { 
       context.Response.OutputStream.Write(buffer, 0, count); 
       count = objLogoStream.Read(buffer, 0, buffersize); 
      } 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

    private Stream GetCompanyLogo(HttpContext context) 
    { 
     try 
     { 
      // Fetch image data stored in DB, convert it to MemoryStream and return it. 
      // You can read querystring value here using "context.Request.QueryString[]" 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 
} 

而且,當時以HTML顯示圖像,圖像指定URL作爲,

ImageUrl ="~/ghCompanyLogoImage.ashx?ID=Val" 

我希望它能幫助。

相關問題