Download image from page with Puppeteersharp

1.1k Views Asked by At

Is there a way to download an image displayed on a page through Puppeteersharp ?

I know I could use a selector to retrieve src and then use a WebClient or HTTPRequest but I am wondering if I could do it directly

1

There are 1 best solutions below

2
On

If anyone needs it, here is the code I used below :

        Base64Image img = new Base64Image();            
        using (Page page = await parentPage.Browser.NewPageAsync())
        {                
            var tcs = new TaskCompletionSource();
            page.Response += async (sender, e) =>
            {
                string contentType = null;
                string[] imageMimeTypes = { "image/jpeg", "image/png", "image/webp", "image/gif" };

                if (e.Response.Headers.ContainsKey("content-type"))
                    contentType = (string)e.Response.Headers["content-type"].ToLower();

                if (e.Response.Headers.ContainsKey("Content-Type"))
                    contentType = (string)e.Response.Headers["Content-Type"].ToLower();

                if (imageMimeTypes.Any(t => t == contentType))
                {
                    img.ContentType = contentType;
                    

                    await e.Response.BufferAsync().AsTask().ContinueWith(async (data) => img.Bytes = await data);   
                    tcs.TrySetResult();
                }
            };
            await Task.WhenAll(page.GoToAsync(uri.AbsoluteUri), tcs.Task);
            await page.CloseAsync();
        }
        return img;