473,241 Members | 1,494 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,241 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 2607
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.