3

I have a link given by an ONVIF ip camera that contains a snapshot taken by the said camera.

When I try to open this link on a browser like chrome, I get the following prompt:

Your connection to this site is not private

When I try to load this image from a c# windows form picturebox, I get the following error:

Load:

picturebox0.Load(mySnapUrl);

Error:

System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'

I can see the image in my browser once I enter the appropriate username and password.

Is there any way I could load such an image in a picturebox?

EDIT 1:

I tried this solution to manually load the image on a web client in which I added credentials by hand and I still get the same error at the downloadData line.

var webClient = new WebClient();
var credentialCache = new CredentialCache();
credentialCache.Add(new Uri(mySnapUrl), "Basic", new NetworkCredential(user, password));
webClient.Credentials = credentialCache;
var imgStream = new MemoryStream(webClient.DownloadData(mySnapUrl));//Error
picturebox0.Image = new System.Drawing.Bitmap(imgStream);
11
  • 4
    If the camera vendor doesn't provide any technical details, you can use a tool such as Fiddler (or the browser embedded developer tools / F12) to sniff the wire and check what's the authentication. Also, you don't need a CredentialCache, you can just set wc.Credentials = new NetworkCredential(u,p) Feb 26, 2019 at 9:15
  • 1
    Have you tried this? Feb 27, 2019 at 2:35
  • @SimonMourier if you want the bounty, just copy the answer I gave. I'll delete mine.
    – LoukMouk
    Mar 1, 2019 at 18:17
  • @RezaAghaei same goes to you. First one to do will get it!
    – LoukMouk
    Mar 1, 2019 at 18:17
  • 1
    @LoukMo - such (rare) courtesy deserves a reward, please keep your answer :-) Mar 1, 2019 at 19:21

1 Answer 1

4

As @Simon Mourier and @Reza Aghaei said in the comments, I didn't need to add a CredentialCache but only Credentials. The solution is similar to this one.

Solution:

var webClient = new WebClient();
webClient.Credentials = new NetworkCredential(user, password);
var imgStream = new MemoryStream(webClient.DownloadData(mySnapUrl));//Good to go!
picturebox0.Image = new System.Drawing.Bitmap(imgStream);

Edit:

I personally had to be able to load the said image asynchronously, because I used to load my images with picturebox0.LoadAsync(mySnapUrl).

I got the big idea from this source.

To be able to to the same with an image that needs credentials, I created an async Task to load the image...

private async Task<Image> GetImageAsync(string snapUrl, string user, string password)
{
    var tcs = new TaskCompletionSource<Image>();

    Action actionGetImage = delegate ()
    {
        var webClient = new WebClient();
        webClient.Credentials = new NetworkCredential(user, password);
        var imgStream = new MemoryStream(webClient.DownloadData(snapUrl));
        tcs.TrySetResult(new System.Drawing.Bitmap(imgStream));
    };

    await Task.Factory.StartNew(actionGetImage);

    return tcs.Task.Result;
}

... and then set the image with the following:

var result = GetImageAsync(mySnapUrl, user, password);
result.ContinueWith(task =>
{
    picturebox0.Image = task.Result;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.