2011-09-12 47 views
10

我的數據庫中有圖像的二進制數據,我想在ASP.NET的圖像控件中顯示它。怎麼樣?如果不可能,請找到另一種方法將其保存在數據庫中,並將其顯示在圖像控件中。在ASP.NET中將二進制數據轉換爲圖像控件

+2

看看這個問題的答案類似的問題: http://stackoverflow.com/questions/6987433/display-image-from-database-in-asp-net-with-c – IrishChieftain

+0

或這一個:http://stackoverflow.com/q/612342/76051 – Carsten

回答

29

創建一個普通的HTML img元素,像這樣:

<img runat="server" id="image" /> 

而且在code behind做到這一點:

image.src = "data:image/png;base64," + Convert.ToBase64String(imageBytes); 

凡imageBytes是byte[]

你完成了。圖像將被顯示。

+0

這是目前爲止顯示二進制圖像最簡單的方法。但我發現這種方法只適用於JPG圖像。我嘗試了這與PNG圖像,但它沒有奏效。 – Arbaaz

+0

只要瀏覽器支持,該方法就可以用於jpg和png。這可能是因爲你的瀏覽器不支持它或者它們的實現有問題。 – Icarus

+0

你能解釋一下'data:image/png; base64'嗎? – Arbaaz

1

在一個通用處理器(ashx的):

public class ImageHandler : IHttpHandler 
     { 

      public void ProcessRequest(HttpContext context) 
      { 

        if(!string.IsNullOrEmpty(context.Request.QueryString["ImageId"])){ 
        try 
        { 
         string ImageId = context.Request.QueryString["ImageId"].ToString(); 
         ImageDataModel idm = new ImageDataModel(); 
         byte[] ImageData = idm.getImageData(ImageId); 

         context.Response.ContentType = "image/JPEG"; 
         context.Response.OutputStream.Write(ImageData, 0, ImageData.Length); 


        } 
4

最有可能的圖像被存儲在數據庫中的字節數組。如果是這樣,那麼你可以使用這個:

public static System.Drawing.Image ByteArrayToImage(byte[] bArray) 
{ 
    if (bArray == null) 
     return null; 

    System.Drawing.Image newImage; 

    try 
    { 
     using (MemoryStream ms = new MemoryStream(bArray, 0, bArray.Length)) 
     { 
      ms.Write(bArray, 0, bArray.Length); 
      newImage = System.Drawing.Image.FromStream(ms, true); 
     } 
    } 
    catch (Exception ex) 
    { 
     newImage = null; 

     //Log an error here 
    } 

    return newImage; 
} 
1
public Byte[] Ret_image(Int32 id) 
{ 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandText = "select * from tbimage where [email protected]"; 
    cmd.Connection = con; 
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = id; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    dr.Read(); 
    Byte[] ar = (Byte[])(dr[1]); 
    dr.Close(); 
    cmd.Dispose(); 
    return ar; 
} 
1
protected void Button2_Click(object sender, EventArgs e) 
{ 
    Byte[] ar = Ret_image(Convert.ToInt32(TextBox2.Text)); 
    String st = Server.MapPath("abc.jpg"); 
    FileStream fs = new FileStream(st, FileMode.Create, FileAccess.Write); 
    fs.Write(ar, 0, ar.Length); 
    fs.Close(); 
    Image1.ImageUrl = "abc.jpg";   
} 

使用此事件點擊按鈕來獲取圖像,並在這裏調用Ret_Image方法。

0
SqlConnection con = new SqlConnection(); 
string _path; 
Using SYstem.IO; 
Using System.Data.SQLClient; 

//convert Image to binary and save in DB 

private void button1_Click(object sender, EventArgs e) 
{ 
    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     _path = openFileDialog1.FileName; 
     InsertInSQL(_path); 
    } 
} 

private void InsertInSQL(string _path) 
{ 
    con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS; 
    string strQ = "insert into dbo.PicTBL(Pic)values(@p)"; 
    SqlCommand command = new SqlCommand(strQ,con); 
    command.Parameters.AddWithValue("@p",ImageToBinary(_path)); 
    con.Open(); 
    command.ExecuteNonQuery(); 
    con.Close(); 
}  

public static byte[] ImageToBinary(string _path) 
{ 
    FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read); 
    byte[] b = new byte[fS.Length]; 
    fS.Read(b, 0, (int)fS.Length); 
    fS.Close(); 
    return b; 
} 

//Convert Binary to imge and save in a folder 
private void button1_Click_1(object sender, EventArgs e) 
{ 
    DataTable dt = Rimage(); 
    foreach (DataRow row in dt.Rows) 
    { 
     byte[] b = (byte[])row["Pic"]; 
     Image img = BinaryToImage(b); 
     img.Save("D:\\NewFolder\\" + row["ID"].ToString() + ".jpg"); 
    } 
} 

private Image BinaryToImage(byte[] b) 
{ 
    if (b == null) 
     return null; 

    MemoryStream memStream = new MemoryStream(); 
    memStream.Write(b, 0, b.Length); 

    return Image.FromStream(memStream); 
} 

private DataTable Rimage() 
{ 
    con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS; 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandText = "select * from dbo.PicTBL"; 
    cmd.Connection = con; 
    SqlDataAdapter adp = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 
    con.Open(); 
    adp.Fill(dt); 

    return dt; 
} 
+0

這就是我所做的一切!好好享受 –

相關問題