gpt4 book ai didi

c# - 缺少 AxHost.GetPictureFromIPicture() 方法,正在从 MS Access 数据库中检索图片(附件)

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:04 24 4
gpt4 key购买 nike

我正在尝试使用 AxHost.GetPictureFromIPicture() 从 MS Access 2013 数据库文件 (*.accdb) 获取 GIF 图像(保存为附件类型)- 将其转换为图像以便我可以将其显示在 PictureBox 中.但是没有方法! :( 我错过了什么吗?我需要设置或安装 smtg 吗?

如果不进行转换,我会收到此错误:“无法将‘System.__ComObject’类型的 COM 对象转换为类类型‘System.Drawing.Image’”

我真的以正确的方式做整件事吗?或者有更好的解决方案吗?请帮帮我。

DBEngine dbe = new DBEngine();
Database db = dbe.OpenDatabase("Database1.accdb", false, false, "");
Recordset rs = db.OpenRecordset("select solution from tab2 where id = 1", RecordsetTypeEnum.dbOpenDynaset, 0, LockTypeEnum.dbOptimistic);
rs.MoveFirst();

object o = rs.Fields[0].Value;
Image img = (Image)o; -> error
Image img = AxHost.GetPictureFromIPicture(o); - the method is missing
pictureBox1.Image = img;

rs.Close();

最佳答案

AxHost Class 的文档说

You typically do not use the AxHost class directly. You can use the Windows Forms ActiveX Control Importer (Aximp.exe) to generate the wrappers that extend AxHost.

我必须承认我还没有尝试过这种方法。据我所知,从 Access 数据库的附件字段中可靠地提取文件的唯一方法是使用 ACE DAO Field2.SaveToFile() 方法目的。在您的情况下,代码看起来像这样:

public partial class Form1 : Form
{
string tempFileName;
Image img;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DBEngine dbe = new DBEngine();
Database db = dbe.OpenDatabase(@"C:\Users\Public\AttachmentsDB.accdb");
Recordset rsMain = db.OpenRecordset(
"select solution from tab2 where id = 1",
RecordsetTypeEnum.dbOpenSnapshot);
Recordset2 rsAttach = rsMain.Fields["solution"].Value;
tempFileName = System.IO.Path.GetTempPath() + "\\" + rsAttach.Fields["FileName"].Value;
try
{
System.IO.File.Delete(tempFileName);
}
catch { }
Field2 fldAttach = (Field2)rsAttach.Fields["FileData"];
fldAttach.SaveToFile(tempFileName);
rsAttach.Close();
rsMain.Close();
db.Close();
img = Image.FromFile(tempFileName);
pictureBox1.Image = img;
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
img.Dispose();
System.IO.File.Delete(tempFileName);
}

}

关于c# - 缺少 AxHost.GetPictureFromIPicture() 方法,正在从 MS Access 数据库中检索图片(附件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com