As DerekTP123 said, don't use regex for this. Work with the JSON object - you have width, height and URL just as you need it. it will make the code simpler to read, easier to maintain, and less error prone.
Sure you will have to learn how to deal with JSON, but as JSON is everywhere - you can as well learn it and get the benefits right away.
The most popular library is JSON.NET - so I recommend you stick to that, as you will find most examples using this. Microsoft use it as well.
Google something like: Json.Net tutorial.
In general you have two options - Write a C# object representing the JSON and have JSON.NET fill it out. A bit more typing initially, but then it is easy to use. See more
here[
^].
Alternatively you can read the object as a JsonObject. Then you have to index all the properties yourself. You won't need any C# classes representing the json - but you will have to make sure you type the right property names when using it - the compiler won't help you. Some examples
here[
^].
If - as you mention in the comment - it isn't easy to access the JSON for one or another reason, it is possible to write a regex, just be prepared to fiddle with it regularly to keep it working.
I have created an example that seems to work:
"width"\s*:\s*(?'width'\d+).+?"height"\s*:\s*(?'height'\d+).+?"url"\s*:\s*\"(?'url'.+?)"
It relies on the order of the properties in the json object - something you really shouldn't do as the property order isn't significant in json - meaning the code writing it could change it around for no apparent reason - but it is not easy to write a simple regex that can handle reordering.
The regex is pretty straight forward as regexes go. First it looks for
"width"
, a colon (with optional whitespace around it), followed by a named caption group 'width' taking the next digits. The
?'width'
just inside the parenthesis defines the name. It is not necessary, but it makes it easier to extract the values later in a robust way - as future changes could add additional groups (a group being anything in parenthesis).
It then skips until
"height"
which is captured the same way. Notice the skipping over other json properties is done using
.+?
. The trailing
?
tells the regex to "stop" at the first possible opportunity (so the first time it can match
"height"
. Without this, the regex is "greedy" - so it will match as much as it can. This would make it read the first width in your text, then skip all the way to the last height - and you would only get a single match.
Finally it skips to
"url"
and creates a new group capturing the text inside the following quotes - again using a non-greedy match to make sure it stop at the first quote instead of eating your entire text in one go.
You can play with making it more restrictive to avoid false positives of course. I recommend you use an online validator to quickly see the result of your changes, maybe something like
regex101.com[
^]
Once you have the URL it is easy to download the image (as long as the site does not try to stop you).
I recommend you look at
WebClient[
^]
Specifically the methods OpenRead, DownloadData, and DownloadFile. You can use any of them, but depending on what you want to do with the image, one will most likely offer a more convenient output than the other two. You can also replace your HttpWebRequest/HttpWebResponse with a WebClient. It will do all work reading the response stream for you (basically it just wraps the HttpWebRequest/Response and will do all the boring stuff for you).