Search code examples
c#sqlodbcfilemaker

Insert image file into Filemaker container using C#


I am try to insert a picture (.bmp) into a container field in Filemaker. I have managed to get it to insert fine, but it saves it as a .dat file. I was wondering if there was a way to specify what file type to save it as?

        try
        {
            Bitmap tempImage = new Bitmap("C:\\temp\\black.bmp");
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            tempImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] image = stream.ToArray();

            OdbcConnection connection = new OdbcConnection("DSN=Filemaker;UID=admin");
            connection.Open();


            OdbcCommand command = new OdbcCommand("INSERT INTO TestDatabase (LocatorNum, FileName, SampleSet, Image) VALUES (" + 
                                                                                                                        "'003'" + ", " + 
                                                                                                                        "'003'" + ", " + 
                                                                                                                        "'003'" + ", " +
                                                                                                                        "?" + ")");

            command.Connection = connection;
            command.Parameters.AddWithValue("?", OdbcType.VarBinary).Value = image;
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Solution

  • FileMaker containers have multiple streams. Each stream is identified with a four-character identifier: 'FNAM' is file name, 'SIZE' is image dimensions, 'FILE' is file data. If you want the container to display as an image, you can have a supported image stream, e.g. 'JPEG', 'PDF ' (note the trailing space), and so on.

    In your case you need to put your data into one of streams. I don't know if FileMaker supports BMP as an image format, at least I cannot find the stream code for it, but at least you put it as 'FILE'. FileMaker docs say that you need to use the PutAs() function:

    PutAs( column, type )
    

    I'm not sure how exactly is it to be used and cannot test right now, but from what I understand it must be among column names:

    INSERT INTO TestDatabase
           (LocatorNum, FileName, SampleSet, PutAs(Image, 'FILE'))
    VALUES ('003', '003', '003', ?)
    

    Maybe you can edit multiple streams this way, i.e.:

    INSERT INTO TestDatabase
           (..., PutAs(Image, 'FILE'), PutAs(Image, 'JPEG'), ...)
    

    'FILE', 'JPEG' and other similar streams are binary, so you need to declare a ? placeholder and then bind the query with parameters the way you do this now. I'm not sure about the 'FNAM' stream though; I suspect it can be simply set to a string. I have no idea how to pass 'SIZE', but I think FileMaker can calculate this one on its own.

    UPDATE: BMP stream code is 'BMPf'.