Covert byte data to Image - c#

I am beginner in .net.I am trying to convert from byte to image format.I am cosing error
My code is
MemoryStream ms = new MemoryStream(byt);
Image returnImage = Image.FromStream(ms);
returnImage.Save("D:\\image.jpg");
Occuring error in line " Image returnImage = Image.FromStream(ms);" as "Parameter is not valid."

Please try below Code
public static void byteArrayToImage(byte[] data)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
Image img=new Bitmap(Image.FromStream(ms));
img.Save(#"D:\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

Related

Value cannot be null. Parameter name: encoder in bitmap casting 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);
}

C# storing Byte[] and retrieving it from MySQL

I'm inserting the byte[] file into MySQL BLOB column, but I can not show the image when retrieving. I have one function to store the image as byte[]
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
Which I call this way:
Image img = Image.FromFile('trend.jpg');
#field = imageToByteArray(img);
In PhpMyAdmin it shows this way
On the other hand, when I try to retrieve the image into a picturebox, I receive an error.
byte[] bytes = (byte[])item["image"];
pictureBox1.Image = byteArrayToImage(bytes);
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Invalid character in a base-64

Error" Parameter is not valid " while converting Bytes into Image

I am converting bytes into an image but I get an error
Parameter is not valid
I am pasting my code. Kindly check the code and suggested that was I am doing right or wrong.
Image arr1 = byteArrayToImage(Bytess);
This is the function.
public static Image byteArrayToImage(byte[] byteArrayIn)
{
if (null == byteArrayIn || byteArrayIn.Length == 0)
return null;
MemoryStream ms = new MemoryStream(byteArrayIn);
try
{
Process currentProcess1 = Process.GetCurrentProcess();
Image returnImage = Image.FromStream(ms);
return returnImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I applied many techniques and solutions but it did not work for me
Your answer would be appreciated.
Thanks
try this
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);
return img;
}
After trying many things I found a way which has a little bit more control.
In this example you can specify the pixel format and copy the bytes to a Bitmap.
byte[] buffer = GetImageBytes();
var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length);
bitmap.UnlockBits(bitmap_data);
var result = bitmap as Image;
The problem is because, you are bringing it incorrectly from database. Try changing your code like this:
while (registry.Read())
{
byte[] image = (byte[])registry["Image"];
}
In my case I got the error since my base64 string had wrong encoding before calling Image.FromStream.
This worked for me in the end:
byte[] bytes = System.Convert.FromBase64String(base64ImageString);
using (MemoryStream ms = new MemoryStream(bytes))
{
var image = Image.FromStream(ms);
image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
cmd.CommandText="SELECT * FROM `form_backimg` WHERE ACTIVE=1";
MySqlDataReader reader6= cmd.ExecuteReader();
if(reader6.Read())
{
code4 = (byte[])reader6["BACK_IMG"]; //BLOB FIELD NAME BACK_IMG
}
reader6.Close();
MemoryStream stream = new MemoryStream(code4); //code4 is a public byte[] defined on top
pictureBox3.Image = Image.FromStream(stream);
try this,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}

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....

Converting byte to image and image to byte using C#

currently i am working with ASP.NET and C# to store image into MySQL (using blob datatype). I'm storing it successfully into database, but now problem is, How can I retrieve that byte[] to image format ?
FUNCTION: code to convert byte[] to image
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); --> here gives me error as `parameter is not valid`
return returnImage;
}
retieved as datatable...
if (dt1.Rows.Count > 0)
{
byteArrayToImage((byte[]) dt1.Rows[0]["PortfolioSlideImages"]);
//MemoryStream ms = new MemoryStream((byte[])dt1.Rows[0]["PortfolioSlideImages"]);
//Image returnImage = Image.FromStream(ms);
//return returnImage;
}
Try this function,
public Image byteArrayToImage(byte[] byteArrayIn)
{
Image returnImage = null;
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
returnImage = Image.FromStream(ms);
}
return returnImage;
}
for more info, visit this link

Categories