23

Displaying Base64 encoded images in .NET MAUI

 2 years ago
source link: https://blog.verslu.is/maui/displaying-base64-encoded-images-dotnet-maui/?amp%3Butm_medium=rss&%3Butm_campaign=displaying-base64-encoded-images-dotnet-maui
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
New-Project-3.png

Displaying Base64 encoded images in .NET MAUI

March 2, 2023March 1, 2023 by Gerald

This question comes up from time to time, so I decided to answer this once and for all in an easy to share form: a blogpost. Read all about how to encode and decode Base64 images for viewing in .NET MAUI.

Spoiler alert: this is not specific to .NET MAUI, you can also use this with Xamarin.Forms, or WinUI or WPF, or… Well, you get the idea!

However, there are some serious considerations about using this method, so be sure to read all the way to the bottom!

The sample code for this post is on my GitHub account: https://github.com/jfversluis/MauiBase64ImageSample

What the Base64?!

Base64 is a way to convert binary files, usually images, to text. That way you can easily store them in, for instance, a database.

For this blog post I will be using the “official” .NET MAUI logo, the ones that we find next to the templates in Visual Studio currently. You can see it down below for reference.

icon.png
The .NET MAUI Logo, not yet in Base64

Decoding Base64

If you have been dealing with converting one object to another, you probably already know the static Convert class in .NET. We can also use that same class to convert binary files from and to Base64.

Let’s first have a look at decoding an image. In the sample repository that I created for this post you can find a big string hidden in the Base64ImageProvider.cs file. This is the Base64 encoded version of the image above.

To use this in a regular .NET MAUI Image element, you can do the following.

https://gist.github.com/jfversluis/b6e0e6125d875f0e2c4bdf1dee78b39e

Sample code to demonstrate how to show a Base64 encoded image in .NET MAUI

Note how you would typically add the using keyword when using a Stream. In this case you don’t want to do that. Because that will close the Stream before the image gets loaded.

Encoding an Image as Base64 String

Now, let’s see how to do it the other way around, take an image and encode it as a string.

As you might have guessed by now, there is also a Convert.ToBase64String() method. We’ll need convert our file to a byte array and then we can convert it. In .NET MAUI, you can load a file that you provide inside of your app as a resource (under the Resources\Raw folder in the project) with the FileSystem APIs.

From that we get a Stream, that we load into a MemoryStream and a MemoryStream can be converted into a byte[].

https://gist.github.com/jfversluis/b0c25625e4693c04681719a8e992ecbb

Sample code to demonstrate how to encode an image as Base64 string in .NET MAUI

Here I am using the using keywords so that the streams I have used are cleaned up after we’re done with them.

Of course there are other ways to get files, so you might have to do something different here instead of FileSystem.OpenAppPackageFileAsync, as long as you manage to convert your file to a byte array, you should be good.

Considerations

As you might know working with strings can be a very costly operation. With this that is no different. You’re first loading a very big string in memory as a byte array and only then put it in another stream that you’re going to use as an actual image.

This will be around 2.25 times the memory usage as when you would load the image directly from a stream, as also indicated by Jonathan Peppers. You can also spot this when you run the sample app. The simple image I’m using even has a noticeable lag while loading.

Additionally, the Base64 string variant of your image will be about 20-30% larger than the actual image.

Summary

Now you know how to work with Base64 and .NET MAUI images! While this post starts with how this can be used to save image in databases, that is something I do not typically recommend.

Databases usually aren’t optimized to hold these amounts of (binary) data. Typically what you want to do is save the images somewhere on disk and then save the path to said image in the database. That will also prevent the other considerations that I have mentioned above.

So all-in-all, while this is technically possible and you could choose to do so, I would like to ask you to seriously reconsider other options for actually doing this.

The code that I’ve use for this post is on this GitHub repository: https://github.com/jfversluis/MauiBase64ImageSample

Good luck!

Categories maui Tags .Net MAUI, base64, dotnet, Images, maui

</div


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK