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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:57:04+00:00 2026-05-25T14:57:04+00:00

i have the following code: var request = (HttpWebRequest)HttpWebRequest.Create(url); var response = request.GetResponse(); var

  • 0

i have the following code:

var request = (HttpWebRequest)HttpWebRequest.Create(url);
var response = request.GetResponse();
var stream = response.GetResponseStream();
if (stream != null) {
    Image newImage = Image.FromStream(stream, true);
    pic.Thumb = newImage.ImageToByteArray();
}

What happens if the read times out? Or the connection is aborted mid-download?

The docs say that it will throw an ArgumentException if it’s not a valid format or is null, but I have no idea if it will throw that exception if the image is only partially downloaded.

Unfortunately, I can’t rely on the ContentLength header to tell me the proper size of the file, because the server lies and gives a larger content length than the file actually is. So my hope is that Image.FromStream will be able to tell if the image is complete or not.

Can anyone provide some insight here?

Note: ImageToByteArray is just an extension method that uses a memory stream to convert the Image to a byte[]

UPDATE:

According to Darin, then an ExternalException gets thrown when you try to save the image. However, my own testing in which I truncated an image file shows that FromStream does in fact throw an ArgumentException if the image is not the correct number of bytes.

  • 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-25T14:57:04+00:00Added an answer on May 25, 2026 at 2:57 pm

    What happens if the read times out?

    An exception will be thrown before entering the if condition.

    Or the connection is aborted?

    An exception will be thrown before entering the if condition.

    but I have no idea if it will throw that exception if the image is only partially downloaded.

    An image cannot be partially downloaded. The GetResponseStream is a blocking method meaning that either you get everything, or an exception (or of course in your case you could also get an exception if what you fetched is not an image but some HTML page which will happen when you try to instantiate the Image GDI+ object).

    As a side note, to avoid leaking, you probably also want to wrap this Image disposable resource into a using statement.

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

Sidebar

Related Questions

I have the following code: var acOptions = { source:function (request, response) { $.ajax({
I have the following piece of code: // setup the AJAX request var pageRequest
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have an URL loader with the following code: public function getUploadURL():void { var
I have the following code: var request = new GeocodingRequest(); request.Address = postcode; request.Sensor
i have the following code: var net = require('net'); var server = net.createServer(function (stream)
I have the following code ... var len = request.Code.Trim().Length; if (len.Equals(0)) { throw
In my controller/request-handler, I have the following code: def monkey(self, **kwargs): cherrypy.response.headers['Content-Type'] = application/json
I have the following code: var tempIdx = document.getElementById('cityBuild').selectedIndex; var tempBuilding = document.getElementById('cityBuild').options[tempIdx].value; //
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var

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.