I am using the following code to retrieve a bitmap from a file located on my computer system:
Bitmap shellThumb = sf.Thumbnail.ExtraLargeBitmap;
where sf is a ShellFile.
I then use the following to convert the bitmap to base64:
public static string ToBase64String(this Bitmap bmp, ImageFormat imageFormat)
{
string base64String = string.Empty;
MemoryStream memoryStream = new MemoryStream();
bmp.Save(memoryStream, imageFormat);
memoryStream.Position = 0;
byte[] byteBuffer = memoryStream.ToArray();
memoryStream.Close();
base64String = Convert.ToBase64String(byteBuffer);
byteBuffer = null;
return base64String;
}
I then call this method using the following:
string base64ImageAndTag = shellThumb.ToBase64String(ImageFormat.Png);
I then use JSON.Net to write this string as well as other properties of the file to a JSON file to be used in another project - there is no alternative to JSON, it has to be used.
string json = JsonConvert.SerializeObject(files.ToArray(), Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
where files is a List.
I have to read in a thumbnail for each file in a specified folder.
From reading in the files, I get an OutOfMemory Exception and the conversion is the cause of this while writing to the JSON file.
Is there a way to better encode the bitmap so that the exception is avoided? I have searched for solutions for this problem, but am not sure how/where to apply any possible solutions to make this work.
Many thanks!!