picturebox and access DB help

  • Thread starter Thread starter Paolol
  • Start date Start date
P

Paolol

Hi ,
I'm trying to show a bitmap I pasted on an Access DB, but when I try to
convert the memorystream to bitmap I get an error, can any one help me
out ??
Thanks a lot.
Paolo L.
 
Paolol, we really need to know what the error is to be able to help. How
about posting the complete text and the code that caused it.
 
Thanks Greg, this is the code I got some of this from a project I
founded in Internet, when I use the DrawToScale function or I try to set
the Image ( both are commented out ) I get the same error " invalid
argument " ????
I was not able to find any wrong argument in the code, but I'm new to C#
so please help me out :))
Thanks,
Paolo L.
---------------------
private void dataGrid1_Click(object sender, System.EventArgs e)
{
try
{
iIDListview =
System.Convert.ToInt32(this.dataGrid1[dataGrid1.CurrentRowIndex,0].ToString());
string sString = this.dataGrid1[dataGrid1.CurrentRowIndex,1].ToString();
//this.oleDbDataAdapter2.Fill(this.dsImmage11);
this.oleDbConnection1.Open();
this.oleDbDataAdapter2.SelectCommand.Parameters[0].Value=sString.Trim();
byte[] bB = (byte[])
this.oleDbDataAdapter2.SelectCommand.ExecuteScalar();
if (bB.Length > 0 )
{
System.IO.MemoryStream Stream1 = new System.IO.MemoryStream(bB,true);
Stream1.Write(bB,78,bB.Length-78 );
//
//pictureBox1.Image = Image.FromStream(Stream1,true);
//DrawToScale(Image.FromStream(Stream1,false));

//
Stream1.Close();
}//if*/
// caricamento dettagli
this.oleDbDataAdapter3.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter3.Fill(this.dsDetail1);
this.oleDbDataAdapter4.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter4.Fill(this.dsDetLoc1);
this.oleDbDataAdapter5.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter5.Fill(this.dsDetMove1);
//
}
catch (Exception eE)
{
MessageBox.Show(eE.Message);
}
finally
{
this.oleDbConnection1.Close();
}
}
private void DrawToScale(Image bmp)
{
// The client rectangle
Rectangle rc = pictureBox1.ClientRectangle;

// From Programming Windows with C#, by Charles Petzold
// Figure out the scaling necessary for the image
SizeF size = new SizeF( bmp.Width / bmp.HorizontalResolution,
bmp.Height / bmp.VerticalResolution);
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);

size.Width *= fScale;
size.Height *= fScale;

// Create a new bitmap of the proper size for the existing bitmap
// and assign it to the picture box
pictureBox1.Image = new Bitmap(bmp, size.ToSize());
}
 
Paolol, first thing I would try is resetting Stream1 back to the beginning.
After calling Write on a stream the current position is set to the end of
the bytes written. When you pass that stream in to the Image methods then
it is not valid information.

Hope that helps.

--
Greg Ewing [MVP]
http://www.citidc.com

Paolol said:
Thanks Greg, this is the code I got some of this from a project I
founded in Internet, when I use the DrawToScale function or I try to set
the Image ( both are commented out ) I get the same error " invalid
argument " ????
I was not able to find any wrong argument in the code, but I'm new to C#
so please help me out :))
Thanks,
Paolo L.
---------------------
private void dataGrid1_Click(object sender, System.EventArgs e)
{
try
{
iIDListview =
System.Convert.ToInt32(this.dataGrid1[dataGrid1.CurrentRowIndex,0].ToString(
));
string sString = this.dataGrid1[dataGrid1.CurrentRowIndex,1].ToString();
//this.oleDbDataAdapter2.Fill(this.dsImmage11);
this.oleDbConnection1.Open();
this.oleDbDataAdapter2.SelectCommand.Parameters[0].Value=sString.Trim();
byte[] bB = (byte[])
this.oleDbDataAdapter2.SelectCommand.ExecuteScalar();
if (bB.Length > 0 )
{
System.IO.MemoryStream Stream1 = new System.IO.MemoryStream(bB,true);
Stream1.Write(bB,78,bB.Length-78 );
//
//pictureBox1.Image = Image.FromStream(Stream1,true);
//DrawToScale(Image.FromStream(Stream1,false));

//
Stream1.Close();
}//if*/
// caricamento dettagli
this.oleDbDataAdapter3.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter3.Fill(this.dsDetail1);
this.oleDbDataAdapter4.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter4.Fill(this.dsDetLoc1);
this.oleDbDataAdapter5.SelectCommand.Parameters[0].Value= iIDListview;
this.oleDbDataAdapter5.Fill(this.dsDetMove1);
//
}
catch (Exception eE)
{
MessageBox.Show(eE.Message);
}
finally
{
this.oleDbConnection1.Close();
}
}
private void DrawToScale(Image bmp)
{
// The client rectangle
Rectangle rc = pictureBox1.ClientRectangle;

// From Programming Windows with C#, by Charles Petzold
// Figure out the scaling necessary for the image
SizeF size = new SizeF( bmp.Width / bmp.HorizontalResolution,
bmp.Height / bmp.VerticalResolution);
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);

size.Width *= fScale;
size.Height *= fScale;

// Create a new bitmap of the proper size for the existing bitmap
// and assign it to the picture box
pictureBox1.Image = new Bitmap(bmp, size.ToSize());
}

---------------------
Paolol, we really need to know what the error is to be able to help. How
about posting the complete text and the code that caused it.
 
Hi Greg,
thanks for the help, at last I found the error, it was in the immage i
stored in Access, I deleted the old one and drop a new one from "Paint"
( cut/paste ) and now it works.
Did you have nay idea why this happen, in the Access Form I use to enter
the image all was showing OK ??
Again Thanks.
Paolo L.
 
Back
Top