简体   繁体   中英

Converting bitmap to base64string

I'm trying to add a bitmap to a json string I'm sending to a website. To do this I'm trying to convert the bitmap into a base64string, but this string turns up to be empty if checked on the website.

This is the code I use to covert the bitmap into a base64string

Bitmap bmp = (Bitmap)Image.FromFile(@"C:\ProgramData\test.jpg", true);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
bmp.Save(mStream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = mStream.ToArray();
string base64String = Convert.ToBase64String(imageBytes);

but base64String seems to be empty, yet if I make a bitmap out of it again and save that bitmap to my PC everything seems to be fine.

To do this I'm trying to convert the bitmap into a base64string, but this string turns up to be empty if checked on the website.

Your code is working when running from console application (btw look at comments you can do it more easily). Problem most likely is that local file system outside your web application is not accessible by web application. Try to read file from your web application folder.

To read file outside of your application folder look at this answer ASP.NET - Reading and writing to the file-system, outside the application The easiest way is to impersonate your AppPool with user account with appropriate rights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM