Search code examples
c#bitmapcameraaforgegige-sdk

Convert raw image from JAI GigE camera to bitmap in C#


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?


Solution

  • 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