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 1084525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:34:20+00:00 2026-05-16T22:34:20+00:00

This is a C# Winforms App in .NET 4.0. I receive a byte array

  • 0

This is a C# Winforms App in .NET 4.0.

I receive a byte array from a web server which is supposed to be a JPG image. I convert this array to an image as follows:

// byte[] ImageData ...
bool ValidImage = false;
try
{
    MemoryStream ms = new MemoryStream(ImageData);
    Bitmap FinalImage = new Bitmap(ms);
    ValidImage = true;
}
catch (ArgumentException)
{
    ImageStatus = "Invalid";
}
if (ValidImage) // do stuff...

Very often the data received is not an image but garbage of some sort. I really would prefer some sort of TryParse approach, as I understand handling logic by using exception catching to be less than desirable.

How can I improve this method to cleanly determine if a byte array can be converted to an image without relying on an exception?

EDIT:

Based on Mikael’s answer, I am now checking the first two bytes for a rudimentary “sanity check” before attempting to convert the image. It’s still possible to have an invalid data stream, but since I usually receive text instead of an image, the header check will greatly reduce the frequency of the exception.

// byte[] ImageData ...
bool ValidImage = false;
try
{
    if (ImageData[0] == 0xFF && ImageData[1] == 0xD8)
    {
        MemoryStream ms = new MemoryStream(ImageData);
        Bitmap FinalImage = new Bitmap(ms);
        ValidImage = true;
    }
}
catch (ArgumentException)
{
    ImageStatus = "Invalid";
}
if (ValidImage) // do stuff...
  • 1 1 Answer
  • 2 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-16T22:34:21+00:00Added an answer on May 16, 2026 at 10:34 pm

    You could check the first bytes to validate the header at least.

    byte 1-2: 0xFFD8

    byte 3-4: pixel width

    byte 5-6: pixel height

    if these are sensible you are on your way.

    As for using an exception to handle the errors, I don’t think it should be a problem. You’re only handling the particular error, and for a reason.

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

Sidebar

Related Questions

I have an asp.net web app which holds its data in an sql server
I'm converting an app from ASP.NET WebForms to WinForms. There is one asp.net page
we have WinForms app which stores data in SQL Server (2000, we are working
I am trying to migrate a simple WinForms app to a ASP.Net web app.
We have a .NET 2.0 winforms app that connects to a SQL Server 2005
Is this possible to check in assembly what client (winforms app or asp.net page)
I have a NET 2.0 Winforms app based partly on this code . It
Using c#, VS2005, and .NET 2.0. (XP 32 bit) This is a Winforms app
I have a .net winforms app which has a few animation effects, fade ins
I've got a VB.NET WinForms app in which I have the need to refer

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.