Value cannot be null. Parameter name: encoder in bitmap casting c# - c#

In order to convert bitmap to base64 i have to convert my bitmap to Image
I get this message when i'm converting an image to memorystream as img.Save(ms, img.RawFormat); after casting my image from a screenshoot bitmap like Image img = (Image)bitmap; or Image img = bitmap as Image, but it's working fine when i use local stored image like Image img = Image.FromFile(Path).
how can i avoid this error while i don't want to store the screenshoot and read it again each time
this is the code i have tried
Image img = bitmap as Image;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, img.RawFormat);
string base64 = Convert.ToBase64String(ms.ToArray());
}

Actually, you don't have to convert Bitmab to Image while you can encode your Bitmab with Base64 directly
try this:
using (MemoryStream ms = new MemoryStream())
{
bitmab.Save(ms, ImageFormat.Jpeg); // you can change your image format as you want
byte[] imageBytes = ms.ToArray();
string base64 = Convert.ToBase64String(imageBytes);
}

Related

File Compression Type changes when converting image to base64

I have and image with a compression type of CCITT T.6 which is converted to base64 and sent to a backend API, where base64 string will be converted back to the original image and validate the file details. My problem is whenI convert the base64 string back to its original image, the compression type for the image has now changed to LZW. Does converting an image to a base64 string change its compression type? If so how can I keep the files original compression type.
string img = "";
using (Image image = Image.FromFile(filepath))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
img = base64String;
}
}
LoadImage(img);
public void LoadImage(string base64image)
{
byte[] bytes = Convert.FromBase64String(base64image);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
File.WriteAllBytes(filepath,bytes);
}
Getting rid of the memory stream and using Convert.ToBase64String(File.ReadAllBytes(filepath)) suggested by Steeeve seems to have solved the problem. Image Compression types are now consistent after regenerating the image from a base64 string.

How can I convert high resolution JPG to byte array?

I have a 63572 x 63573 High resolution JPG image.
I want to convert the image to byte array and convert byte array to OpenCvsharp.Mat.
the image is grayscale.
I tried to way that below code. However, It occurs 'System.OverflowException'(System.Drawing.dll).
System.Drawing.Image image = System.Drawing.Image.FromFile(filepath);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var data = ms.ToArray();
So, I tried to way that below code. However,
It didn't run properly.
var data = File.ReadAllBytes(filepath);
using (Mat mat = new Mat(height, width, MatType.CV_8UC1, data))
{
...
}

How can i get an imagefrom the web with headers?

How can i get in c# an image as bitmapimage from an url and pass to it some headers? (not parameters, headers)
And also i found a simple solution but i couldn't understand why it was wrong, the problem is that:
WHen i get byte[] from the server i get the image with a lot of question marks, this appends i thinks because of a different text encoding, how can i fix the code so i can get successfully the image from c# with headers and visible to bitmap?
You have to convert your image into a base64 encoded string and pass it to the headers.
using (Image image = Image.FromFile(Path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
Then on the other side you can parse it and convert it back to an image:
public Image LoadImage()
{
//data:image/png;base64,
byte[] bytes = Convert.FromBase64String(YOUR_BASE64_ENCODED_STRING);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}

convert binary to bitmap using memory stream

Hi I wanna convert binary array to bitmap and show image in a picturebox. I wrote the following code but I got exception that says that the parameter is not valid .
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream);
mStream.Dispose();
return bm;
}
It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.
It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.
public static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
Don't dispose of the MemoryStream. It now belongs to the image object and will be disposed when you dispose the image.
Also consider doing it like this
var ms = new MemoryStream(blob);
var img = Image.FromStream(ms);
.....
img.Dispose(); //once you are done with the image.
System.IO.MemoryStream mStrm = new System.IO.MemoryStream(your byte array);
Image im = Image.FromStream(mStrm);
im.Save("image.bmp");
Try this. If you still get any error or exception; please post your bytes which you are trying to convert to image. There should be problem in your image stream....

How to convert Bitmap to a Base64 string?

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.
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;
No need for byte[] ...just convert the stream directly (w/using constructs)
using (var ms = new MemoryStream())
{
using (var bitmap = new Bitmap(newImage))
{
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var SigBase64= Convert.ToBase64String(ms.GetBuffer()); //Get Base64
}
}

Categories