SORU
1 EYLÜL 2010, ÇARŞAMBA


Bu siteden resim indir .NET/C#

Sitesinden resim indirmek için çalışıyorum. Ben kullanıyorum kodu görüntü mevcut iken gayet iyi çalışıyor. Eğer görüntüde sorun yaratıyor mevcut değil. Nasıl görüntünün durumu doğrulamak için?

Kod:

Yöntem 1:

WebRequest requestPic = WebRequest.Create(imageUrl);

WebResponse responsePic = requestPic.GetResponse();

Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error

webImage.Save("D:\\Images\\Book\\"   fileName   ".jpg");

Yöntem 2:

WebClient client = new WebClient();
Stream stream = client.OpenRead(imageUrl);

bitmap = new Bitmap(stream); // Error : Parameter is not valid.
stream.Flush();
stream.Close();
client.dispose();

if (bitmap != null)
{
    bitmap.Save("D:\\Images\\"   fileName   ".jpg");
}

Düzenleme:

Akış şu ifadeler vardır:

      Length  '((System.Net.ConnectStream)(str)).Length' threw an exception of type  'System.NotSupportedException'    long {System.NotSupportedException}
    Position  '((System.Net.ConnectStream)(str)).Position' threw an exception of type 'System.NotSupportedException'    long {System.NotSupportedException}
 ReadTimeout  300000    int
WriteTimeout  300000    int

CEVAP
1 EYLÜL 2010, ÇARŞAMBA


Herhangi bir resim dersleri dahil etmek için gerek yok, sadece WebClient.DownloadFile arayın:

string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}

Güncelleme
Eğer varsa dosyanın var olup olmadığını kontrol etmek ve dosya indirmek için isteyeceksiniz beri, daha iyi aynı istek içinde bunu yapmak için. İşte bu kadar, bu bir yöntem:

private static void DownloadRemoteImageFile(string uri, string fileName)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Check that the remote file was found. The ContentType
    // check is performed since a request for a non-existent
    // image file might be redirected to a 404-page, which would
    // yield the StatusCode "OK", even though the image was not
    // found.
    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 oit
        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);
        }
    }
}

Kısaca, dosya için bir istek, yanıt kodu OK, Moved Redirect biri olduğunu doğrular yaparve ayrıcaContentType bir görüntü olduğunu. Eğer bu koşullar doğruysa, dosya indirilir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • backyardjay

    backyardjay

    8 ŞUBAT 2009
  • bunliu

    bunliu

    2 Mayıs 2007
  • Jason Parker

    Jason Parker

    14 Aralık 2009