HOWTO: Export Image to an IStream Object

SUMMARY is a Visual C++ 6.0 sample that demonstrates how to export image to an IStream object.

For performance or other reasons sometimes it is desirable to export the barcode image to the memory only as a stream of data.


In the IDL, ExportImage is defined as:

[HRESULT ExportImage(VARIANT Destination, 
    ImageFormatEnum ImageFormat);  

The interface function takes two parameters. The first one is a VARIANT and the second one is an interger indicating the image format. The first parameter can contain two types - if it is a string, the parameter contains the path to the destination file. If it holds an IUnknown pointer, it is treated as an IStream object.

This sample uses the following steps to export the barcode image:

  1. Calls CreateStreamOnHGlobal() to create a standard IStream object.

  2. Calls QueryInterface to retrieve the IUnknown pointer.

  3. ackages the IUnknown pointer into a VARIANT.

  4. Calls ExportImage to export the image into IStream object.

Code Hightlights

Creating Barcode Objects at Background

This step can be skipped if you create the Barcode ActiveX object in Visual Studio Form designer or other integrated programming environment.

//Create Morovia Barcode ActiveX object
CComPtr<IDispatch> spDisp;
HRESULT hr = spDisp.CoCreateInstance(L"Morovia.BarcodeActiveX");
if ( FAILED(hr) ) {
  cerr << _T("Failed to load Morovia Barcode ActiveX object.") << endl;
  cerr << _T("Please reinstall the ActiveX object using the installer provided")<< endl;
  cerr << _T("by Morovia Corporation.")<< endl;
  return -1;
cout << _T("Morovia Barcode ActiveX loaded succesfully.") << endl;  

Memory Stream

Create a standard memory IStream object and then retrieve its IUnknown interface.

CComPtr<IStream> spStream;
CreateStreamOnHGlobal(NULL, TRUE, &spStream);
CComPtr<IUnknown> spUnknown;

Exporting Image

HRESULT hr = _com_dispatch_method(spDisp, 1004, DISPATCH_METHOD, VT_EMPTY, 
(void*)(NULL), L"\x000D\x0003", spUnknown, 1); 

In this sample we use the IDispatch interface to invoke the function. If you have the IBarcode interface pointer, you may use IBarcode pointer which is simpler:

CComVariant streamVar( spUnknown);
CComQIPtr<IBarcode> spBarcode(spDisp);
spBarcode->ExportImage(streamVar, 1); 

The integer 1 indicates we ask for the JPEG format. For the list of image formats supported by the control, refer to the Barcode ActiveX Reference Manual.



  • Morovia Barcode ActiveX 3