[英]Invalid parameter when retrieving image from DB
In the end I want to grab a OLE type image from an Access Database and put it into a picture box. 最后,我想从Access数据库中获取OLE类型的图像并将其放入图片框。 Working with Visual Studio 2012 in C# and MS Access 2010. My solution is an app non-web related. 在C#和MS Access 2010中使用Visual Studio2012。我的解决方案是与网络无关的应用程序。
So this is the query code. 这就是查询代码。 I'm constructing an object ( Equipamento
) with among others an System.Drawing.Image
attribute that is the focus of the issue. 我正在构建一个对象( Equipamento
),其中包含System.Drawing.Image
属性,这是问题的重点。
OleDbConnection l = OleDbConnectionDAO.createConnection();
Equipamento eq = new Equipamento();
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(
"SELECT * FROM [APP_Equipamento_Geral] WHERE COD_ETIQ like '%"
+ codigo
+ " %'",
l);
DataSet ds = new DataSet();
adapter.Fill(ds, "[APP_Equipamento_Geral]");
string s = ds.Tables["[APP_Equipamento_Geral]"].Columns[16].ColumnName;
foreach (DataRow row in ds.Tables["[APP_Equipamento_Geral]"].Rows)
{
eq.NInventario = row["Codigo"].ToString();
eq.Modelo = row["MODELO"].ToString();
eq.Marca = row["Marca_"].ToString();
eq.GamaMedida = row["Gama Medida"].ToString();
if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
{
byte[] b = new byte[0];
b = (byte[])row["FOTO"];
eq.Img = getImageFromBytes(b);//Error caught here
}
//if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
//{
// byte[] b = (byte[])row["FOTO"];
// MemoryStream ms = new MemoryStream(b);
// eq.Img = Image.FromStream(ms); //Error caught here
//}
}
}
And here is the auxiliary method: 这是辅助方法:
private Image getImageFromBytes(byte[] myByteArray)
{
System.IO.MemoryStream newImageStream
= new System.IO.MemoryStream(myByteArray, 0, myByteArray.Length);\
return Image.FromStream(newImageStream, true);
}
That last commented piece of code was another of my attempts that also gave the 最后一段被注释的代码是我的另一尝试,它也使
Invalid parameter 无效的参数
error. 错误。 Any solutions? 有什么办法吗?
Note: If I take out the image part everything works fine. 注意:如果我取出图像部分,一切正常。
An image stored as an OLE object
has a different format from a serialized System.Drawing.Image
. 存储为OLE object
的图像的格式与序列化的System.Drawing.Image
格式不同。 That's why I asked how the images were stored. 这就是为什么我问图像是如何存储的。
While I cannot vouch for this, never having used it personally, the following code is much recommended. 虽然我不能为此提供担保,但从来没有亲自使用过,但是强烈建议以下代码。 Supposedly, it uses the GDI+ lib from MS (included in Win standard installation) to import/export pics to/from Access OLE. 据说,它使用来自MS的GDI +库(包括在Win标准安装中)将图片导入到Access OLE或从Access OLE导出图片。
http://www.access-im-unternehmen.de/index1.php?BeitragID=337&id=300 http://www.access-im-unternehmen.de/index1.php?BeitragID=337&id=300
You can find other suggestions (including a utility to extract your images from Access) at this link: 您可以在此链接中找到其他建议(包括从Access提取图像的实用程序):
Converting MS Access "OLE Objects" back to plain JPEGs - best way? 将MS Access“ OLE对象”转换回纯JPEG-最佳方法?
You can't convert a image or string to arraybite only using the cast. 您不能仅使用强制转换将图像或字符串转换为arraybite。
If you want convert an image to arrayBite use this function 如果要将图像转换为arrayBite,请使用此功能
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
to reconvert arraybite to image use this 转换arraybite到图像使用此
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.