views:

2112

answers:

2
+1  Q: 

Image in datatable

I read image by using OpenFileDialog. Sample code is below:

openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != null)
   if (picBoardImage.Image != null)
{
    picBoardImage.Image.Dispose();
}
picBoardImage.Image = Image.FromFile(openFileDialog1.FileName);

I want to store this image in datatable. How can I do that?

+2  A: 

You can do it like this -

DataTable table = new DataTable("ImageTable"); //Create a new DataTable instance.

DataColumn column = new DataColumn("MyImage"); //Create the column.
column.DataType = System.Type.GetType("System.Byte[]"); //Type byte[] to store image bytes.
column.AllowDBNull = true;
column.Caption = "My Image";

table.Columns.Add(column); //Add the column to the table.

Then, add a new row to this table and set the value of the MyImage column.

DataRow row = table.NewRow();
row["MyImage"] = <Image byte array>;
tables.Rows.Add(row);

EDIT: You can take a look at this CodeProject article for help on converting an image to a byte array.

Kirtan
to take image in datatable .....i need to take image in byte ...how can i do it? how can i take image in byte from openFileDialog1.
Shamim
The article converts to a GIF, which is a fairly poor choice. Among other reasons, GIFs only support 256 colors. Luckily, this only requires changing Gif to Png in the linked code.
Matthew Flaschen
Yes Matthew, the format can be any of your choice.
Kirtan
+1  A: 

I think you can use this link http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=799 this explains everything you want.

Pranali Desai