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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:16:15+00:00 2026-05-19T02:16:15+00:00

Im getting some images from a webpage at a specified url, i want to

  • 0

Im getting some images from a webpage at a specified url, i want to get their heights and widths. I’m using something like this:

 Stream str = null;
 HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(ImageUrl);
 HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
 str = wRes.GetResponseStream();

 var imageOrig = System.Drawing.Image.FromStream(str);
 int height = imageOrig.Height;
 int width = imageOrig.Width;

My main concern with this is that that the image file may actually be very large,

Is there anything I can do? ie specify to only get images if they are less than 1mb?
or is there a better alternative approach to getting the dimension of an image from a webpage?

thanks

  • 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-19T02:16:16+00:00Added an answer on May 19, 2026 at 2:16 am

    Some (all?) image formats include the width and height property in the header of the file. You could just request enough bytes to be able to read the header and then parse them yourself. You can add a range header to your web request that will request only the first 50 bytes (50 is just an example, you’d probably need less) of the image file with:

    wReq.AddRange(0, 50);
    

    I suppose this will only work if you know that the formats you are working with include this data.

    Edit: Looks like I misunderstood the AddRange method before. It’s fixed now. I also went ahead and tested it out by getting the width and height of a png using this documentation.

    static void Main(string[] args)
    {
        string imageUrl = "http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";
        byte[] pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
        HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(imageUrl);
        wReq.AddRange(0, 30);
        WebResponse wRes = wReq.GetResponse();
        byte[] buffer = new byte[30];
        int width = 0;
        int height = 0;
    
        using (Stream stream = wRes.GetResponseStream())
        {
            stream.Read(buffer, 0, 30);
        }
    
        // Check for Png
        // 8 byte - Signature
        // 4 byte - Chunk length
        // 4 byte - Chunk type - IDHR (Image Header)
        // 4 byte - Width
        // 4 byte - Height
        // Other stuff we don't care about
        if (buffer.Take(8).SequenceEqual(pngSignature))
        {
            var idhr = buffer.Skip(12);
            width = BitConverter.ToInt32(idhr.Skip(4).Take(4).Reverse().ToArray(), 0);
            height = BitConverter.ToInt32(idhr.Skip(8).Take(4).Reverse().ToArray(), 0);
        }
        // Check for Jpg
        //else if (buffer.Take(?).SequenceEqual(jpgSignature))
        //{
        //    // Do Jpg stuff
        //}
        // Check for Gif
        //else if (etc...
    
        Console.WriteLine("Width: " + width);
        Console.WriteLine("Height: " + height);
        Console.ReadKey();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting some objects from an external library and I need to store those
I'm getting some really wierd linking errors from a class I wrote. I am
I'm working on a school project and I'm getting some weird errors from Xcode.
I am using a ListView to display some images and captions associated with those
I'm trying to pull in images from Flickr using the phpFlickr library but the
I'm getting some strange, intermittent, data aborts (< 5% of the time) in some
I am getting some errors thrown in my code when I open a Windows
I am interested in getting some Python code talking to some Ruby code on
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm getting some confusing results around myCustom.ashx handler. If i visit the handler via

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.