Community
cancel
Showing results for 
Search instead for 
Did you mean: 
jb455
Valued Contributor II
62 Views

Converting Bitmap To PXCMImage

Hi,

I'm trying to convert a PXCMImage to a bitmap, manipulate it, then convert it back to a PXCMImage. The first two steps are working fine, but I'm having trouble on the third step: turning it back into a PXCMImage.

Here's what I have so far:

public PXCMImage BitmapToImage(Bitmap b)
{
   try
   {
      PXCMImage.ImageData data = new PXCMImage.ImageData();
      data.FromByteArray(0, ImageToByte(b));
      PXCMImage.ImageInfo iinfo = new PXCMImage.ImageInfo()
      {
         width = b.Width,
         height = b.Height,
         format = PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24
      };
      PXCMImage image = session.CreateImage(iinfo);
      image.ImportData(data, 0);
      return image;
   }
   catch(Exception e)
   {
      return null;
   }
}

I always get an exception on the FromByteArray line. My bitmap to byte array converter is working fine: that's not the issue. The message I get is "Value cannot be null.\r\nParameter name: destination", and the StackTrace is "at System.Runtime.InteropServices.Marshal.CopyToNative(Object source, Int32 startIndex, IntPtr destination, Int32 length)
   at PXCMImage.ImageData.FromByteArray(Int32 index, Byte[] src)"

Can anyone shed any light on why this is happening? Has anyone successfully used this method? I couldn't find any usages of it in any of the samples.

As a secondary issue (I don't know if it's a problem yet as my code doesn't get that far), the docs for PXCMImage.ImportData says the method only needs an ImageData input, but when I use it it also requres an int value named flags. What should this be? Do the docs need updating?

Thanks!
James

0 Kudos
1 Reply
jb455
Valued Contributor II
62 Views

Ok, so I just had a thought and fixed my first problem like so:

PXCMImage image = session.CreateImage(iinfo);
image.AcquireAccess(PXCMImage.Access.ACCESS_WRITE, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24, out data);
data.FromByteArray(0, Common.Image.ImageToByte(b));
image.ImportData(data, 0);
image.ReleaseAccess(data);
return image;

However, the ImportData method is taking a couple of seconds to execute (not great for real time video streaming), and the resultant image comes out as some colourful static fuzz (also less than ideal). Could it be something to do with "flags" integer? Or maybe the byte array needs to be in a specific format? (My bitmap is RGB24 already.)

James

Reply