I'm trying to capture the screen and then convert it to a Base64 string. This is my 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 the image to byte[] System.IO.MemoryStream stream = new System.IO.MemoryStream(); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] imageBytes = stream.ToArray(); // Write the bytes (as a string) to the textbox richTextBox1.Text = System.Text.Encoding.UTF8.GetString(imageBytes); // Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes);
Using a richTextBox to debug, it shows:
BM6�~
So for some reason the bytes aren't correct which causes the base64String to become null. Any idea what I'm doing wrong? Thanks.
If you have a string base64 and need to convert it to a bitmap, this is the simplest and natural method. Code: String base64String = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAA..."; String base64Image = base64String.
Convert Images to Base64Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64. No ads, nonsense, or garbage. Drag and drop your image here!
To convert a string into a Base64 character the following steps should be followed: Get the ASCII value of each character in the string. Compute the 8-bit binary equivalent of the ASCII values. Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits.
How to convert a Base64 string into a Bitmap image in Android app? This example demonstrates how to do I in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
If you are looking for the reverse process, check Base64 to BMP. Choose the source of image from the “Datatype” field. Paste the URL or select a BMP image from your computer. If necessary, select the desired output format. Press the “Encode BMP to Base64” button. Download or copy the result from the “Base64” field.
final String encodedString = "data:image/jpg;base64, ...."; final String pureBase64Encoded = encodedString.substring (encodedString.indexOf (",") + 1); Now just simply use the line below to turn this into a Bitmap Object!
-Take a look at your Base64 encoded String, If it starts with The Base64.decode won't be able to decode it, So it has to be removed from your encoded String: final String encodedString = "data:image/jpg;base64, ...."; final String pureBase64Encoded = encodedString.substring (encodedString.indexOf (",") + 1);
I found a solution for my issue:
Bitmap bImage = newImage; // Your Bitmap Image System.IO.MemoryStream ms = new MemoryStream(); bImage.Save(ms, ImageFormat.Jpeg); byte[] byteImage = ms.ToArray(); var SigBase64= Convert.ToBase64String(byteImage); // Get Base64
The characters you get by doing System.Text.Encoding.UTF8.GetString(imageBytes)
will (almost certainly) contain unprintable characters. This could cause you to only see those few characters. If you first convert it to a base64-string, then it will contain only printable characters and can be shown in a text box:
// Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); // Write the bytes (as a Base64 string) to the textbox richTextBox1.Text = base64String;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With