Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2007, 06:33 PM
Authorized User
 
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determine image weight

hello, i want to load an image from a website using either httpwebrequest or the webclient, and determine the image size from the request/client.

example:

image located at http://test/site/I/test.jpg

use the httpwebrequest to determine the image size and weight?

 
Old November 20th, 2007, 12:35 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I don't think you'll be able to do this with httpwebrequest alone. You can use that to GET (literally) the image data which you could then load into the Image .NET class. From there you can interrogate the class' properties.

-Peter
 
Old November 20th, 2007, 01:16 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

To demonstrate what Peter is saying you could do something like this:
Code:
        WebRequest objRequest = WebRequest.Create("http://test/site/I/test.jpg");
        objRequest.Method = "GET";

        WebResponse objResponse = objRequest.GetResponse();
        System.Drawing.Image b = System.Drawing.Bitmap.FromStream(objResponse.GetResponseStream());
the instance B will now contain the Width, Height, etc about your image, however, you can NOT get the file size of the image from the Image class. You have 2 choices here:
Save the file to disk and use the FileInfo class to get the file size or a slightly less accurate method would be to convert the image into a Byte array and get the length of that array to get a determined file size.

hth.

--Edit:
To the end of using the HttpWebRequest/Response classes, you can declare objRequest and objResponse as an HttpWebRequest and HttpWebResponse respectively but you would modifiy the code:

Code:
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://test/site/I/test.jpg");
...
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse()
hth.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 26th, 2007, 05:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You might be able to check the ContentLength on the HttpWebResponse, but depending on how the server serves up images this might be the filesize, or might be 0 or -1. You can try setting HttpWebRequest.Method to "HEAD" and then checking the ContentLength on the HttpWebResponse if you only want the filesize.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 26th, 2007, 05:34 PM
Authorized User
 
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!

i'm able to get the weight of the image from the content parameters of the webresponse object and get the dimensions from the image object.

Thanks everyone!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine an Image dimension - ASP cancer10 Classic ASP Databases 3 November 21st, 2006 10:48 AM
Determine if numeric Scootterp Access VBA 4 March 2nd, 2006 08:44 AM
Determine OS adman Beginning VB 6 2 January 5th, 2004 02:26 AM
Need Help: Can't determine cause of error xgbnow Visual C++ 3 September 22nd, 2003 05:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.