1

I'm using a JAI GigE Vision camera for image acquisition, but I want to use the AForge Library in C# for the image analysis to create a camera independent solution.

Jai_FactoryWrapper.ImageInfo localImageInfo = new Jai_FactoryWrapper.ImageInfo();
image = (Bitmap)localImageInfo;

But it gives back an error:

Cannot convert type 'Jai_FactoryDotNET.Jai_FactoryWrapper.ImageInfo' to 'System.Drawing.Bitmap'

Could you help me how to convert the raw image from the camera to a bitmap image?

alfayadd
  • 23
  • 9

2 Answers2

1

Here is an example:

   Bitmap image = GetBitmap((int)ImageInfo.SizeX, (int)ImageInfo.SizeY, 8, (byte*)ImageInfo.ImageBuffer);

where GetBitmap is:

    public Bitmap GetBitmap(int nWidth, int nHeight, int nBpp, byte* DataColor)
    {
        Bitmap BitmapImage = new Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb);

        BitmapData srcBmpData = BitmapImage.LockBits(new Rectangle(0, 0, BitmapImage.Width, BitmapImage.Height),
            ImageLockMode.ReadWrite, BitmapImage.PixelFormat);

        switch (BitmapImage.PixelFormat)
        {
            case PixelFormat.Format24bppRgb:
                unsafe
                {
                    byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();

                    int nCount = srcBmpData.Width * srcBmpData.Height;
                    int nIndex = 0;

                    for (int y = 0; y < nCount; y++)
                    {
                        psrcBuffer[nIndex++] = DataColor[y];
                        psrcBuffer[nIndex++] = DataColor[y];
                        psrcBuffer[nIndex++] = DataColor[y];
                    }
                }
                break;
        }

        BitmapImage.UnlockBits(srcBmpData);

        return BitmapImage;
    }

I found it here: http://visioninspection.googlecode.com/svn/trunk/print_2/Vision.ETNI/CControl_JAI2.cs

romanoza
  • 4,775
  • 3
  • 27
  • 44
1

If anyone should need it, I found a simpler (and faster) way:

internal static Bitmap convertToBitmap(Jai_FactoryDotNET.Jai_FactoryWrapper.ImageInfo ImageInfo)  
{  
    Bitmap image = new Bitmap((int)ImageInfo.SizeX, (int)ImageInfo.SizeY, (int)ImageInfo.SizeX, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ImageInfo.ImageBuffer);  
    return image;  
}
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
alfayadd
  • 23
  • 9