简体   繁体   English

从Visual Studio C#中的MS Access数据库中获取OLE(位图)对象形式,我的代码有什么问题?

[英]Taking OLE (bitmap) object form MS Access database in Visual studio C# , What's wrong in my code?

I am trying to fetch an image from MS Access DB. 我正在尝试从MS Access DB获取图像。 Data is fetched correctly but when I'm trying to display some error is showing. 数据已正确提取,但是当我尝试显示一些错误时显示。 My code for display the image is, 我用于显示图像的代码是

            ...
            byte[] photoBytes = (byte[])res[11];
            var ms = new System.IO.MemoryStream(photoBytes);
            image.Image = new System.Drawing.Bitmap(ms);
            ...

Error : Additional information: Parameter is not valid. 错误: 附加信息:参数无效。 在此处输入图片说明

Can anyone tell me where is the error, or probability of error? 谁能告诉我错误在哪里,或者错误的概率在哪里?

my function is 我的功能是

public OleDbDataReader studentInfo(String adm_no)
    {
        OleDbConnection con = new OleDbConnection(ConnStr);
        con.Open();
        OleDbCommand command = new OleDbCommand("SELECT * FROM student_info WHERE adm_no = '"+adm_no+"'", con);
        OleDbDataReader res = command.ExecuteReader();
        return res;
    }

As far as I can remember OLE's are beasts. 据我所记得,OLE是野兽。 If you know for sure what the datatype is you have some chances if you check the binary structure of it. 如果您确定知道数据类型是什么,则可以检查一下其二进制结构。 OLE is a container so it's IMO never just the pure content. OLE是一个容器,因此IMO绝不仅仅是纯内容。

I don't have the code anymore but I remember fishing in the hexdumps of the OLE's of different types (Excel, Word, Textfiles, Images, ...) and ending up with a success rate of maybe 80%. 我没有代码了,但是我记得在不同类型的OLE(Excel,Word,Textfiles,Images ...)的十六进制转储中钓鱼,最终成功率高达80%。 If that was because of the types we decided to support or my very restricted knowledge on the inner structure of OLE's I can't tell anymore. 如果是因为我们决定支持的类​​型,或者由于我对OLE的内部结构的了解非常有限,我将无法再说了。

My recommendation for debugging would be to make absolutely sure you have the raw bitmap data before dealing with the binary data at all: 我的调试建议是,在处理二进制数据之前,绝对要确保您具有原始位图数据:

My approach was to create a small object (in this case a bitmap), store it in the DB, get the BLOB of it and lookup the known pattern in there. 我的方法是创建一个小对象(在本例中为位图),将其存储在DB中,获取它的BLOB并在其中查找已知模式。 I remember I found some structures - like the byte size of the searches object - by reverse- engineering and a somewhat stable offset towards the start of the data. 我记得我通过逆向工程发现了一些结构(例如搜索对象的字节大小),并且在数据开始时有一定的偏移量。

If you, however, happen to know - or even better - have the exact structure and implementation of an OLE- object and are able to deal with it, I am absolutely confident you'll manage to also store it and open it as a bitmap. 但是,如果您碰巧知道-甚至更好- 具有 OLE对象的确切结构和实现并能够处理它,我绝对有信心您还将设法将其存储并以位图的形式打开。

Good luck! 祝好运!

Take a look here for an example of what you are trying to do, although that example is for a JPEG not a Bitmap. 此处查看您要尝试执行的操作的示例,尽管该示例是针对JPEG而非位图的。 Since you have a byte[] , you will need to do something like this for conversion: 由于您具有byte[] ,因此需要执行以下操作进行转换:

using (MemoryStream ms = new MemoryStream(photoBytes))
{
  Bitmap img = (Bitmap)Image.FromStream(ms);
}

There's something wrong with your byte stream. 您的字节流有问题。 Normally I'd say to check whether the MemoryStream 's .Position is set to 0 before passing it into the Bitmap constructor - if it's at the end of the stream, it's possible that you're effectively passing an empty stream - but that shouldn't be the case here. 通常,我会说在将MemoryStream.Position传递给Bitmap构造函数之前检查是否将其设置为0-如果它位于流的末尾,则可能是有效地传递了一个空流-但这不应该这里不是这样。

There are a few places in the constructor where an argument exception would get thrown, but InvaildParameter should be related to something being wrong with the byte stream that you're retrieving. 构造函数中有一些地方会引发参数异常,但是InvaildParameter应该与所获取的字节流有问题有关。 See here: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Bitmap.cs,cbbb65af7f6fafdb,references 参见此处: https : //referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Bitmap.cs,cbbb65af7f6fafdb,参考

And here: https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,4edcade52d698713 此处: https//referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,4edcade52d698713

You should validate that your byte stream is a suitable format for GDI+ to be able to load as an image - try writing the bytes out to a file for example and opening it in Paint. 您应该验证您的字节流是否适合GDI +格式,以便能够作为图像加载-例如,尝试将字节写出到文件中并在Paint中打开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Visual Studio C#代码访问MS Access数据库中的数据 - Accessing data within MS Access database from visual studio C# Code 我的C#代码出了什么问题? - What's wrong with my C# code? 我的C#代码有什么问题 - What's wrong with my C# code 在 Visual C# 中修复我的 MS Access 数据库时出错 - Error fixing my MS Access Database in Visual C# 连接到 MS Access 数据库 2000 - 2003 格式的 Visual Studio C# windows 窗体中的登录功能。 研究并尝试修复无济于事 - login function in a visual studio C# windows form connected to a MS Access database 2000 - 2003 format. Researched and tried fixing to no avail 无法在C#代码中访问我的UI控件。 怎么了? - Cannot access my UI controls in C# code. What's wrong? Visual Studio 2015 中无法识别的数据库格式,在 C# 中使用 MS Access 数据库 - Unrecognised database format in Visual Studio 2015 with MS Access Database in for C# 这有什么不对? 在我的代码中(将图像插入数据库 - C#) - What is wrong here? In my code (Inserting Image into Database - C#) 如何将位图二维码传递给ole对象(图片)水晶报表c# - How to Pass bitmap QR code to ole object (picture) crystal report c# 我的WPF绑定(用C#代码)有什么问题? - What's wrong with my WPF Binding (in C# code)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM