2009-05-25 39 views
2

我使用OpenFileDialog读取图像。示例代码如下:datatable中的图像

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

我想将此图像存储在数据表中。我怎样才能做到这一点?

+0

为相同的文章添加了一个链接,检查更新的答案。 – Kirtan 2009-05-25 05:44:32

+0

您可能需要为此问题添加一些与语言相关的标签。我会,但我没有足够的魔法点。 – Catchwa 2009-05-25 06:00:23

回答

4

你可以做到这样的 -

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. 

然后,添加一个新行到该表,并设置MyImage列的值。

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

编辑:你可以看看this CodeProject上的文章的帮助上的图像转换为字节数组。

2

我其实也在努力做到这一点。我的解决方案实际上并不涉及。

Drawing.Bitmap img = new Drawing.Bitmap("Path to image"); //Replace string with your OpenFileDialog path. 

DataColumn column = new DataColumn("ImageColumn"); 
column.DataType = System.Type.GetType("System.Drawing.Bitmap"); 

//Code to add data to a cell: 
DataRow row = new DataRow(); 
row("ImageColumn") = img; 

对我来说,这工作就像一个魅力。