This post in a continuation of this earlier post around serialising an image. Once you’re able to serialise an image, then you can transmit that image. In this post, we’ll see how we could do that using Azure Service Bus.
A Recap on Image Serialisation
In the previous (linked) post, we saw how we can turn an image into a string of text, and back again. Essentially, what we did was use a BinaryWriter to write the file as a binary stream, and a BinaryReader to turn it back to an image.
The plan here is to do exactly the same thing, but to simply write the stream to an Azure Service Bus Message, and read from it at the other side.
Size Matters
One thing that you’re going to need to be aware of here is the size of the image. The free tier of Service Bus limits you to 256KB. Serialising an image to stream can be less than that, but unless you know different, you should assume that it will be bigger. Even after you sign up for the much more expensive premium tier, when you set-up the topic or queue, you’ll need to specify a maximum size. Images can be very big!
Alternatives
To be clear: the purpose of this post is to demonstrate that you can transmit an image via Service Bus - not that you should. There are other ways to do this: for example, you could upload the image to blob storage, or an S3 bucket, and then just point to that in the message. The one advantage transmitting the image with the message does give you, is that the binary data lives and dies with the message itself - so depending on what you do with the message after receipt, that may be an advantage.
Transmitting the Message
The following is a simple helper method that will send the message for us:
async Task SendMessage(string connectionString, string topicName, string messageText)
{
int byteCount = System.Text.ASCIIEncoding.ASCII.GetByteCount(messageText);
await using var serviceBusClient = new ServiceBusClient(connectionString);
var sender = serviceBusClient.CreateSender(topicName);
var message = new ServiceBusMessage(Encoding.UTF8.GetBytes(messageText));
await sender.SendMessageAsync(message);
}
byteCount tells you how big the message is going to be. Useful when you’re writing a blog post about such things, but you may find it useful for debugging.
The rest is pretty basic Azure Service Bus SDK stuff. I’ve written about this previously in more depth.
We can then combine this with the code from the last post:
var serialisedToSend = SerialiseImageToBinary(
@"c:\\tmp\\myimage.png");
await SendLargeMessage(connectionString, "image-message", serialisedToSend);
The next step is to receive the message at the other side.
Consuming the Message
As with the send, we have a helper method here:
async Task<string> ConsumeNextMessage(string topic)
{
var serviceBusClient = new ServiceBusClient(connectionString);
var receiver = serviceBusClient.CreateReceiver(topic, "sub-1");
var message = await receiver.ReceiveMessageAsync();
return message.Body.ToString();
}
Again, I’ve written about receiving a message using the SDK before.
Here, we do the reverse of the send:
var serialisedToReceive = await ConsumeNextMessage("image-message");
DeserialiseImageFromBinary(
serialisedToReceive,
@$"c:\\tmp\\newimg{DateTime.Now.ToString("yy-MM-dd-HH-mm-ss")}.png");
I’ve used the date so that I could test this multiple times - other than that, we’re receiving the serialised image, and then writing it to disk.
Summary
I’ll re-iterate the sentiment that I’m not advocating this as a good or preferable way to transmit images, simply highlighting that it is possible with relatively little work.