Select to view content in your preferred language

Exception thrown while saving multi-patch textures in ArcGISPro SDK

616
1
05-13-2020 08:43 AM
BillSmith
Regular Contributor

Hello,

I am seeing an occasional error while trying to save out textures for multi-patch features.  It will throw an exception of "Parameter is not valid." when creating an Image from a MemoryStream.  It works for about 70% of the textures, but some fail.

Code snippet.

var patches = builder.Patches;

for (int x = 0; x < patches.Count; ++x) {
   TextureResource texture = null;
   BasicMaterial bm = patches.Material as BasicMaterial;
   EsriMultipatchMaterial material = new EsriMultipatchMaterial();

   material.material_index = -1;

   if (multipatch.HasMaterials) {
      material.material_index = multipatch.GetPatchMaterialIndex(x);
   }

   if (bm != null) {
      texture = bm.TextureResource;
   }

   if (texture != null && material.material_index >= 0) {


      if (m_texture_dir == null || m_texture_dir.Length == 0) continue;

      material.type = see_multipatch_material_type.SEE_TXR_FILE

      string file_name = string.Format("text_{0}_{1}_{2}.jpg",
      layer_name, object_id, material.material_index.ToString("D5"));

      material.txr_file_name = file_name;

      string file_path = m_texture_dir + "\\" + file_name;

      if (!File.Exists(file_path)) {
         // Get the pixel data and save the file.
         // Retrieve the texture pixels and save to JPEG.

         try {
               TextureMap txr_map = texture.Texture as TextureMap; // 
               byte[] img_buffer = txr_map.Buffer;

               MemoryStream stream = new MemoryStream(img_buffer);
               Image image = Image.FromStream(stream);  ( Exception: Parameter is not valid)

               ImageFormat format = ImageFormat.Jpeg;
               image.Save(file_path, format);

               image.Dispose();
            }

..... etc. ....

}}

Any suggestions would be greatly appreciated.

Thanks,

-Bill

1 Reply
NarelleChedzey
Esri Contributor

Bill, 

A couple of suggestions - check the byte buffer length - maybe it is 0. 

Also is the textureMap a JPEGTexture or an UncompressedTexture?

Narelle