C# 如何在数据表中添加图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15380187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i add image in a datatable?
提问by Kavitha
How can i add image in a datatable ? I tried the following code,
如何在数据表中添加图像?我尝试了以下代码,
Image img = new Image();
img.ImageUrl = "~/images/xx.png";
dr = dt.NewRow();
dr[column] = imgdw;
But it showing text System.Web.UI.WebControls.Image
in gridview instead of image.
但它System.Web.UI.WebControls.Image
在 gridview 中显示文本而不是图像。
回答by Max
Use this code:
使用此代码:
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.
Add new row to table:
向表中添加新行:
DataRow row = table.NewRow();
row["MyImage"] = <Image byte array>;
tables.Rows.Add(row);
Check out the following Code project link(Image to byte[]):
查看以下代码项目链接(Image to byte[]):
回答by Manish Mishra
try this code:
试试这个代码:
DataTable dt = new DataTable();
dt.Columns.Add("col1", typeof(byte[]));
Image img = Image.FromFile(@"physical path to the file");
DataRow dr = dt.NewRow();
dr["col1"] = imageToByteArray(img);
dt.Rows.Add(dr);
where imageToByteArray
is
这里imageToByteArray
是
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
so Idea is that don't try to store the Image directly, rather convert it to byte [] and then store it, so that, later you can refetch it and use it or assign it to a picture box like this:
所以想法是不要尝试直接存储Image,而是将其转换为byte []然后存储它,以便以后您可以重新获取它并使用它或将其分配给这样的图片框:
pictureBox1.Image = byteArrayToImage((byte[])dt.Rows[0]["col1"]);
where byteArrayToImage
is:
在哪里byteArrayToImage
:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
回答by Tim B James
If the purpose is to display an image within a GridView, then personally I wouldn't store the actual image within a DataTable, only the image path. Storing the Image would only bloat the DataTable unnecessarily. Obviously this is only if your image is stored on the FileSystem and not in a DataBase.
如果目的是在 GridView 中显示图像,那么我个人不会将实际图像存储在 DataTable 中,只存储图像路径。存储图像只会不必要地膨胀数据表。显然,这仅适用于您的图像存储在文件系统上而不是数据库中的情况。
To display the image within the GridView use a TemplateField
要在 GridView 中显示图像,请使用TemplateField
e.g.
例如
dr = dt.NewRow();
dr[column] = "~/images/xx.png";
<asp:TemplateField>
<ItemTemplate>
<img src='<%#Eval("NameOfColumn")%>' />
</ItemTemplate>
</asp:TemplateField>
This would also work well when you are storing the Image path in a Database rather than storing the raw image.
当您将图像路径存储在数据库中而不是存储原始图像时,这也很有效。