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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:37:39+00:00 2026-06-07T11:37:39+00:00

I’ve written some code to import content from my Blogger blog. Once I’ve downloaded

  • 0

I’ve written some code to import content from my Blogger blog. Once I’ve downloaded all of the HTML content, I go through the image tags and download the corresponding images. In a significant number of cases, System.Drawing.Bitmap.FromStream is throwing an ArgumentException. The URL I’m downloading from looks good and it serves up an image as expected (here’s the URL for one of the problem images: http://4.bp.blogspot.com/_tSWCyhtOc38/SgIPcctWRZI/AAAAAAAAAGg/2LLnVPxsogI/s1600-h/IMG_3590.jpg).

    private static System.Drawing.Image DownloadImage(string source)
    {
        System.Drawing.Image image = null;

        // used to fetch content
        var client = new HttpClient();

        // used to store image data
        var memoryStream = new MemoryStream();

        try
        {
            // fetch the image
            var imageStream = client.GetStreamAsync(source).Result;

            // instantiate a system.drawing.image from the data
            image = System.Drawing.Bitmap.FromStream(imageStream, false, false);

            // save the image data to a memory stream
            image.Save(memoryStream, image.RawFormat);
        }
        catch (IOException exception)
        {
            Debug.WriteLine("{0} {1}", exception.Message, source);
        }
        catch (ArgumentException exception)
        {
            // sometimes, an image will link to a web page, resulting in this exception
            Debug.WriteLine("{0} {1}", exception.Message, source);
        }
        catch (AggregateException exception)
        {
            // sometimes, an image src will throw a 404
            Debug.WriteLine("{0} {1}", exception.Message, source);
        }
        finally
        {
            // clean up our disposable resources
            client.Dispose();
            memoryStream.Dispose();
        }

        return image;
    }

Any idea why an ArgumentException is getting thrown here?

EDIT: It occurred to me that it could be a proxy issue, so I added the following to my web.config:

<system.net>
  <defaultProxy enabled="true" useDefaultCredentials="true">
    <proxy usesystemdefault="True" />
  </defaultProxy>
</system.net>

Adding that section hasn’t made any difference, however.

EDIT: This code is called from the context of an EF database initializer. Here’s a stack trace:

Web.dll!Web.Models.Initializer.DownloadImage(string source) Line 234 C#
Web.dll!Web.Models.Initializer.DownloadImagesForPost.AnonymousMethod__5(HtmlAgilityPack.HtmlNode tag) Line 126 + 0x8 bytes C#
[External Code]
Web.dll!Web.Models.Initializer.DownloadImagesForPost(Web.Models.Post post) Line 119 + 0x34 bytes C#
Web.dll!Web.Models.Initializer.Seed(Web.Models.FarmersMarketContext context) Line 320 + 0xb bytes C#
[External Code]
App_Web_l2h4tcej.dll!ASP._Page_Views_Home_Index_cshtml.Execute() Line 28 + 0x15 bytes C#
[External Code]

  • 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-07T11:37:40+00:00Added an answer on June 7, 2026 at 11:37 am

    OK, I found the issue. It turns out that, in some cases, Blogger references an HTML page that renders an image rather than referencing the image itself. So, the response in that case isn’t a valid image. I’ve added code to check the response headers before attempting to save the image data and that’s fixed the problem. For the benefit of anyone else who hits this issue, here’s the updated code:

        private static System.Drawing.Image DownloadImage(string source)
        {
            System.Drawing.Image image = null;
    
            // used to fetch content
            var client = new HttpClient();
    
            // used to store image data
            var memoryStream = new MemoryStream();
    
            try
            {
                // Blogger tacks on a -h to an image Url to link to an HTML page instead
                if (source.Contains("-h/"))
                    source = source.Replace("-h/", "/");
    
                // fetch the image
                var response = client.GetAsync(source).Result;
                response.EnsureSuccessStatusCode();
    
                var contentType = response.Content.Headers.ContentType.MediaType;
    
                if (!contentType.StartsWith("image/"))
                {
                    Debug.WriteLine(contentType);
                    throw new ArgumentException("Specified source did not return an image");
                }
    
                var imageStream = response.Content.ReadAsStreamAsync().Result;
    
                // instantiate a system.drawing.image from the data
                image = System.Drawing.Bitmap.FromStream(imageStream, true, true);
    
                // save the image data to a memory stream
                image.Save(memoryStream, image.RawFormat);
            }
            catch (HttpRequestException exception)
            {
                // sometimes, we'll get a 404 or other unexpected response
                Debug.WriteLine("{0} {1}", exception.Message, source);
            }
            catch (IOException exception)
            {
                Debug.WriteLine("{0} {1}", exception.Message, source);
            }
            catch (ArgumentException exception)
            {
                // sometimes, an image will link to a web page, resulting in this exception
                Debug.WriteLine("{0} {1}", exception.Message, source);
            }
            finally
            {
                // clean up our disposable resources
                client.Dispose();
                memoryStream.Dispose();
            }
    
            return image;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.