Trying to generate thumbnails from a byte array containing jpg or png extension images and then show it on a razor page. Here is what I currently have. The byte array comes from an object class which contains a field called FileContent
Any help on what I’m doing wrong and next steps would be much appreciated
My current approach was to take the byte array of the image, resize it as a thumbnail image, and then save that as another array, finally using the byte array conversion to try and load the image on the front-end, which has not worked
Razor Page
<QuickGrid Items="@BlobNames">
<TemplateColumn Title="Thumbnail">
@{
var currItem = (context as DocumentObject);
byte[] thumbnail = GenerateThumbnail(currItem.FileContent);
var thumbnailUrl = $"data:image/jpeg;base64,{Convert.ToBase64String(thumbnail)}";
}
</QuickGrid>
Backend
protected byte[] GenerateThumbnail(byte[] imageStream)
{
using (MemoryStream ms = new MemoryStream(imageStream))
{
using (Image originalImage = Image.FromStream(ms))
{
using(Image thumbnail = originalImage.GetThumbnailImage(64,64,()=>false, IntPtr.Zero))
{
using(MemoryStream thumbnailStream = new MemoryStream())
{
thumbnail.Save(thumbnailStream, originalImage.RawFormat);
return thumbnailStream.ToArray();
}
}
}
}
}
When I run the program I get the following error:
Unhandled exception rendering component: The type initializer for ‘System.Drawing.DrawingCom’ threw an exception.
System.TypeInitializationException: The type initializer for ‘System.Drawing.DrawingCom’ threw an exception.
Aashir Mallick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.