将原始图像从 JAI GigE 相机转换为 C# 中的位图

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

我正在使用 JAI GigE Vision 相机进行图像采集,但我想使用 C# 中的 AForge 库进行图像分析以创建独立于相机的解决方案。

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

但是返回错误:

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

你能帮我看看如何将相机的原始图像转换为位图图像吗?

这是一个例子:

   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

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

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