How to download image data (bytes) using ASP.NET
Hello and welcome back to another tip for my ASP.NET programmers!
So I’ve noticed in my career where data importing is an extremely important and valuable skill to possess as a programmer. Knowledge is power after all! With that power comes the responsibility of downloading and processing images.
Now if you’re not familiar with this (as I was when I first started) or have forgotten how to do this with dot net. Then a quick stack overflow search will land you on a solution similar to this one.
We have a string that holds our image URL.
We have a WebClient object that acts as our web browser.
We have a byte array to hold our retrieved image data.
string imageUrl = "https://placehold.it/300/450";
WebClient client = new WebClient();
byte[] imageBytes = client.DownloadData(imageUrl);
This is all well and fine for a sample of “How to do it” but this doesn’t really cover a lot of ground or use cases. Simply copying and pasting this into your codebase really isn’t enough.
It may work for a while, but as you send more URLs through this code you’ll start to notice its drawbacks and lack of robust data handling abilities.
First of all with this code as it sits we have to remember to dispose of the web client. We can remedy that by wrapping this code in a pattern based using-declaration. This is doable on anything that implements the IDisposable interface.
using(WebClient client = new WebClient())
{
string imageUrl = "https://placehold.it/300/450";
byte[] imageBytes = client.DownloadData(imageUrl);
}
This is much better because now the client will dispose of itself when the client is done and out of scope (ie. no longer needed).
But this still doesn’t solve all our problems with this little block of code.
Some resources will block a direct request like this. Typically in the form of a 503 error. You could try and catch this error and either keep trying to process other images, allow the code to keep going if you’re not worried about getting that image, or you can add a few extra lines of code to make this work.
First off, you can set your web client to have a TLS12 protocol passed over on the request. This alone solved about 85% of my 503 errors when retrieving image data.
using(WebClient client = new WebClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string imageUrl = "https://placehold.it/300/450";
byte[] imageBytes = client.DownloadData(imageUrl);
}
To have this work as written you’ll have to add a using statement at the top of your cs file. This protocol is provided with .Net out of the box.
using System.Net;
Try this to solve your 503 errors when trying to access images.
Some servers are even more guarded against downloading images, but you can still circumvent this by passing in a user-agent string in the header.
To add a user-agent header to this request you simply do the following:
using(WebClient client = new WebClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Headers.Add("user-agent", "user agent string");
string imageUrl = "https://placehold.it/300/450";
byte[] imageBytes = client.DownloadData(imageUrl);
}
Once this was added, weeks later, and hundreds of image sources parsed and downloaded, I’ve yet to have an issue downloading image data.
So tell me, what are your solves for downloading image data? Did any of these suggestions help you?
Good luck and keep programming!
-Cory