c# - How to convert Bitmap to a Base64 string? -
i'm trying capture screen , convert base64 string. code:
rectangle bounds = screen.getbounds(point.empty); bitmap bitmap = new bitmap(bounds.width, bounds.height); using (graphics g = graphics.fromimage(bitmap)) { g.copyfromscreen(point.empty, point.empty, bounds.size); } // convert image byte[] system.io.memorystream stream = new system.io.memorystream(); bitmap.save(stream, system.drawing.imaging.imageformat.bmp); byte[] imagebytes = stream.toarray(); // write bytes (as string) textbox richtextbox1.text = system.text.encoding.utf8.getstring(imagebytes); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); using richtextbox debug, shows:
bm6�~
so reason bytes aren't correct causes base64string become null. idea i'm doing wrong? thanks.
the characters doing system.text.encoding.utf8.getstring(imagebytes) (almost certainly) contain unprintable characters. cause see few characters. if first convert base64-string, contain printable characters , can shown in text box:
// convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); // write bytes (as base64 string) textbox richtextbox1.text = base64string;
Comments
Post a Comment