473,487 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"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 2627
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
1966
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
1457
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
22741
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
8447
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
2570
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
418
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
3030
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
34437
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
3339
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
7105
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
6967
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
7132
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
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.