Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:40+00:00 2026-05-27T02:13:40+00:00

Possible Duplicate: What's the best way to display an image from a sql server

  • 0

Possible Duplicate:
What's the best way to display an image from a sql server database in asp.net?
bytearray to image asp.net

How can you display an image from Sql Server 2008 in asp.net using c#?

This is what I got so far in my codebehind. Then I want to get the image and display it onto my table in my .net page. (The images are of .png type and stored as VarBinary)

HANDLER

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Configuration;
using System.Web.Configuration;
using System.IO;


namespace RocoSportsWA
{
    public class DisplayImage : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private SqlConnection _connection;
        private SqlCommand _command;
        private HttpContext _context;

        public void ProcessRequest(HttpContext context)
        {

        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public IAsyncResult BeginProcessRequest(HttpContext context,  AsyncCallback cb, object state)
        {
            // Get the employee ID from the query string
            string _logo = context.Request["Logo"];
            if (String.IsNullOrEmpty(_logo))
                return null;

            int logo = 0;
            bool ok = int.TryParse(_logo, out logo);
            if (!ok) return null;

            _context = context;
            string conn = WebConfigurationManager.ConnectionStrings["Data Source=ROBEL-HP;Initial Catalog=RocoSportsDB;Integrated Security=True"].ConnectionString;
            // Select the image from the database
            _connection = new SqlConnection(conn);
            _connection.Open();
            _command = new SqlCommand("SELECT Logo from TEAM where Team = @HomeTeam, _connection");
            _command.Parameters.AddWithValue("@HomeTeam", logo);
            return _command.BeginExecuteReader(cb, state);
        }

        public void EndProcessRequest(IAsyncResult ar)
        {
            try
            {
                SqlDataReader reader = _command.EndExecuteReader(ar);
                if (reader != null && reader.HasRows)
                {
                    // Get the image returned in the query
                    reader.Read();
                    try
                    {
                        byte[] image = (byte[])reader[0];
                        // WRite the image into the HTTP response output stream
                        _context.Response.ContentType = "image/png";
                        // strip off the 78 byte Ole header (a relic from old MS Access databases)
                        _context.Response.OutputStream.Write(image, 78, image.Length - 78);
                    }
                    catch
                    {

                    }
                }
            }
            finally
            {
                if (_connection != null)
                    _connection.Close();
            }
        }
    }
}

WEBPAGE.NET

<asp:Image ID="HomeTeamImage" runat="server" ImageUrl='<%# "DisplayImage.cs?Logo=" + Eval("HomeTeam") %>'

The HomeTeam in Eval is a label, since I am getting the text in that from session[“HomeTeam”]

CODEBEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace RocoSportsWA.Reporter
{
    public partial class LiveGameReporting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Match.Text = Session["HomeTeam"] + " vs. " + Session["AwayTeam"];
            DateTime.Text = "Date: " + Session["Date"];
            Stadium.Text = "Stadium: " + Session["Stadium"];
            HomeTeam.Text = "" + Session["HomeTeam"];
            AwayTeam.Text = "" + Session["AwayTeam"];

            SqlConnection conn = new SqlConnection("Data Source=ROBEL-HP;Initial Catalog=RocoSportsDB;Integrated Security=True");
            conn.Open();


                SqlCommand HomeTeamcmd = new SqlCommand("SELECT Logo from TEAM where Team = @HomeTeam", conn);
                SqlCommand AwayTeamcmd = new SqlCommand("SELECT Logo from TEAM where Team = @AwayTeam", conn);

                HomeTeamcmd.Parameters.AddWithValue("@HomeTeam", Session["HomeTeam"]);
                AwayTeamcmd.Parameters.AddWithValue("@AwayTeam", Session["AwayTeam"]);

                conn.Open();
                byte[] HomeTeamImageByte = (byte[])HomeTeamcmd.ExecuteScalar();
                byte[] AwayTeamImageByte = (byte[])AwayTeamcmd.ExecuteScalar();

                var bitmapImage = new BitmapImage();
                bitmapImage.SetSource(new MemoryStream(HomeTeamImageByte));
                Image1.Source = bitmapImage;
        }

        }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:13:41+00:00Added an answer on May 27, 2026 at 2:13 am

    This code will convert a binary blob to an image:

    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(new MemoryStream(imageData));
    newImage.Source = bitmapImage;
    

    where imageData is of type byte[].

    As long as you set up your database mapping correctly the data will be in the right format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: ASP.Net:Best way to run scheduled tasks How to fire a server side
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Best way to get files from a dir filtered by certain extension
Possible Duplicate: What is the best way to store user settings for a .NET
Possible Duplicate: Best way to programmatically detect iPad/iPhone hardware How can I differentiate the
Possible Duplicate: best way to clear contents of .NET’s StringBuilder Is there a quick
Possible Duplicate: Lucene.Net and SQL Server I need to storage and index files, like
Possible Duplicate: Best Way to Sprite Images? I have the following image that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.