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

时间:2015-05-03 19:00:31

标签: c# bitmap camera aforge gige-sdk

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

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

但它给出了一个错误:

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

您能帮我解决如何将原始图像从相机转换为位图图像的问题吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;  
}