[英]How to convert a raw binary image in 2-2-2-2 format into a bitmap in 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.