2

I want to implement a method to download Image from website to laptop.

public static void DownloadRemoteImageFile(string uri, string fileName)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if ((response.StatusCode == HttpStatusCode.OK ||
            response.StatusCode == HttpStatusCode.Moved ||
            response.StatusCode == HttpStatusCode.Redirect) &&
            response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
        {
             //if the remote file was found, download it
            using (Stream inputStream = response.GetResponseStream())
            using (Stream outputStream = File.OpenWrite(fileName))
            {
                byte[] buffer = new byte[4096];
                int bytesRead;
                do
                {
                    bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                    outputStream.Write(buffer, 0, bytesRead);
                } while (bytesRead != 0);
            }
        }
}

But the ContentType of request or response is not "image/jpg" or "image/png". They're always "text/html". I think that's why after I save them to local, they has incorrect content and I cannot view them.

Can anyone has a solution here? Thanks

Azzurri
  • 63
  • 2
  • 9
  • 1
    You might want to put your code in a code block and make sure it's formatted nicely. It's pretty difficult to read right now. – mmitchell Sep 29 '12 at 03:28
  • Thanks, I'm newbie and I'm learning to post a question correctly. Sorry for this inconvenient. – Azzurri Sep 29 '12 at 03:34
  • 1
    what url are you passing in to this? If the url is not hosting a jpeg, you will get whatever it is hosting. Or it could be that the site requires authentication to download images. – feroze Sep 29 '12 at 04:22
  • @feroze: here is example url. I think it host for jpg file http://interfacelift.com/wallpaper/7yz4ma1/03085_steam_1366x768.jpg – Azzurri Sep 29 '12 at 04:29

4 Answers4

0

You can use this code - based on JpegBitmapDecoder class

JpegBitmapDecoder decoder = new JpegBitmapDecoder(YourImageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
//here you can adjust your YourImageStreamSource with outputStream value

BitmapSource bitmapSource = decoder.Frames[0];

Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Save("YourImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Link : http://msdn.microsoft.com/en-us/library/aa970689.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • my problem is not decode an image. It's about my method save response as html type to and image file, so I cannot access it. Example: my url is "http://abc.com/image.jpg" and I want to save it to "C:\image.jpg". The original image on website is 500KB, but after I use my method, my image.jpg is 1KB and I cannot use it. – Azzurri Sep 29 '12 at 03:44
  • Image.Save("yourImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); – Aghilas Yakoub Sep 29 '12 at 03:52
0

Try setting the content type to specific image type

Response.ContentType = "image/jpeg";
Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
  • Respoonse.ContentType is only use to "Get" ContentType, we cannot use it to set ContentType for response. I tried to set ContentType for Request before call GetResponse(). But it doesn't work too :( – Azzurri Sep 29 '12 at 03:46
  • here is another SO post,hope it helps you http://stackoverflow.com/questions/1271701/reading-image-from-web-server-in-c-sharp-proxy – Prabhu Murthy Sep 29 '12 at 03:47
0

It may be possible that the sites you wish to get the image(s) from may need a cookie(s). Sometimes when we use our browsers to go to the site, we may not notice it, but the browser actually goes to the site for perhaps a millisecond, before quickly reloading, while at the same time getting the cookie. But before loading the site again, our browser would then pass it the cookie this time, whereby the site accepts it and returns the image.

To elaborate, this means your method would be doing only half of what your browser is actually doing. Half of 2 GET request methods. The first one would be to get the cookie, and the second one to actually get the image itself.

Information from (and maybe a bit related): C# generate a cookie dynamically that site will accept?

Community
  • 1
  • 1
Kaitlyn
  • 791
  • 1
  • 10
  • 28
-1

Your code is ok, but what you are trying to do is often considered undesired behavior by web site owners. Most sites want you to see images on the site but not download them at random. You can search for oposite of your question to know what techniqes and protections your are against to.

I strongly recommend to read usage agreement or any similar document on the site you are trying to scrape beore continuing.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179