Convert IRasterValue to IMemoryBlobStream?

794
2
Jump to solution
03-26-2012 04:50 AM
StefanOffermann
Occasional Contributor II
Hi,

I have to convert an image from a raster column in an Oracle SDE feature class to a feature attachment of the same feature class. I am using C# to develop the code. I can read the IRasterValue from the blob column. I can add a feature attachment to the feature class. My problem is: How can I convert the image (IRasterValue) to the right object for the feature attachment (IMemoryBlobStream)?

Best regards,
Stefan
0 Kudos
1 Solution

Accepted Solutions
JohnNelson3
New Contributor III
Hopefully, the example below converting to IRaster -> Byte Array -> IMemoryBlobStream helps.

IRasterValue.Raster

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//0048000000mn000000

try
            {
                IFunctionRasterDataset pFuncRasterDataset = mybuilderItem.Dataset; // Get the FunctionRasterDataset from mybuilderItem
                IRasterDataset pRasterDataset;
                pRasterDataset = (IRasterDataset)pFuncRasterDataset; // Cast the FunctionRasterDataset to Raster Dataset
                IPropertySet thePropSet = pFuncRasterDataset.Properties; // Get the properties of the raster Dataset
                IRaster praster = pRasterDataset.CreateDefaultRaster(); // Create default raster from the above raster dataset
                praster.ResampleMethod = rstResamplingTypes.RSP_NearestNeighbor; // The raster is resampled by RSP_NearestNeighbor
                IRasterProps pRasterProps = (IRasterProps)praster; // Raster properties are used to update the height, width of the raster
                pRasterProps.Height = 256;
                pRasterProps.Width = 256;

                IRasterExporter pConverter = new RasterExporterClass(); // IRasterExporter object is used to convert the raster to byte array.
                byte[] pBytesArr;
                pBytesArr = pConverter.ExportToBytes(praster, "TIFF"); // Convert the resampled Raster to a Byte array
                IMemoryBlobStream memBlobStream = new MemoryBlobStream(); // Create new IMemoryBlobStream
                IMemoryBlobStreamVariant varBlobStream = (IMemoryBlobStreamVariant)memBlobStream; // Assign to IMemoryBlobStreamVariant
                object anObject = pBytesArr;
                varBlobStream.ImportFromVariant(anObject); // IMemoryBlobStreamVariant object is assigned the byte array
                thePropSet.SetProperty("ThumbNail", memBlobStream); // and saved to the property "ThumbNail"
            }

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000001qr000000.ht...

// Open a file as a memory blob stream.
IMemoryBlobStream memoryBlobStream = new MemoryBlobStreamClass();
memoryBlobStream.LoadFromFile("Image1.png");

// Create an attachment.
IAttachment attachment = new AttachmentClass
{
    ContentType = "image/png", Data = memoryBlobStream, Name = "Image1.png"
};


http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/ISaveAs2_Interface/00250...

View solution in original post

2 Replies
JohnNelson3
New Contributor III
Hopefully, the example below converting to IRaster -> Byte Array -> IMemoryBlobStream helps.

IRasterValue.Raster

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//0048000000mn000000

try
            {
                IFunctionRasterDataset pFuncRasterDataset = mybuilderItem.Dataset; // Get the FunctionRasterDataset from mybuilderItem
                IRasterDataset pRasterDataset;
                pRasterDataset = (IRasterDataset)pFuncRasterDataset; // Cast the FunctionRasterDataset to Raster Dataset
                IPropertySet thePropSet = pFuncRasterDataset.Properties; // Get the properties of the raster Dataset
                IRaster praster = pRasterDataset.CreateDefaultRaster(); // Create default raster from the above raster dataset
                praster.ResampleMethod = rstResamplingTypes.RSP_NearestNeighbor; // The raster is resampled by RSP_NearestNeighbor
                IRasterProps pRasterProps = (IRasterProps)praster; // Raster properties are used to update the height, width of the raster
                pRasterProps.Height = 256;
                pRasterProps.Width = 256;

                IRasterExporter pConverter = new RasterExporterClass(); // IRasterExporter object is used to convert the raster to byte array.
                byte[] pBytesArr;
                pBytesArr = pConverter.ExportToBytes(praster, "TIFF"); // Convert the resampled Raster to a Byte array
                IMemoryBlobStream memBlobStream = new MemoryBlobStream(); // Create new IMemoryBlobStream
                IMemoryBlobStreamVariant varBlobStream = (IMemoryBlobStreamVariant)memBlobStream; // Assign to IMemoryBlobStreamVariant
                object anObject = pBytesArr;
                varBlobStream.ImportFromVariant(anObject); // IMemoryBlobStreamVariant object is assigned the byte array
                thePropSet.SetProperty("ThumbNail", memBlobStream); // and saved to the property "ThumbNail"
            }

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000001qr000000.ht...

// Open a file as a memory blob stream.
IMemoryBlobStream memoryBlobStream = new MemoryBlobStreamClass();
memoryBlobStream.LoadFromFile("Image1.png");

// Create an attachment.
IAttachment attachment = new AttachmentClass
{
    ContentType = "image/png", Data = memoryBlobStream, Name = "Image1.png"
};


http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/ISaveAs2_Interface/00250...
StefanOffermann
Occasional Contributor II
Thanks John,

IRasterExporter.ExportToBytes is exactly the interface and method I was looking for 🙂

After reading the blob object from the feature to the variable 'photo', this code works for me:

            
object photo = feature.Value[blobFieldIndex];
IRasterExporter exporter = new RasterExporterClass();
var rasterAsBytes = exporter.ExportToBytes(((IRasterValue) photo).Raster, "TIFF");
IMemoryBlobStream memBlobStream = new MemoryBlobStream();
var varBlobStream = (IMemoryBlobStreamVariant) memBlobStream;
varBlobStream.ImportFromVariant(rasterAsBytes);
var tableAttachments = (ITableAttachments) featureClass;
var attachmentManager = tableAttachments.AttachmentManager;
attachmentManager.AddAttachment(feature.OID, new AttachmentClass
                                                          {
                                                              Name = "nameOfRaster.jpg",
                                                              ContentType = "image/jpg",
                                                              Data = varBlobStream
                                                          });
0 Kudos