Reading Image from SQL Error in C# - c#

I have a SQLite DB I have field called PersonalPic of Type BLOB
this is code in which I save image to SQLite
cm.CommandText = "insert into People(Name,FullName,FatherName,MotherName,NationalID,Mobile,Phone,Birth,Intma,Info,Address,CuAddress,PersonalPic,Pics) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+dateTimePicker1.Value.ToString("yyyy-MM-dd")+"','"+textBox8.Text+"','"+textBox9.Text+"','"+textBox10.Text+"','"+textBox11.Text+"','"+ConvertToString(personalpic)+"','"+ConvertToString(pic)+"')";
cm.ExecuteNonQuery();
public string ConvertToString(String path)
{
FileStream fs = new FileStream(path,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] rawData = new byte[fs.Length];
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(rawData);
return base64String;
}
It`s saved without no problems when I try to retrieve image it gives me Parameter is no valid Exception
SQLiteDataReader reader = cm.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox1.Text = reader["Name"].ToString();
textBox2.Text = reader["FullName"].ToString();
textBox3.Text = reader["FatherName"].ToString();
textBox4.Text = reader["MotherName"].ToString();
textBox5.Text = reader["NationalID"].ToString();
textBox6.Text = reader["Mobile"].ToString();
textBox7.Text = reader["Phone"].ToString();
dateTimePicker1.Value = DateTime.Parse(reader["Birth"].ToString());
textBox8.Text = reader["Intma"].ToString();
textBox9.Text = reader["Info"].ToString();
byte[] photoarray = (byte[])reader["PersonalPic"];
MemoryStream ms = new MemoryStream(photoarray);
MessageBox.Show(photoarray.Length.ToString());
ms.Position = 0;
pictureBox1.Image = Image.FromStream(ms);
}
}
reader.Close();
Exception is in the Line (pictureBox1.Image = Image.FromStream(ms))
Please Help!!

Aside from the horrendous sql inject insert statement, you're converting your picture byte array to a B64 string. And you're not converting it back on the other side. You're passing complete garbage data to your pictureBox. Either convert it back from byte[] -> B64 string and then decode it back to a byte[].. or just don't save it as a B64 string in the first place
Replace ConvertToString with ConvertToBlob
public byte[] ConvertToBlob(String path)
{
FileStream fs = new FileStream(path,
FileMode.OpenOrCreate, FileAccess.Read);
byte[] rawData = new byte[fs.Length];
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
return rawData;
}
And pass the raw data Byte[] directly to your SQL Query (which you need to rewrite as a parameterised query).

Related

SqlDataReader: Read varbinary into byte array

I have a SQL Server table with a varbinary(max) column. I use it to store images in it. The images are selected with an OpenFileDialog, translated into a byte[] like this
public byte[] ConvertImageToByteArray(String filepath)
{
try
{
return File.ReadAllBytes(filepath);
}
catch (Exception)
{
throw;
}
}
and then stored into the database using this line of code:
sqlCmd.Parameters.Add("#image", SqlDbType.VarBinary).Value = image;
It looks like this stored in the database, so I guess all seems to work like expected.
Unfortunately I am unable to load the images back from the datatable.
I am using a SqlDataReader to do so:
DbSql db = new DbSql();
SqlDataReader dr = db.GetDataReader(sqlCmd);
if (dr.Read())
{
if (!dr.IsDBNull(1))
productName = dr.GetString(1);
if (!dr.IsDBNull(2))
identNumber = dr.GetString(2);
[...]
if (!dr.IsDBNull(23))
comment = dr.GetString(23);
if (!dr.IsDBNull(24))
{
byte[] image = dr.GetSqlBytes(24).Value; // <- This is where I try to grab the image
}
}
It seems like I am not able to create a proper byte[] with
image = dr.GetSqlBytes(24).Value;
because my next step is not able to turn it into an image again:
public Image ConvertImageFromByteArray(byte[] array)
{
try
{
MemoryStream ms = new MemoryStream(array);
return Image.FromStream(ms);
}
catch (Exception) { throw; }
}
EDIT:
When trying something like
pictureBox.Image = ConvertImageFromByteArray(image)
I get an error saying "Invalid parameter" (self translated, saying "Ungültiger Parameter" in german)
Anyone able to offer a solution?
Once you cast your varbinary(MAX) to a byte array
image = (byte[])dr[24];
Try this..
MemoryStream ms = new MemoryStream(image);
imagePictureBox.Image = System.Drawing.Image.FromStream(ms);
This is how I create the byte array...
if (File.Exists(sLogoName) == false)
throw new Exception("File Not Found: " + sLogoName);
FileStream sourceStream = new FileStream(sLogoName, FileMode.Open, FileAccess.Read);
int streamLength = (int)sourceStream.Length;
Byte[] byLogo = new Byte[streamLength];
sourceStream.Read(byLogo, 0, streamLength);
sourceStream.Close();

Error with conversion codes from object to image [duplicate]

Why am I getting the exception "Parameter not valid" in my code:
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
The length of byteArrayIn is 169014. I am getting this exception despite the fact that no value in it is greater than 255.
I had the same problem and apparently is solved now, despite this and some other gdi+ exceptions are very misleading, I found that actually the problem was that the parameter being sent to a Bitmap constructor was not valid. I have this code:
using (System.IO.FileStream fs = new System.IO.FileStream(inputImage, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
{
try
{
using (Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false))
{
try
{
bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
GC.Collect();
}
catch (Exception ex)
{
throw ex;
}
}
}
catch (ArgumentException aex)
{
throw new Exception("The file received from the Map Server is not a valid jpeg image", aex);
}
}
The following line was causing an error:
Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)
The file stream was built from the file downloaded from the Map Server. My app was sending the request incorrectly to get the image, and the server was returning something with the jpg extension, but was actually a html telling me that an error ocurred. So I was taking that image and trying to build a Bitmap with it.
The fix was to control/ validate the image for a valid jpeg image.
Hope it helps!
My guess is that byteArrayIn doesn't contain valid image data.
Please give more information though:
Which line of code is throwing an exception?
What's the message?
Where did you get byteArrayIn from, and are you sure it should contain a valid image?
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
ImageConverter imageConverter = new System.Drawing.ImageConverter();
System.Drawing.Image image = imageConverter.ConvertFrom(fileData) as System.Drawing.Image;
image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
The "parameter is not valid" exception thrown by Image.FromStream() tells you that the stream is not a 'valid' or 'recognised' format. Watch the memory streams, especially if you are taking various offsets of bytes from a file.
// 1. Create a junk memory stream, pass it to Image.FromStream and
// get the "parameter is not valid":
MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02});
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);`
// 2. Create a junk memory stream, pass it to Image.FromStream
// without verification:
MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02});
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms, false, true);
Example 2 will work, note that useEmbeddedColorManagement must be false for validateImageData to be valid.
May be easiest to debug by dumping the memory stream to a file and inspecting the content.
Which line is throwing the exception? The new MemoryStream(...)? or the Image.FromStream(...)? And what is the byteArrayIn? Is it a byte[]? I only ask because of the comment "And none of value in it is not greater than 255" - which of course is automatic for a byte[].
As a more obvious question: does the binary actually contain an image in a sensible format?
For example, the following (although not great code) works fine:
byte[] data = File.ReadAllBytes(#"d:\extn.png"); // not a good idea...
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);
Console.WriteLine(img.Width);
Console.WriteLine(img.Height);
This error is caused by binary data being inserted into a buffer.
To solve this problem, you should insert one statement in your code.
This statement is:
obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length));
Example:
FileStream obj_FileStream = new FileStream(str_ImagePath, FileMode.OpenOrCreate, FileAccess.Read);
Byte[] Img = new Byte[obj_FileStream.Length];
obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length));
dt_NewsFeedByRow.Rows[0][6] = Img;
all the solutions given doesnt work.. dont concentrate only on the retrieving part. luk at the inserting of the image. i did the same mistake. I tuk an image from hard disk and saved it to database. The problem lies in the insert command. luk at my fault code..:
public bool convertImage()
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
}
catch
{
MessageBox.Show("image can not be converted");
return false;
}
}
public void insertImage()
{
// SqlConnection con = new SqlConnection();
try
{
cs.Close();
cs.Open();
da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = " +photo+" WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
da.UpdateCommand.ExecuteNonQuery();
cs.Close();
cs.Open();
int i = da.UpdateCommand.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successfully Inserted...");
}
}
catch
{
MessageBox.Show("Error in Connection");
}
cs.Close();
}
The above code shows succesfully inserted... but actualy its saving the image in the form of wrong datatype.. whereas the datatype must bt "image".. so i improved the code..
public bool convertImage()
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
}
catch
{
MessageBox.Show("image can not be converted");
return false;
}
}
public void insertImage()
{
// SqlConnection con = new SqlConnection();
try
{
cs.Close();
cs.Open();
//THIS WHERE THE CODE MUST BE CHANGED>>>>>>>>>>>>>>
da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = #img WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
da.UpdateCommand.Parameters.Add("#img", SqlDbType.Image);//CHANGED TO IMAGE DATATYPE...
da.UpdateCommand.Parameters["#img"].Value = photo;
da.UpdateCommand.ExecuteNonQuery();
cs.Close();
cs.Open();
int i = da.UpdateCommand.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successfully Inserted...");
}
}
catch
{
MessageBox.Show("Error in Connection");
}
cs.Close();
}
100% gurantee that there will be no PARAMETER NOT VALID error in retrieving....SOLVED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Most of the time when this happens it is bad data in the SQL column. This is the proper way to insert into an image column:
INSERT INTO [TableX] (ImgColumn) VALUES (
(SELECT * FROM OPENROWSET(BULK N'C:\....\Picture 010.png', SINGLE_BLOB) as tempimg))
Most people do it incorrectly this way:
INSERT INTO [TableX] (ImgColumn) VALUES ('C:\....\Picture 010.png'))
Just Follow this to Insert values into database
//Connection String
con.Open();
sqlQuery = "INSERT INTO [dbo].[Client] ([Client_ID],[Client_Name],[Phone],[Address],[Image]) VALUES('" + txtClientID.Text + "','" + txtClientName.Text + "','" + txtPhoneno.Text + "','" + txtaddress.Text + "',#image)";
cmd = new SqlCommand(sqlQuery, con);
cmd.Parameters.Add("#image", SqlDbType.Image);
cmd.Parameters["#image"].Value = img;
//img is a byte object
** /*MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();*/**
cmd.ExecuteNonQuery();
con.Close();

Browser not displaying base64 images

I want to display images on browser by converting them to base64 string.
Method of conversion is very simple as follows
string convertedfile = Convert.ToBase64String(fileData);
base64 string
<img src="data:image/jpeg;base64,DQoaCgAAAA1JSERSAAAAFAAAABQIBgAAAI2JHQ0AAAABc1JHQgCuzhzpAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAl2cEFnAAAAgAAAAIAAMOExmgAAAAZiS0dEAAAAAAAA+UO7fwAAACJ6VFh0U29mdHdhcmUAAHjaKy8v18vMyy5OTixI1csvSgcANtgGWBBTylwAAADzSURBVDhPY/wPBAxUBExUNAts1KiBlIfoaBhiD8N1NycwHLi/mqgAJhiGp15uYtj1aCbD5OMZDDdenSJsKCjr4QJvfzz977GZAS5t2Mvw//P3j/i0/EeoRlP299/v/667UaX//vv7X6MOpxawCThlw04z/H//8zmGa158fPDfsBq3oYzYSpusW8IMPgJVDEa8gQxff31k+P8PaPE/Bob/fxkYuNkEGfZeXskwZ1cnw6Hm9xhhimHgn/8/Gea9SGP485OR4fu37wx/fzIw/PkBwTA2818OIP8fQ0P4XAY2FjYUQ7G6kHBU4lZBMNmQavjgNxAArnYq4oN7ItkAAAAASUVORK5CYII=" alt="Original File"/>
For some reason my browser is not able to display it. Is there any MIME type I have to set in IIS? or what could be the problem?
Edit:
To write file data to SQL
Stream fs = imgUpload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//string base64String = Convert.ToBase64String(bytes);
//imgPicture.Src = "data:image/png;base64," + base64String;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString());
conn.Open();
using (SqlCommand cmd = new SqlCommand("update tbEHUsers set photo=#binaryValue where UserID=101", conn))
{
// Replace 8000, below, with the correct size of the field
cmd.Parameters.Add("#binaryValue", SqlDbType.VarBinary, -1).Value = bytes;
cmd.ExecuteNonQuery();
}
conn.Close();
My method to get image data from SQL(column datatype varbinary)
byte[] fileData =null;
using (SqlDataReader rdr = CMD.ExecuteReader(CommandBehavior.SequentialAccess))
{
if (rdr.Read())
{
// For some reason the data being returned is blank
// When I run it in SQL I get data being returned.
fileData = (byte[])rdr.GetValue(0);
using (System.IO.FileStream fs = new System.IO.FileStream("D:\\Testing.jpg", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
{
using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
{
bw.Write(fileData);
bw.Close();
}
}
}
rdr.Close();
}
The data that you have isn't a PNG image at all. Exchange it for a real PNG image, and it will work.
Example, a 10x10 red square:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAXSURBVChTY/jPwPCfGDyqEC+mtkKG/wCSw8c5Wcde4gAAAABJRU5ErkJggg==">
Here is your original image in base64 form:
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAUABQDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAQFBgf/xAAiEAABBAEDBQEAAAAAAAAAAAABAAIDBBEFEkETITFDUbH/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAgP/xAAZEQEBAQADAAAAAAAAAAAAAAAAAQIDERL/2gAMAwEAAhEDEQA/AO/oot+62jW6hALnO2MDjgFx+ngck/pwFUQaxFSuwwyaky7HaeGEtxuilPjDR6ye3JacZJBJbneXM15TdyXpoURFooREQEREH//Z">

Binary stream '0' does not contain a valid BinaryHeader error while Deserialization custom object

I faced this error while deserialization a custom object
I am trying to insert a collection of custom class into sql database & retrieve it the insertion going well but retrieving the data & deserialize give me this error
My code sample:
private void InsertObject()
{
ReceiptCollection items = SqlDataRepository.ReceiptProvider.GetAll();
string connectionString = "my connection";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
string sql = "INSERT INTO [dbo].[LogHeader]([MasterObject]) VALUES (#MasterObject)";
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, items);
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Write(bytes, 0, bytes.Length);
connection.Open();
cmd.Parameters.AddWithValue("#MasterObject", bytes);
cmd.ExecuteNonQuery();
}
}
private void RetrieveObjects()
{
string connectionString = "my connection";
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
string sql = "Select MasterObject From [dbo].[LogHeader] WHERE LogHeaderID=2";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
{
connection.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream(bytes);
memoryStream.Position = 0;
ReceiptCollection items = (ReceiptCollection)binaryFormatter.Deserialize(memoryStream); // the error happened here
}
}
I was facing the same problem in the serialization and deserialization of a custom class. Everywhere I looked around, they have the same code marked as the solution (as your code presented on the top) but I couldn't make it run correctly. All I was recieving after the memoryStream.Write() method, was an array of zeroes. I changed my code and got it to work.
What I did was (implemented in your code):
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, items);
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection))
{
byte[] bytes = new byte[memoryStream.Capacity];
bytes = memoryStream.GetBuffer();
connection.Open();
cmd.Parameters.AddWithValue("#MasterObject", bytes);
cmd.ExecuteNonQuery();
}
This is for sending a byte array to the data base. For retrieving it, I did the following:
connection.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
memoryStream.Position = 0;
ReceiptCollection items = (ReceiptCollection)binaryFormatter.Deserialize(memoryStream);
}
Try it! It really worked for me.
Start by checking what actually got stored in that column, and also check if the column's type is varbinary or similar. The error suggests that the serialized object's data stream got badly corrupted or truncated. If the row/column does not contain a long "hexstring", then there's a write problem with inserting/updating and search further there.

How to convert Binary to Byte and write as a file in c#

I am trying to read binary from database and write as a file in local disk using c#.
using the below code...
But there is problem in this line : byte[] fileAsByte = byte.Parse(row["Blob"]);
public static void ReadBlob()
{
int icount = 0;
string FileName;
SqlConnection Mycon = new SqlConnection(Con);
Mycon.Open();
string queryString = "select * from " + TblName;
SqlDataAdapter adapter = new SqlDataAdapter(queryString, Mycon);
DataTable dtBlob = new DataTable();
adapter.Fill(dtBlob);
foreach (DataRow row in dtBlob.Rows)
{
byte[] fileAsByte = byte.Parse(row["Blob"]);
FileName = FilePath + TblName + row["BlobId"].ToString() + FileType;
WriteBlob(fileAsByte, FileName);
}
Mycon.Close();
}
public static void WriteBlob(byte[] buff, string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff);
bw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
byte.Parse will try to parse a single byte. Have you tried just casting?
byte[] fileAsByte = (byte[]) row["Blob"];
If that fails, it should at least show you what type is actually in the DataRow. Hopefully it's some type which is reasonably easily convertible to byte[].
If your column is of type varbinary(max), you can use GetSqlBytes or GetSqlBinary on the SqlDataReader. If your column is of type varchar(max) or nvarchar(max), use GetSqlChars on SqlDataReader. You can as well use, GetBytes {this takes a size of the array buffer} or GetSqlBytes.
Also, as suggested above, for varbinary(MAX) the following line as well should work
byte[] binaryData = (byte[])row["Blob"];
Hope this helps.

Categories