Search code examples
c#performanceoptimizationbase64data-uri

performance of method to convert image to base64


I use the following method to convert an image to a base64 string:

FileStream fs = new FileStream(imagePath, FileMode.Open,FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
return Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);

This method is run at runtime several times for each page load. I am concerned of the impact on performance on my site.

I am aware it will impact on performance but will it significantly impact on it?


Solution

  • In general the best way to determine performance impact is to measure it. It looks like this might cause some CPU usage, memory overhead, and some disk IO, so those are the areas that I'd watch for trouble. Use a load testing tool to simulate a realistic number of concurrent users, and see what happens.

    For what it is worth, if those images are always the same ones, then you can probably cache the output of this method.