I have an ID from a Steam Workshop submission, how can I get the Thumbnail Image in C#?
public async static Task<Image> GetSteamWorkshopSubmissionThumbnail(string id)
{
}
Not an ideal solution, but here's my current approach:
How it works: Fetch the HTML page of the submission URL, regex match for the url of the image, fetch the image from url, return it.
Should probably add CancellationToken arg.
The ideal approach is to use the official Steam API. I got no idea how to do that, so this is what we're working with.
If someone posts a better solution I'll mark that as the answer.
Edit:
Improved to handle some issues:
It's possible it only has the thumbnail, but no other images, added another regex to resolve.
It's possible the workshop submission doesn't have any image, now handles that. (Returns default image)
public async static Task<Image> GetSteamWorkshopThumbnail(string id)
{
try
{
string steamCommunityFileIDURL = "https://steamcommunity.com/sharedfiles/filedetails/?id=";
string url = steamCommunityFileIDURL + id;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
WebBrowser wb = new WebBrowser();
wb.DocumentStream = response.GetResponseStream();
wb.ScriptErrorsSuppressed = true;
HtmlDocument doc = wb.Document;
await Utilities.WaitUntil(() => doc.ActiveElement != null);
Regex workshopItemPreviewImageMainFilter = new Regex(@"(?<=workshopItemPreviewImageMain src="")https:\/\/steamuserimages-a.akamaihd.net\/ugc\/[0-9]*\/\w+\/");
Regex previewImageMainFilter = new Regex(@"(?<=workshopItemPreviewImageEnlargeable src="")https:\/\/steamuserimages-a.akamaihd.net/ugc/[0-9]+/\w+/");
string imageURL = workshopItemPreviewImageMainFilter.Match(doc.ActiveElement.OuterHtml).ToString();
if (imageURL == "")
{
imageURL = previewImageMainFilter.Match(doc.ActiveElement.OuterHtml).ToString();
}
if (imageURL == "") { imageURL = "https://community.cloudflare.steamstatic.com/public/images/sharedfiles/steam_workshop_default_image.png"; }
Image image = null;
using (WebClient webClient = new WebClient())
{
using (Stream stream = webClient.OpenRead(imageURL))
{
image = Image.FromStream(stream);
}
}
return image;
}
catch { return null; }
}