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?

4

2 回答 2

1

这是一个例子:

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

哪里GetBitmap是:

    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;
    }

我在这里找到它:http: //visioninspection.googlecode.com/svn/trunk/print_2/Vision.ETNI/CControl_JAI2.cs

于 2015-05-03T19:19:37.533 回答
1

如果有人需要它,我找到了一种更简单(更快)的方法:

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;  
}
于 2015-11-15T22:01:48.777 回答