Parameter is not valid while converting byte array to image - c#

i have a database with an image filed.I have read this Image field and convert to byte array with code below.
imageByte = ((dtgrid.CurrentRow.Cells[2].Value) as System.Data.Linq.Binary).ToArray();
When I Create a stream and Assign it to PictureBox image like this:
MemoryStream ms = new MemoryStream(imageByte);
pictureBox1.Image = Image.FromStream(ms);
I have this error:
Parameter is not valid.
How Can I fix this.

Related

Image byte array is not saving properly

I want to save an image that exist in picture box as a byte array to the database.I am new to programming so please help me to resolve this. Everytime I save my image byte array in the database, it always shows the same byte array. 0x53797374656D2E427974655B5D but this is what I see always in the database. No matter which image I save, it will always saves this code: 0x53797374656D2E427974655B5D. Please help me to resolve this issue.
This is my code
Byte[] imgBytes = null;
ImageConverter imgConverter = new ImageConverter();
imgBytes =
(System.Byte[])imgConverter.ConvertTo(PictureBox1.Image,Type.GetType("System.Byte[]"));
To save an image, use Image.Save
ImageConverter is not for converting image to binary.
var stream = new MemoryStream();
PictureBox1.Image.Save(stream, ImageFormat.Png);
byte[] data = stream.ToArray();

C# byte[] to bitmap System.InvalidOperationException

I'm doing a C# web service soap receiving an image.
I send a string contain the byte characters.
I transform the string in byte[] and next I world like to create the Bitmap.
The line Bitmap img = new Bitmap(ms); generate an exception : invalid argument.
I have a in the ms object this error : System.InvalidOperationException
value contain the correct string, imgBytes contain the good number of sell.
public string GetImage(string value)
{
byte[] imgBytes = Encoding.ASCII.GetBytes(value);
MemoryStream ms = new MemoryStream(imgBytes, true);
Bitmap img = new Bitmap(ms);
Code with debug mode
Exception
Thank you for your help.
It looks like your string holds base64 encoded data. Try to decode it to a byte array via Convert.FromBase64String
I had a similar problem. Basically you write into your memory stream (in the constructor) and the position pointer is at the end. So before reuse the memory stream you can try setting its position pointer to the beginning. Like this:
MemoryStream ms = new MemoryStream(imgBytes, true);
ms.Position = 0;
Bitmap img = new Bitmap(ms);
or the more general approach:
MemoryStream ms = new MemoryStream(imgBytes, true);
ms.Seek(0, SeekOrigin.Begin);
Bitmap img = new Bitmap(ms);
Hope this will solve your Problem.
Update
I think #heinbeinz answer is also important: First decode your string from the right encoding (normally base64), then set the position.

Convert Image from Byte Array to Image from MS Access using C# [duplicate]

This question already has answers here:
Byte array to image conversion
(14 answers)
Closed 7 years ago.
Are there any way to convert Image which is stored in MS Access to byte array using C#? Currently, I have stored my Image into my database which is in byte array format. Now I want to use OleDbReader to read through my image as a byte array, but I dont know how to it.
This is my Insert image into database code:
private byte[] ConvertToDBFormat(IImage InputImage)
{
Bitmap BmpImage = new Bitmap(InputImage.Bitmap);
MemoryStream MyStream = new MemoryStream();
BmpImage.Save(MyStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] ImageAsBytes = MyStream.ToArray();
return ImageAsBytes;
}
This is my code where I want to read from the database as byte
byte[] buffer = (byte[])reader["FaceImage"];
You can convert the byte[] to a Bitmap using MemoryStream
like this:
private Bitmap ConvertToBitmapFormat(byte[] buffer)
{
MemoryStream stream = new MemoryStream(buffer);
Bitmap image = new Bitmap(stream, false);
return image;
}

Why I got error parameter is not valid while retrieving image to picture box?

I have written this code for retrieving image from database to picturebox. I got these error
'Parameter is not valid'
at this line pictureBox1.Image = Image.FromStream(stm);
Byte[] imagebyte = new Byte[0];
imagebyte = (Byte[])(dr["Pic_Image"]);
MemoryStream stm = new MemoryStream(imagebyte);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromStream(stm);
would you please solve this error?
When you provide the stream , it expects one of the following files :
BMP
GIF
JPEG
PNG
TIFF
Make sure that your file is in one of these formats

C# - High Quality Byte Array Conversion of Images

I am converting images to byte array and storing in a text file using the following code. I am retrieving them successfully as well.
My concern is that the quality of the retrieved image is not up to the expectation. Is there a way to have better conversion to byte array and retrieving? I am not worried about the space conception.
Please share your thoughts.
string plaintextStoringLocation = #"D:\ImageSource\Cha5.txt";
string bmpSourceLocation = #"D:\ImageSource\Cha50.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
////Convert to Byte[]
byte[] clearByteArray = ImageToByteArray(sourceImg);
////Store it for future use (in plain text form)
StoreToLocation(clearByteArray, plaintextStoringLocation);
//Read from binary
byte[] retirevedImageBytes = ReadByteArrayFromFile(plaintextStoringLocation);
//Retrieve from Byte[]
Image destinationImg = ByteArrayToImage(retirevedImageBytes);
//Display Image
pictureBox1.Image = destinationImg;
EDIT: And the solution is - use Base64
//Plain Text Storing Location
string plaintextStoringLocation = #"D:\ImageSource\GirlInflower23.txt";
string bmpSourceLocation = #"D:\ImageSource\GirlInflower1.bmp";
////Read image
Image sourceImg = Image.FromFile(bmpSourceLocation);
string base64StringOfIMage = ImageToBase64(sourceImg, ImageFormat.Bmp);
byte[] byteOfString = Convert.FromBase64String(base64StringOfIMage);
StoreToLocation(byteOfString, plaintextStoringLocation);
byte[] retrievedBytesForStrimngForImage = ReadByteArrayFromFile(plaintextStoringLocation);
MemoryStream memStream = new MemoryStream(retrievedBytesForStrimngForImage);
//memStream.Read();
Image retrievedImg = Image.FromStream(memStream);
pictureBox1.Image = retrievedImg;
Yes, it is possible to get completely lossless storage. If you just store it in its original BMP format there will be no problem. I assume you are converting it to text because you want to send it via some protocol where binary characters will be corrupted.
Instead of whatever you are doing, you could consider using Convert.ToBase64String.
I haven't had any problems with this fragment...try it...if you get good results then the problem is in your Image -> byte[] or byte[] -> Image code :)
Image srcImage;
Image destImage;
// load an image
srcImage = Image.FromFile(filename);
// save the image via stream -> byte[]
using(MemoryStream stream = new MemoryStream()){
image.Save(stream, ImageFormat.xxx);
byte[] saveArray = stream.ToArray();
/*..... strore saveArray......*/
}
// rehydrate
byte[] loadArray = /*...get byte array from storage...*/
using(MemoryStream stream = new MemeoryStream(loadArray)){
destImage = Image.FromStream(stream);
}
pictureBox.Image = dstImage;
// don't forget...dispose of any Image/Stream objects

Categories