In my previous article I showed you how to retrieve the page views from Google Analytics, now it's time to write them on an image using .NET
For the Twitter cover photo I have this 1500x500 PNG file.
As you can see, after the current status there's empty space, so I can write there the number of blog views. For this I used another .NET Core 3.1 console application, where I installed the System.Drawing.Common Nuget package. I will eventually use an Azure Function for all of this, but I always use a simple console app when I have to do a PoC, so for the moment we keep things simple!
The code looks like this:
There's a single function called UpdateImage(), and the code it's pretty straightforward:
- I placed the original cover photo in D:\banner.png, then I sent this in the first parameter
- in the second parameter I asked to create a new image at D:\new-banner.png
- the third parameter has the text, in this case I just used "100.000 views" for demo purposes
The function allows me to do some customizations like:
- the color of the text and the position where it will be written(line 26)
- font attributes like family, weight and size (line 24)
There are more possibilities but right now I'm keeping things simple.
The result, after being uploaded manually to Twitter, looks like this:
In the next blog post I'll retrieve the number of views every hour with an Azure Function, write it on the image, and automatically upload it to Twitter.
As I said in yesterday's blog post, I'll also stream this on Twitch or YouTube on Wednesday, so if you enjoy a video more than a blog post then you might want to watch that. I haven't decided yet on which platform to stream but if you have any suggestions I would definitely appreciate them in the comments below :) I'll pick a time and announce it a few hours ahead on my Twitter profile, so you can follow me there if you don't want to miss it!