This document is archived and information here might be outdated.  Recommended version.


Write Blob Field Contents To File Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Write Blob Field Contents To File Snippet

Read the contents of a specified record (ObjectID) fom a Table for a Blob field and write to a file on disk.

[C#]
/// <summary>
/// Read the contents of a specified record (ObjectID) fom a Table for a Blob field and write to a file on disk.
/// </summary>
/// <param name="string_Filename">A System.String that is the name of a binary file to write to. Example: "C:\temp\myPicture2.bmp"</param>
/// <param name="table">An ITable interface that is the table is to have a specific row read for a BLOB.</param>
/// <param name="int32_BlobFieldIndex">A System.Int32 that is the index number of the Blob field in the Fields collection of the Table. Example: 3</param>
/// <param name="int32_ObjectID">A System.Int32 that is the specific ObjectID record for which we want to obtain the Blob field. Example: 1968</param>
/// <returns>True=successful. False=unscucceeful.</returns>
/// <remarks></remarks>
public System.Boolean WriteBlobFieldContentsToFile(System.String string_Filename, ESRI.ArcGIS.Geodatabase.ITable table, System.Int32 int32_BlobFieldIndex, System.Int32 int32_ObjectID)
{

    try
    {

        //Get the specific Row based upon the ObjectID
        ESRI.ArcGIS.Geodatabase.IRow row=table.GetRow(int32_ObjectID);

        //Get the field containing the Blob field.
        ESRI.ArcGIS.Geodatabase.IFields fields=row.Fields;
        ESRI.ArcGIS.Geodatabase.IField field=fields.get_Field(int32_BlobFieldIndex);

        //Test to ensure the index number specified is a Blob Field
        if (field.Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob)
        {

            //Get the Blob Field valie and save to a file on disk
            ESRI.ArcGIS.esriSystem.IMemoryBlobStream memoryBlobStream=(ESRI.ArcGIS.esriSystem.IMemoryBlobStream)row.get_Value(int32_BlobFieldIndex);
            memoryBlobStream.SaveToFile(string_Filename);

            //success
            return true;
        }
        else
        {

            //unsuccessful
            return false;

        }

    }
    catch (System.Exception ex)
    {

        //unsuccessful with error, interrogate more
        return false;

    }

}

[Visual Basic .NET]
''' <summary>
''' Read the contents of a specified record (ObjectID) fom a Table for a Blob field and write to a file on disk.
''' </summary>
''' <param name="string_Filename">A System.String that is the name of a binary file to write to. Example: "C:\temp\myPicture2.bmp"</param>
''' <param name="table">An ITable interface that is the table is to have a specific row read for a BLOB.</param>
''' <param name="int32_BlobFieldIndex">A System.Int32 that is the index number of the Blob field in the Fields collection of the Table. Example: 3</param>
''' <param name="int32_ObjectID">A System.Int32 that is the specific ObjectID record for which we want to obtain the Blob field. Example: 1968</param>
''' <returns>True=successful. False=unscucceeful.</returns>
''' <remarks></remarks>
Public Function WriteBlobFieldContentsToFile(ByVal string_Filename As System.String, ByVal table As ESRI.ArcGIS.Geodatabase.ITable, ByVal int32_BlobFieldIndex As System.Int32, ByVal int32_ObjectID As System.Int32) As System.Boolean

  Try

    'Get the specific Row based upon the ObjectID
    Dim row As ESRI.ArcGIS.Geodatabase.IRow=table.GetRow(int32_ObjectID)

    'Get the field containing the Blob field.
    Dim fields As ESRI.ArcGIS.Geodatabase.IFields=row.Fields
    Dim field As ESRI.ArcGIS.Geodatabase.IField=fields.Field(int32_BlobFieldIndex)

    'Test to ensure the index number specified is a Blob Field
    If field.Type=ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob Then

      'Get the Blob Field valie and save to a file on disk
      Dim memoryBlobStream As ESRI.ArcGIS.esriSystem.IMemoryBlobStream=CType(row.Value(int32_BlobFieldIndex), ESRI.ArcGIS.esriSystem.IMemoryBlobStream) ' Explicit Cast
      memoryBlobStream.SaveToFile(string_Filename)

      'success
      Return True
    Else

      'unsuccessful
      Return False

    End If

  Catch ex As System.Exception

    'unsuccessful with error, interrogate more
    Return False

  End Try

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.System
  • System