Add image to database
-
Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay
-
Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay
SQL database can accept the Binary type, which corresponds to an C# byte[]. So convert your bitmap into an array of bytes when you want to store it in the database. And when you're ready to read it from the database, construct one from the read in bytes (you should be able to use something like Bitmap.FromStream). The graveyards are filled with indispensible men.
-
Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay
-
Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay
This has been covered many times in this forum. Please click "Search comments" above. One specific thread not so long ago can be found here[^].
Microsoft MVP, Visual C# My Articles
-
Hi How can insert a image or read it and show in ado.net? i want to store image (for exmaple .jpg or bmp) in database and show them in form(with which controls??) Regards' Amirjalay
1. Create a binary column in your database large enough to store the picture (image is a good choice). 2. Stream the target picture (System.Drawing.Image instance or whatever) to a byte array (I generally use the System.IO.MemoryStream class and the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to create the byte stream for writing to the DB) Example Image Encode: =====================
Image myImage = Image.FromFile(filePath); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, myImage); byte[] imageBytes = stream.GetBuffer(); //Insert code to update the database column from step 1 with the //contents of imageBytes
3. To retrieve the target image, load the row containing it, and get the byte array from the column containing the image data, then use the System.Drawing.Image.FromStream static method to instantiate a new Image instance. hope this helps... No design ever survives first contact with its users.