AxHost.GetPictureFromIPicture() method missing, retrieving picture (attachment) from MS Access database

331 views Asked by At

I'm trying to use AxHost.GetPictureFromIPicture() to get a GIF image (saved as an attachment type) from MS Access 2013 database file (*.accdb) - convert it to Image so I can display it in a PictureBox. But the method is not there! :( Am I missing something? Do I need to set or install smtg?

Without conversion I get this error: "Unable to cast COM object of type 'System.__ComObject' to class type 'System.Drawing.Image'"

Do I actually do the whole thing the right way? Or is there a better solution? Please, help me.

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();
1

There are 1 answers

0
Gord Thompson On

The documentation for the AxHost Class says

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.

I must admit that I have not tried that approach. To the best of my knowledge the only way to reliably extract a file from an Attachment field in an Access database is to use the .SaveToFile() method of an ACE DAO Field2 object. In your case, the code would look something like this:

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);
    }

}