Images, SQL and C# -PLEASE HELP
-
Hi, Im developing a project which consists of a client which sends images to a server. The server must store these images in a database, and I am having a multitude of problems. I first tried creating a web-service which accepted the image as a parameter in a web method, but it would appear that System.Drawing.Image.Bitmap is not serializable - it generates an XML Serialization error. I then thought I would try and access the database directly and just store add images using an INSERT command, but I am getting an error message that the 'object' (I believe this refers to the image object) must implement IConvertable. Can anyone help me at all - I've been trying to sort this for the last week, and I really need to sort it. Thanks in advance Peter
-
Hi, Im developing a project which consists of a client which sends images to a server. The server must store these images in a database, and I am having a multitude of problems. I first tried creating a web-service which accepted the image as a parameter in a web method, but it would appear that System.Drawing.Image.Bitmap is not serializable - it generates an XML Serialization error. I then thought I would try and access the database directly and just store add images using an INSERT command, but I am getting an error message that the 'object' (I believe this refers to the image object) must implement IConvertable. Can anyone help me at all - I've been trying to sort this for the last week, and I really need to sort it. Thanks in advance Peter
Adding images to a database has been covered numerous times in the past. Please use the Search Comments link above and search for previous solutions, such as these found in a recent thread: http://www.codeproject.com/script/comments/forums.asp?msg=704229&forumid=1649&XtraIDs=1649&searchkw=database+image&sd=10%2F22%2F2003&ed=1%2F20%2F2004#xx704229xx[^]. As far as sending images (either
Image
orBitmap
, a derivative ofImage
, neither of which are serializable), most implementations use aMemoryStream
(or anotherStream
likeFileStream
, especially if the images were already saved to disk) to save the images to a buffer (abyte[]
array), which is serializable (sinceArray
is serializable). Either way, once your image gets deserialized on the server, you still have to insert it into the database. Depending on your implementation requirements, you might actually be better off storing the image on the file system and inserting a file reference (like the URL to access it remotely, which you can always usePage.MapPath
to get the file system path) into the database. This way, if you want to display the images in a web page, you don't have to extract them first or use a page to dynamically extract and stream them out using a different content-type than a normal page (ex: .aspx file). For a good example using SQL Server, see C# Photo Album Viewer[^]. If this is for use on a server, I highly recommend that you do not use Access. Use a real RDBMS that implements ACID properties like SQL Server or the MSDE (a connection-limited version of SQL Server). You can find more information about SQL Server at http://www.microsoft.com/sql/default.asp[^] or download the MSDE for free at -
Adding images to a database has been covered numerous times in the past. Please use the Search Comments link above and search for previous solutions, such as these found in a recent thread: http://www.codeproject.com/script/comments/forums.asp?msg=704229&forumid=1649&XtraIDs=1649&searchkw=database+image&sd=10%2F22%2F2003&ed=1%2F20%2F2004#xx704229xx[^]. As far as sending images (either
Image
orBitmap
, a derivative ofImage
, neither of which are serializable), most implementations use aMemoryStream
(or anotherStream
likeFileStream
, especially if the images were already saved to disk) to save the images to a buffer (abyte[]
array), which is serializable (sinceArray
is serializable). Either way, once your image gets deserialized on the server, you still have to insert it into the database. Depending on your implementation requirements, you might actually be better off storing the image on the file system and inserting a file reference (like the URL to access it remotely, which you can always usePage.MapPath
to get the file system path) into the database. This way, if you want to display the images in a web page, you don't have to extract them first or use a page to dynamically extract and stream them out using a different content-type than a normal page (ex: .aspx file). For a good example using SQL Server, see C# Photo Album Viewer[^]. If this is for use on a server, I highly recommend that you do not use Access. Use a real RDBMS that implements ACID properties like SQL Server or the MSDE (a connection-limited version of SQL Server). You can find more information about SQL Server at http://www.microsoft.com/sql/default.asp[^] or download the MSDE for free atThanks once again for your help Heath!! I did use the search facility for the message boards and general articles, but I was unable to find something which actually stored the image in the database, not just a referance. I will consider doing this though in my implementation. I am using MS SQL Server - not access, and I will carefully go through the mentioned articles. Thankyou once again - I will battle on! Peter
-
Hi, Im developing a project which consists of a client which sends images to a server. The server must store these images in a database, and I am having a multitude of problems. I first tried creating a web-service which accepted the image as a parameter in a web method, but it would appear that System.Drawing.Image.Bitmap is not serializable - it generates an XML Serialization error. I then thought I would try and access the database directly and just store add images using an INSERT command, but I am getting an error message that the 'object' (I believe this refers to the image object) must implement IConvertable. Can anyone help me at all - I've been trying to sort this for the last week, and I really need to sort it. Thanks in advance Peter
You can convert it to a byte[] before send to the server and then store it directly. public StorePhoto(byte[] photoContent) { ....... SqlParameter param1 = new SqlParameter("@photo",SqlDbType.Image); param1.Direction = ParameterDirection.Input; param1.Value = photoContent; ....... }