Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Convert bitmap to base64 string using System.Drawing.Common package in C#

$
0
0

Hello all!

I'm trying to use this library to create base64 string from Bitmap image using code like this:

Bitmap bitmap = new Bitmap(1,1);
 FillBitmap();      
 byte[] bytes = (byte[])TypeDescriptor.GetConverter(bitmap)
            .ConvertTo(bitmap, typeof(byte[]));
 string result = Convert.ToBase64String(bytes);

I'm get an exception on third line: 

An exception of type 'System.NotSupportedException' occurred in System.ComponentModel.TypeConverter.dll but was not handled in user code: ''TypeConverter' is unable to convert 'System.Drawing.Bitmap' to 'System.Byte[]'.' at System.ComponentModel.TypeConverter.GetConvertToException(Object value, Type destinationType) at System.ComponentModel.TypeConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType) at System.ComponentModel.TypeConverter.ConvertTo(Object value, Type destinationType)

Why it's wrong? How can I convert Bitmap to bytes array?

Thank you.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>