1.10.2 Importing Images


Origin allows you to import images into a matrix or a worksheet cell, and onto a graph. The following sections will show you how to import images in your Origin C applications.

Import Image into Matrix

The following example function demonstrates how to import an image file into a matrix. The function takes three arguments: matrix name, file name, and grayscale depth. The key functions being called in this example are oimg_image_info and oimg_load_image. The first is used to get information about the image contained in the image file. The information obtained is used in preparing the target matrix. The latter function is used to do the actual importing of the image file into the target matrix as grayscale data values.

#include <import_image.h> // needed for oimg_ functions
 
bool import_image_to_matrix_data(
    LPCSTR lpcszMatrixName, // matrixbook name
    LPCSTR lpcszFileName,   // image file name
    int nGrayDepth)         // import as 8-bit or 16-bit gray
{
    // Get the target matrix object
    MatrixObject mo(lpcszMatrixName);
    if( !mo.IsValid() )
        return false;

    // Get source image information
    int nWidth, nHeight, nBPP;
    if( !oimg_image_info(lpcszFileName, &nWidth, &nHeight, &nBPP) )
        return false;

    // Set target matrix to same dimensions as source image
    if( !mo.SetSize(nHeight, nWidth, 0) )
        return false;
 
    // Set target matrix data size
    int nDataType = (16 == nGrayDepth ? FSI_USHORT : FSI_BYTE);
    if( !mo.SetInternalData(nDataType, FALSE, FALSE) )
        return false;

    // Import the image into the matrix
    bool bRet;
    if( FSI_USHORT == nDataType )
    {
        Matrix<WORD>& mm = mo.GetDataObject();
        bRet = oimg_load_image(lpcszFileName, &mm, 16, nHeight, nWidth);
    }
    else // FSI_BYTE
    {
        Matrix<BYTE>& mm = mo.GetDataObject();
        bRet = oimg_load_image(lpcszFileName, &mm, 8, nHeight, nWidth);
    }
    return bRet;
}

Import Image into Worksheet Cell

The following example will embed a JPEG image from a file into a worksheet cell. This is accomplished using the AttachPicture method of the Worksheet class.

int nRow = 0, nCol = 0;
string strFile = "D:\\Graph1.jpg";
DWORD dwEmbedInfo = EMBEDGRAPH_KEEP_ASPECT_RATIO;

Worksheet wks = Project.ActiveLayer();
if( wks.AttachPicture(nRow, nCol, strFile, dwEmbedInfo) )
{
    wks.Columns(nCol).SetWidth(20);
    wks.AutoSize();
}

Import Image to Graph

The following example will embed a JPEG image from a file onto a graph layer. This is accomplished using the image_import_to_active_graph_layer global function.

#include <image_utils.h>

// make sure image_utils.c is compiled before calling
// the image_import_to_active_graph_layer function.
LT_execute("run.LoadOC(Originlab\\image_utils.c)");

string strFile = "D:\\Graph1.jpg";
image_import_to_active_graph_layer(strFile);