2013-09-25 39 views
3

我需要从数据库中检索二进制映像。如何在ASP.NET中使用C#从数据库中检索二进制映像

我的查询如下。

SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyGames;Integrated Security=True"); 
SqlCommand cmd = new SqlCommand("Select blueBallImage from CorrespondingBall WHERE objective = Default Ball", con); 

我不知道如何检索blueBallImage这是一个二进制图像。

成功检索后,我需要使用包含文本的下拉列表将文本添加到图像上。代码如下。

Bitmap bmp = new Bitmap(@"C:\Users\apr13mpsip\Documents\Visual Studio 2012\WebSites\CorrespondingBallWebSite\Images\blueBallDefault.png"); 

目前,我不知道如何检索图像。因此,我硬编码它,我不想要的。我想从数据库中检索它。

Graphics gra = Graphics.FromImage(bmp); 

gra.DrawString(ddlCharacter.Text, new Font("Verdana", 18), Brushes.Black, new PointF(4, 6)); 

MemoryStream ms1 = new MemoryStream(); 
bmp.Save(ms1, ImageFormat.Png); 
var base64Data = Convert.ToBase64String(ms1.ToArray()); 
imgImage.ImageUrl = "data:image/png;base64," + base64Data; 

回答

6

这是一个基本的样例,用于从数据库中加载图像qui ckly并加载到ASP中的HTML图像源。请告诉我,如果你的作品;-)

//Get byte array from image file in the database with basic query 
SqlDataAdapter myAdapter1 = new SqlDataAdapter("Select [logo] FROM [dbo].[tblCompanyInfo]", GlobalUser.currentConnectionString); 
DataTable dt = new DataTable(); 
myAdapter1.Fill(dt); 

foreach (DataRow row in dt.Rows) 
{ 
    // Get the byte array from image file 
    byte[] imgBytes = (byte[]) row["logo"]; 

    // If you want convert to a bitmap file 
    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap)); 
    Bitmap MyBitmap = (Bitmap)tc.ConvertFrom(imgBytes); 

    string imgString = Convert.ToBase64String(imgBytes); 
    //Set the source with data:image/bmp 
    imgLogoCompany.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString); 
} 
+0

Thank you. However, I do not want to display the image it out yet. How can you set the image to Bitmap? –

+0

with this -> TypeConverter和之后 - >位图MyBitmap =(位图)tc.ConvertFrom(imgBytes);我在答案中添加了一个示例。你可以看到:-)。告诉我后,如果它的工作或没有 –

+0

是的,它的工作!非常感谢你! –

0

你需要做的ASP.NET处理程序(* .ashx的),用作图像的

<img src="ImageHandler.ashx?id=<%=id%>" /> 

在图像处理程序,你需要像这样的代码

public class ImageHandler : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     // Load the image (see previous code sample) 
     byte[] data = Convert.FromBase64String(encodedString); 

     // Display the image 
     context.Response.OutputStream.Write(data, 0, data.Length); 
     context.Response.ContentType = "image/JPEG"; 
    } 
} 
字节

请参考这里以获取更多信息Image from Database

+0

我可以知道你是什么意思看到前面的代码示例是什么意思? –

+0

他意味着加载的图像^^ – Derek

+0

我试图插入处理程序

0
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     Connect(); 
    } 

    try 
    { 
     cm = new SqlCommand("select profile_pic from TblNewMemberRegistration where user_name='[email protected]'", cn); 
     byte[] b = (byte[])cm.ExecuteScalar(); 
     stream.Write(b, 0, b.Length); 
     Bitmap bm = new Bitmap(stream); 
     Response.ContentType = "image/gif"; 
     bm.Save(Response.OutputStream, ImageFormat.Gif); 
    } 
    catch(Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
    finally 
    { 
     cn.Close(); 
     stream.Close(); 
    } 
} 
protected void Connect() 
{ 
    cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString); 
    cn.Open(); 


} 
+0

我已经写了这个代码来获取存储在数据库中的图像....这段代码正在工作,但是在我的sql-server中创建了一个特定的命名数据库,但它不能与另一个...当我与其他数据库运行此代码,然后它会引发一个异常参数无效..任何一个PLZ任何一个正确的,如果可能的话...在我的数据库图像存储在“图像”数据类型.. –

相关问题