hai to all...!!
iam working with Ms Access 2000.
iam saving an image in the database.
After that i want to retrive the same image from database and displaying it in the picture box.
but its not working. why it is happening?
please help me.
iam sending the code.
when button1 clicks it store in database.
when button2 clicks it should display in picturebox.
[code=vbnet]
private void button1_Click(o bject sender, EventArgs e)
{
if( DialogResult.OK == FileOpenDlg.Sho wDialog() )
{
System.IO.FileS tream stream = new System.IO.FileS tream(FileOpenD lg.FileName, System.IO.FileM ode.Open, System.IO.FileA ccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buf fer, 0, (int)stream.Len gth);
stream.Close();
string strName = System.IO.Path. GetFileNameWith outExtension(Fi leOpenDlg.FileN ame);
string connstring = "provider=micro soft.jet.oledb. 4.0;Data Source=D:\\srin u\\database\\db 1.mdb";
OleDbConnection conn = new OleDbConnection (connstring);
conn.Open();
string sql = "insert into FileInfo values(1,'name' ,'" + buffer + "')";
OleDbCommand cmd = new OleDbCommand(sq l, conn);
cmd.ExecuteNonQ uery();
conn.Close();
}
private void button2_Click(o bject sender, EventArgs e)
{
try
{
string connstring = "provider=micro soft.jet.oledb. 4.0;data source=D:\\srin u\\database\\db 1.mdb";
OleDbConnection conn = new OleDbConnection (connstring);
conn.Open();
string query = "Select FullName From FileInfo";
OleDbCommand cmd = new OleDbCommand(qu ery, conn);
byte[] b = (byte[])cmd.ExecuteSca lar();
MessageBox.Show (b.Length.ToStr ing());
if(b.Length > 0)
{
System.IO.Memor yStream stream = new System.IO.Memor yStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream)) ;
stream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
}
private void DrawToScale(Ima ge bmp)
{
Rectangle rc = pictureBox1.Cli entRectangle;
SizeF size = new SizeF( bmp.Width / bmp.HorizontalR esolution, bmp.Height / bmp.VerticalRes olution);
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);
size.Width *= fScale;
size.Height *= fScale;
pictureBox1.Ima ge = new Bitmap(bmp, size.ToSize());
pictureBox1.Siz eMode = PictureBoxSizeM ode.StretchImag e;
}[/code]
what mistake is iam doing?
i think it is correct method. but its not working.
please help me.
thank u very much in advance.....
iam working with Ms Access 2000.
iam saving an image in the database.
After that i want to retrive the same image from database and displaying it in the picture box.
but its not working. why it is happening?
please help me.
iam sending the code.
when button1 clicks it store in database.
when button2 clicks it should display in picturebox.
[code=vbnet]
private void button1_Click(o bject sender, EventArgs e)
{
if( DialogResult.OK == FileOpenDlg.Sho wDialog() )
{
System.IO.FileS tream stream = new System.IO.FileS tream(FileOpenD lg.FileName, System.IO.FileM ode.Open, System.IO.FileA ccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buf fer, 0, (int)stream.Len gth);
stream.Close();
string strName = System.IO.Path. GetFileNameWith outExtension(Fi leOpenDlg.FileN ame);
string connstring = "provider=micro soft.jet.oledb. 4.0;Data Source=D:\\srin u\\database\\db 1.mdb";
OleDbConnection conn = new OleDbConnection (connstring);
conn.Open();
string sql = "insert into FileInfo values(1,'name' ,'" + buffer + "')";
OleDbCommand cmd = new OleDbCommand(sq l, conn);
cmd.ExecuteNonQ uery();
conn.Close();
}
private void button2_Click(o bject sender, EventArgs e)
{
try
{
string connstring = "provider=micro soft.jet.oledb. 4.0;data source=D:\\srin u\\database\\db 1.mdb";
OleDbConnection conn = new OleDbConnection (connstring);
conn.Open();
string query = "Select FullName From FileInfo";
OleDbCommand cmd = new OleDbCommand(qu ery, conn);
byte[] b = (byte[])cmd.ExecuteSca lar();
MessageBox.Show (b.Length.ToStr ing());
if(b.Length > 0)
{
System.IO.Memor yStream stream = new System.IO.Memor yStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream)) ;
stream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
}
private void DrawToScale(Ima ge bmp)
{
Rectangle rc = pictureBox1.Cli entRectangle;
SizeF size = new SizeF( bmp.Width / bmp.HorizontalR esolution, bmp.Height / bmp.VerticalRes olution);
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);
size.Width *= fScale;
size.Height *= fScale;
pictureBox1.Ima ge = new Bitmap(bmp, size.ToSize());
pictureBox1.Siz eMode = PictureBoxSizeM ode.StretchImag e;
}[/code]
what mistake is iam doing?
i think it is correct method. but its not working.
please help me.
thank u very much in advance.....
Comment