problem while retriving image from database. [modified]
-
Hi, iam doing windows application c# my database is "Firebird Database"; iam able to insert an picture into database,but iam unable to retrive the same.. my code is : int id = int.Parse(textBox1.Text); FbConnection cnn = new FbConnection("ServerType=1;User=SYSDBA;Password=masterkey;Database=E:/database/test.fdb"); cnn.Open(); string query = "select photo from cust1 where id=" + id; FbDataAdapter adb = new FbDataAdapter(query, cnn); DataSet ds = new DataSet(); adb.Fill(ds, "cust1"); DataRowCollection rdsrows = ds.Tables[0].Rows; if (!rdsrows[0][0].Equals(DBNull.Value)) { byte[] content = (byte[])rdsrows[0][0]; MemoryStream stream = new MemoryStream(content); Bitmap img = new Bitmap(stream);-------------------"Error Here" pictureBox1.Image = img; } it's giving error "parameter is not valid" please inform where the problem is
murali krishna
modified on Monday, February 25, 2008 1:36 AM
-
Hi, iam doing windows application c# my database is "Firebird Database"; iam able to insert an picture into database,but iam unable to retrive the same.. my code is : int id = int.Parse(textBox1.Text); FbConnection cnn = new FbConnection("ServerType=1;User=SYSDBA;Password=masterkey;Database=E:/database/test.fdb"); cnn.Open(); string query = "select photo from cust1 where id=" + id; FbDataAdapter adb = new FbDataAdapter(query, cnn); DataSet ds = new DataSet(); adb.Fill(ds, "cust1"); DataRowCollection rdsrows = ds.Tables[0].Rows; if (!rdsrows[0][0].Equals(DBNull.Value)) { byte[] content = (byte[])rdsrows[0][0]; MemoryStream stream = new MemoryStream(content); Bitmap img = new Bitmap(stream);-------------------"Error Here" pictureBox1.Image = img; } it's giving error "parameter is not valid" please inform where the problem is
murali krishna
modified on Monday, February 25, 2008 1:36 AM
Have you looked in the debugger to see what content looks like ? Compared it with the bytes of an image you're passing in ?
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, iam doing windows application c# my database is "Firebird Database"; iam able to insert an picture into database,but iam unable to retrive the same.. my code is : int id = int.Parse(textBox1.Text); FbConnection cnn = new FbConnection("ServerType=1;User=SYSDBA;Password=masterkey;Database=E:/database/test.fdb"); cnn.Open(); string query = "select photo from cust1 where id=" + id; FbDataAdapter adb = new FbDataAdapter(query, cnn); DataSet ds = new DataSet(); adb.Fill(ds, "cust1"); DataRowCollection rdsrows = ds.Tables[0].Rows; if (!rdsrows[0][0].Equals(DBNull.Value)) { byte[] content = (byte[])rdsrows[0][0]; MemoryStream stream = new MemoryStream(content); Bitmap img = new Bitmap(stream);-------------------"Error Here" pictureBox1.Image = img; } it's giving error "parameter is not valid" please inform where the problem is
murali krishna
modified on Monday, February 25, 2008 1:36 AM
Try using Image instead of Bitmap. Are you sure that the photo field type is BLOB (binary)? I have no idea of what happens here, but I have some experience with Firebird in C# (sourceforge.net/projects/pokelib) and I wrote some functions for image conversion:
public static byte[] FileToBytes(string filePath) { FileStream fs; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); } catch (IOException) { return null; } // Create a byte array of file stream length byte[] fileData = new byte[fs.Length]; //Read block of bytes from stream into the byte array fs.Read(fileData, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); return fileData; } public static Image FileToImage(string imagePath) { FileStream fs; try { fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read); } catch (IOException) { return null; } // Create a byte array of file stream length byte[] imageData = new byte[fs.Length]; //Read block of bytes from stream into the byte array fs.Read(imageData, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); MemoryStream ms = new MemoryStream(imageData); return Image.FromStream(ms); } public static byte[] ImageToBytes(Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms, imageIn.RawFormat); return ms.ToArray(); } public static Image BytesToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; }
-
Have you looked in the debugger to see what content looks like ? Compared it with the bytes of an image you're passing in ?
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Hi, Thanks for your reply. while saving content with in bytes is showing Dimensions{77500} like this but while retriving content with in bytes showing Dimensions{13} only..
murali krishna
-
Hi, Thanks for your reply. while saving content with in bytes is showing Dimensions{77500} like this but while retriving content with in bytes showing Dimensions{13} only..
murali krishna
Well, gee, that seems like it could be a problem.... Sounds like your code to store the image is broken. I assume there's not more than 13 bytes in your database.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, iam doing windows application c# my database is "Firebird Database"; iam able to insert an picture into database,but iam unable to retrive the same.. my code is : int id = int.Parse(textBox1.Text); FbConnection cnn = new FbConnection("ServerType=1;User=SYSDBA;Password=masterkey;Database=E:/database/test.fdb"); cnn.Open(); string query = "select photo from cust1 where id=" + id; FbDataAdapter adb = new FbDataAdapter(query, cnn); DataSet ds = new DataSet(); adb.Fill(ds, "cust1"); DataRowCollection rdsrows = ds.Tables[0].Rows; if (!rdsrows[0][0].Equals(DBNull.Value)) { byte[] content = (byte[])rdsrows[0][0]; MemoryStream stream = new MemoryStream(content); Bitmap img = new Bitmap(stream);-------------------"Error Here" pictureBox1.Image = img; } it's giving error "parameter is not valid" please inform where the problem is
murali krishna
modified on Monday, February 25, 2008 1:36 AM
Try this: How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET[^]
#region signature my articles #endregion