473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

"Invalid Parameter Used" error when reading images from MemoryStre

cnu
Hi
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 :

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

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

Jul 21 '05 #1
7 2621
CNU,

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

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

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

<code>
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;
sqlCmd.Parameters.Add(sqlParam1);

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

sqlCmd.ExecuteNonQuery();
this.m_sqlTransaction.Commit();
</code>

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

Thanks.
Jul 21 '05 #3
cnu,

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);

http://msdn.microsoft.com/library/de...classtopic.asp

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);
br.Close();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("image",Type.GetType("System.Byte[]"));
dt.Rows.Add(dt.NewRow());
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?

Cor
Jul 21 '05 #4
cnu
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
cnu,

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.

Cor
Jul 21 '05 #6
cnu
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

<code>
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;

ds.Tables["IMAGE"].Rows.Add(dr);
int nRowCount = sqlDataAdapter.Update(ds, "IMAGE");
</code>

Jul 21 '05 #7
Cnu,

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.

Cor
Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Wally | last post by:
I am trying to display images from an Access 2000 database and I get an error "Invalid Parameter Used" when I execute the code line "picBLOB.Image = Image.FromStream(stmBLOBData)" in my Visual...
4
by: sparks | last post by:
As you might know that "java.lang" package are automatically imported by the java compiler so that one don't need to write the import statement of that package in the source code. Are there any...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
2
by: Tommy Vercetti | last post by:
I am working on a Managed C++ project and I get the following error: TestThread.cpp(3) : error C2859: c:\projects\ProjectName\debug\vc70.pdb is not the pdb file that was used when this...
3
by: Arnold | last post by:
I am having problem loading the image from the database. It gives this error: "Invalid parameter used." This is my source code: Private abyt() As Byte Private fo As New OpenFileDialog Private sf...
7
by: cnu | last post by:
Hi 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...
4
by: escristian | last post by:
Hello. I'm trying to create an Image so I use something like this: Image newImage = Image.FromFile(filename); Now when it's a bmp file and certain .gif files it gives me an exception that...
3
by: Paul | last post by:
Hi All, In my application, I wished to check certain things on each page load, so rather than paste the same code in each pages constructor, I thought it would be more logical to inherit from...
1
by: =?Utf-8?B?QUcgTW9yZW5v?= | last post by:
I had a virus. Ran Norton Antivirus. Now when I start my computer I get "FTP Port is used" error message and no internet access, however, computer is connected to wireless home network. Any...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.