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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:59:02+00:00 2026-05-12T23:59:02+00:00

I am trying to check if a file is an image before I upload

  • 0

I am trying to check if a file is an image before I upload it to the image server.
I am doing it with the following function, which works exceptionally well:

static bool IsValidImage(Stream imageStream)
            {
                bool isValid = false;
                try
                {
                    // Read the image without validating image data
                    using (Image img = Image.FromStream(imageStream, false, false))
                    {
                        isValid = true;
                    }
                }
                catch
                {
                    ;
                }
                return isValid;
            }

The problem is that when the below is called immediately afterwards, The line:

while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0)

evalueates to zero and no bytes are read. I notice that when I remove the
IsValidImage function, bytes are read and the file is written. It seems
that bytes can only be read once? Any idea how to fix this?

using (FileStream outfile = new FileStream(filePath, FileMode.Create))
                    {
                        const int bufferSize = 65536; // 64K
                        int bytesRead = 0;

                        Byte[] buffer = new Byte[bufferSize];

                        while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0)
                        {
                            outfile.Write(buffer, 0, bytesRead);
                        }

                        outfile.Close(); //necessary?

                    }

UPDATE: Thanks for your help Marc. I am new to stream manipulation and could use a little
more help here. I took a shot but may be mixing up the use of filestream and memorystream.
Would you mind taking a look? Thanks again.

using (FileStream outfile = new FileStream(filePath, FileMode.Create))
using (MemoryStream ms = new MemoryStream())
{

   byte[] buffer = new byte[1024];
   int bytesRead;

   while ((bytesRead = request.FileByteStream.Read(buffer, 0, buffer.Length)) > 0)
   {
         ms.Write(buffer, 0, bytesRead);
   }

   // ms now has a seekable/rewindable copy of the data

   // TODO: read ms the first time

   // I replaced request.FileByteStream with ms but am unsure about 
   // the using statement in the IsValidImage function.

   if (!IsValidImage(ms) == true)
   {
        ms.Close();
        request.FileByteStream.Close();
        return;
   }

   ms.Position = 0;

   // TODO: read ms the second time

   byte[] m_buffer = new byte[ms.Length];
   while ((bytesRead = ms.Read(m_buffer, 0, (int)ms.Length)) > 0)
   {
        outfile.Write(m_buffer, 0, bytesRead);
   }


 } 


 static bool IsValidImage(MemoryStream imageStream)
 {
            bool isValid = false;
            try
            {
                // Read the image without validating image data
                using (Image img = Image.FromStream(imageStream, false, false))
                {
                    isValid = true;
                }
            }
            catch
            {
                ;
            }
            return isValid;
    }
  • 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-12T23:59:02+00:00Added an answer on May 12, 2026 at 11:59 pm

    As you read from any stream, the position increases. If you read a stream to the end (as is typical), and then try to read again, then it will return EOF.

    For some streams, you can seek – set the Position to 0, for example. However, you should try to avoid relying on this as it is not available for many streams (especially when network IO is involved). You can query this ability via CanSeek, but it would be simpler to avoid this – partly as if you are branching based on this, you suddenly have twice as much code to maintain.

    If you need the data twice, then the options depends on the size of the data. For small streams, buffer it in-memory, as either a byte[] or a MemoryStream. For larger streams (or if you don’t know the size) then writing to a scratch file (and deleting afterwards) is a reasonable approach. You can open and read the file as many times (in series, not in parallel) as you like.


    If you are happy the stream isn’t too large (although maybe add a cap to prevent people uploading swap-files, etc):

    using (MemoryStream ms = new MemoryStream()) {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) {
            ms.Write(buffer, 0, bytesRead);
        }
    
        // ms now has a seekable/rewindable copy of the data
    
        // TODO: read ms the first time
        ms.Position = 0;
        // TODO: read ms the second time
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before trying to query the AD server I would like to check if it
I am trying to check if a file is on the server with the
I am trying to check if a file exists in my rails application. I
I am trying to upload a file via a form and then save in
I am trying to upload files using php, and it works perfectly up until
I am trying to upload a file and send it to the service layer
I'm trying to add a file upload script in php to a website I'm
I am getting the error below when trying to upload an JPEG image to
I'm trying to find a better way to check for file access in a
I am trying to check if the file is selected in the fileupload in

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.