467,140 Members | 1,377 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,140 developers. It's quick & easy.

"Invalid Parameter Used" error when reading images from MemoryStre

I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading
images to db works fine for me. Reading from db to byte[] is also ok. But,
when I try to display them in my form controls, I get this error.
This is how it goes :

byte[] bImage1 = (byte[])datasetImageList.Tables["IMAGE"].Rows[0]["IMAGE1"];
System.IO.MemoryStream ms = new MemoryStream(bImage1, 0, bImage1.Length);
this.lblRunImage.Image = System.Drawing.Image.FromStream(ms); // Exception
occurs here.

I have tested this with .gif and .bmp and it does not work in either case.
Please help me...

Jul 21 '05 #1
  • viewed: 2323
7 Replies

Why are you so sure that the uploading is right,

On what way did you display them before?

(I ask this because I don't see direct strange things in your code for using

Jul 21 '05 #2
Thanks Cor for the reply.

May be your point is valid. This is how I upload the file...

string Type = "MYIMAGE";
MemoryStream fs = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, @"c:\test.gif");
byte[] objImage1 = fs.ToArray();

// database actions
CDBHandler.m_sqlTransaction = this.m_sqlConnection.BeginTransaction();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = this.m_sqlConnection;
sqlCmd.Transaction = this.m_sqlTransaction;
sqlCmd.CommandText = "insert into Image (Type,Image1) values (@Type,@Image1)";

SqlParameter sqlParam1 = new SqlParameter();
sqlParam1 = new SqlParameter("@Type", SqlDbType.VarChar);
sqlParam1.Value = strType;

SqlParameter sqlParam2 = new SqlParameter();
sqlParam2 = new SqlParameter("@Image1", SqlDbType.VarBinary);
sqlParam2.Value = objImage1;


This is working fine. But, I noticed one thing while debugging...the size
of the byte[] while reading from the DB is more than the size of the byte[]
uploaded to the DB.
NOTE: I've edited the actual code since it is quite long and I cannot post
it as it is a part of our project. So, if something is messy, please let me

Jul 21 '05 #3

I have made a sample based on your code for you, that was easier than check
your code.

And now it is ready, I see that probably your second parameter format is
wrong. :-)
sqlParam2 = new SqlParameter("@Image1", SqlDbType.Image);


However see my sample everything is in it and it is more compact than what
is now in your program.

(It is direct a picture viewer however has as well all extras that you need
except the writing to the database).

\\\needs a picturebox on a form
private void Form1_Load(object sender, System.EventArgs e)
OpenFileDialog fo = new OpenFileDialog();
if (fo.ShowDialog() == DialogResult.OK)
System.IO.FileStream fs = new System.IO.FileStream
(fo.FileName, System.IO.FileMode.Open);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] abyt = br.ReadBytes((int)fs.Length);
System.Data.DataTable dt = new System.Data.DataTable();
dt.Rows[0]["image"] = abyt;
Byte[] abyt2 = (byte[]) dt.Rows[0][0];
System.IO.MemoryStream ms2 = new System.IO.MemoryStream(abyt2);
pictureBox1.Image = Image.FromStream(ms2);

I hope this helps?

Jul 21 '05 #4
Thanks a lot Cor. I have tried your code and it just works perfect. But one
interesting thing is it does not work with .ico files.

Coming to my code, the reason why I have used SqlParameters is, I generate
the sql statements dynamically. I have used SqlDBType.VarBinary instead of
SqlDBType.Image because, the both support byte[] and max size supported by
VarBinary is more than Image.

Any way, I replaced my image uploading code with your code and
SqlDBType.VarBinary with SqlDBType.Image, but the error is occurring.
Jul 21 '05 #5

It is normal that it does not work with ico files, I am not an imaging guy,
however that seems to be comletly different and is not an image that you can
show in the pic box. Although you can convert it to a byte array of course
and back, what you can do with any file.

However from your message I don't understand if any image does not work in
your program or only the ico.

Jul 21 '05 #6
It works well with .gif and .bmp. The only problem is with .ico.

Now, I have another problem on hand. If I try to insert like this, I am
getting Stac

string strSqlStatement = "SELECT * FROM IMAGE"
+ " where TYPE like 'EC%'"
+ " and UPDATEUSER='" + objImage.UpdateUser + "'";

sqlDataAdapter = new SqlDataAdapter(strSqlStatement, sqlConnection); // ***
System.StackOverflowException is raised here ***
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
DataSet ds = new DataSet("IMAGE");
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlDataAdapter.Fill(ds, "IMAGE");

DataRow dr = ds.Tables["IMAGE"].NewRow();
// objImage is a class object that stores all the values for the table
dr["Type"] = objImage.Type;
dr["Number"] = objImage.Number;
dr["Image1"] = objImage.Image1;
dr["Image2"] = objImage.Image2;
dr["Image3"] = objImage.Image3;
dr["UpdateUser"] = objImage.UpdateUser;
dr["UpdateTime"] = objImage.UpdateTime;

int nRowCount = sqlDataAdapter.Update(ds, "IMAGE");

Jul 21 '05 #7

A stack overflow is mostly because an infinity loop.

However please do not add new questions to old, that makes it for people
using this newsgroup as a kind of reference impossible to use that.

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by hvaisane@cc.joensuu.fi | last post: by
4 posts views Thread by escristian@hotmail.com | last post: by
1 post views Thread by =?Utf-8?B?QUcgTW9yZW5v?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.