Friday, February 6, 2009

Using C# how do get image size (bytes) from a image obtained from a URL

Hi,
You should download the image first and then check the image size. I dont think there is any other option

Here is the c# code for Image/File download
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
private int DownloadImage(string link)
{
try
{
Uri uri = new Uri(link);
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest
.Method = "GET";

webrequest
.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*";

webrequest
.KeepAlive = false;
HttpWebResponse responce = (HttpWebResponse)webrequest.GetResponse();
Stream S = responce.GetResponseStream();
FileStream writeStream = new FileStream(<<FilePath>, FileMode.Create, FileAccess.Write); //Filepath to save
ReadWriteStream(S, writeStream);
responce
.Close();
return 1;
}
catch (Exception)
{
return 0;
}
}

// readStream is the stream you need to read
// writeStream is the stream you want to write to
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream
.Write(buffer, 0, bytesRead);
bytesRead
= readStream.Read(buffer, 0, Length);
}
readStream
.Close();
writeStream
.Close();
}

2 comments:

  1. Well, the only method that always is available would be to request the file:

    int length;
    using (WebClient client = new WebClient()) {
    length = client.DownloadData(url).Length;
    }

    If the folder allows directory browsing, and there is no default page in the folder, you could request the folder to get a directory listing, which may contain the file size. It would be a size in kiB rather than bytes, though.

    ReplyDelete
  2. Well, neither of the streams support seeking. I don't get that error when testing the code. Did you try to get the size of the response stream or something?

    However, you don't have to save the file if you are only interrested in the size. Look at the code that I posted earlier, that is the simplest way to get the size, and it works fine if the file isn't too large (as it loads the entire file into memory).

    If the web server supports the HEAD method, this is the most efficent way of getting the file size:

    1:
    2:
    3:
    4:
    5:
    6:
    7:
    8:
    9:
    10:
    11:
    12:



    private static long GetSize(string url) {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "HEAD";
    //request.KeepAlive = false;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    return response.ContentLength;
    }

    Example:

    long size = GetSize("http://www.guffa.com/images/logo.gif");
    // size will be 1050

    ReplyDelete