简体   繁体   English

如何在ASP.NET中使用C#从数据库中检索二进制图像

[英]How to retrieve binary image from database using C# in ASP.NET

I need to retrieve a binary image from the database. 我需要从数据库中检索二进制图像。

My queries are as below. 我的疑问如下。

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);

I do not know how to retrieve blueBallImage which is a binary image. 我不知道如何检索作为二进制图像的blueBallImage。

After I have retrieve it successfully, I need to add a text onto the image using a dropdownlist which contain the text. 成功检索后,我需要使用包含文本的下拉列表在图像上添加文本。 The codes are as below. 代码如下。

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

For the time being, I do not know how to retrieve the image. 目前,我不知道如何检索图像。 Therefore, I hard coded it which I do not want. 因此,我硬编码了我不想要的东西。 I want to retrieve it from database. 我想从数据库中检索它。

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;

Here is a basic sample to load an image from database quickly and load into a html image source in ASP. 这是从数据库快速加载图像并加载到ASP中的html图像源的基本示例。 Please tell me if it works for you ;-) 请告诉我它是否适合你;-)

//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);
}

you need to made an ASP.NET handler (*.ASHX) that serves the bytes of the image 你需要创建一个ASP.NET处理程序(* .ASHX)来提供图像的字节

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

In image handler you need to code like this 在图像处理程序中,您需要像这样编码

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";
    }
}

please refer this for more info Image from Database 请参阅此以获取更多信息来自数据库的图像

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

    try
    {
        cm = new SqlCommand("select profile_pic from TblNewMemberRegistration where    user_name='sudhanshu@mailbox.com'", 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();


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从SQL检索解码的二进制图像,然后通过iTextsharp asp.net c#插入PDF - Retrieve decoded binary image from SQL and insert into PDF via iTextsharp asp.net c# 如何使用 ASP.NET 从 SQL 服务器数据库中检索和显示 GridView 中的值 - How to retrieve and display values in GridView from SQL Server database in ASP.NET using C# 如何从SQL Server数据库中检索图像字段civilimage1,civilimage2,以在带有vb.net或c#的ASP.NET中使用图像控件进行显示? - How to retrieve image fields civilimage1,civilimage2 from SQL Server database to show using image control in ASP.NET with vb.net or c#? ASP.NET C#从SQL Server数据库获取检索并显示图像 - ASP.NET C# Get retrieve and show image from SQL Server database 如何使用c#从asp.net中的数据库绑定图像? - how to bind image from database in asp.net using c#? 通过asp.net使用linQ从数据库检索图像 - retrieve an image from database using linQ by asp.net 在asp.net中从数据库中检索图像 - Retrieve image from database in asp.net 使用asp.net从SQL数据库检索二进制数据 - retrieve Binary Data from SQL Database with asp.net How to retrieve a Json object from SQL procedure in ASP.NET using C#? - How to retrieve a Json object from SQL procedure in ASP.NET using C#? 使用asp.net gridview和c#从php myadmin数据库显示图像 - displaying image from php myadmin database using asp.net gridview and c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM