C# 数据表中的图像
我使用OpenFileDialog读取图像。示例代码如下:C# 数据表中的图像,c#,image,datatable,C#,Image,Datatable,我使用OpenFileDialog读取图像。示例代码如下: openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != null) if (picBoardImage.Image != null) { picBoardImage.Image.Dispose(); } picBoardImage.Image = Image.FromFile(openFileDialog1.FileName); 我想将此图像存储在dat
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != null)
if (picBoardImage.Image != null)
{
picBoardImage.Image.Dispose();
}
picBoardImage.Image = Image.FromFile(openFileDialog1.FileName);
我想将此图像存储在datatable中。我该怎么做呢?你可以这样做-
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);
DataRow row=table.NewRow();
行[“MyImage”]=;
tables.Rows.Add(row);
编辑:您可以查看CodeProject文章,以获得有关将图像转换为字节数组的帮助。我实际上也在尝试实现这一点。我的解决方案实际上没有那么复杂
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;
对我来说,这就像一个魅力。要在datatable中拍摄图像……我需要在byte中拍摄图像……我如何才能做到?如何从openFileDialog1获取字节图像。本文将转换为GIF,这是一个相当糟糕的选择。除其他原因外,GIF仅支持256色。幸运的是,这只需要将链接代码中的Gif更改为Png。为相同的内容添加了文章链接,请检查更新的答案。您可能需要为此问题添加一些与语言相关的标记。我会的,但我没有足够的魔法点。@Shamim:如果科坦的答案是有效的,接受它,将问题标记为已回答,如果不提供mor信息或关闭问题。