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

  • Home
  • SEARCH
  • 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 7835587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:42:17+00:00 2026-06-02T13:42:17+00:00

I’m trying to output an image to the output stream through an HttpHandler but

  • 0

I’m trying to output an image to the output stream through an HttpHandler but it keeps throwing an ArgumentException. I’ve googled the issue and tried so many things but I still couldn’t fix the problem. Anyway, here’s the code:

    public void ProcessRequest(HttpContext context)
    {
        Int32 imageId = context.Request.QueryString["id"] != null ? 
            Convert.ToInt32(context.Request.QueryString["id"]) : default(Int32);

        if (imageId != 0)
        {
            //context.Response.ContentType = "image/jpeg";
            Byte[] imageData = this._imageInfoManager.GetTradeMarkImage(imageId);

            using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
            {
                using (Image image = Image.FromStream(ms, true, true)) //this line throws
                {
                    image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                }
            }
        }
        throw new ArgumentException("Image could not be found.");
    }

Note that the imageData byte array is not empty and the memory stream is being filled up correctly. Any thoughts?

UPDATE:
Here’s the code for GetTradeMarkImage… Note that the images are stored in the an SQL Server database in the image format.

    public Byte[] GetTradeMarkImage(Int32 id)
    {
        object result = DB.ExecuteScalar(SqlConstants.SqlProcedures.Request_GetImageById, id);

        if (result != null)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, result);
                return ms.ToArray();
            }                
        }
        return null;
    }
  • 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-06-02T13:42:20+00:00Added an answer on June 2, 2026 at 1:42 pm

    Okay, now you’ve posted the GetTradeMarkImage code, that’s almost certainly the problem:

    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(ms, result);
        return ms.ToArray();
    }
    

    Why would you expect the value of BinaryFormatter to be a valid image stream? It’s not clear what’s in your database (a BLOB?) nor what the execution-time type of result is here (which you should find out by debugging) but you shouldn’t be using BinaryFormatter here. I suspect you just want to get the raw data out of the database, and put that into the byte array.

    If you’re lucky, you may just be able to cast result to byte[] to start with. (I don’t know what ExecuteScalar does with blobs, and this obviously isn’t the “normal” ExecuteScalar method anyway). Otherwise, you may need to use an alternative approach, opening a DataReader and getting the value that way.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.