视线语音鼠标 4 C#图像的读取和显示

读取图像的rgb信息,做视频流的话,不用指针速度是无法忍受的。
用rectangle框选,转为bitmapData,用指针读,注意颜色顺序是b,g,r。
【视线语音鼠标 4 C#图像的读取和显示】显示的话也是差不多的,我的图像显示是二值图(Seg存的),返回bitmap,到时可用pictureBox什么的显示就行了。
不多说了,上代码。
public void InIt(Bitmap Source) { int iWidth = Source.Width; int iHeight = Source.Height; Rectangle rect = new Rectangle(0, 0, iWidth, iHeight); BitmapData bmpData = https://www.it610.com/article/Source.LockBits(rect, ImageLockMode.ReadWrite, Source.PixelFormat); int r, g, b; IntPtr iPtr = bmpData.Scan0; unsafe //启动不安全代码 { byte* Ptr = (byte*)(void*)iPtr; int srcOffset = bmpData.Stride - iWidth * 4; for (int j = 0; j < iHeight; j++) { for (int i = 0; i < iWidth; i++, Ptr += 4) { b = (int)Ptr[0]; g = (int)Ptr[1]; r = (int)Ptr[2]; } Ptr += srcOffset; } } Source.UnlockBits(bmpData); } public static Bitmap Display(double[,] Seg) { int iWidth = Seg.GetLength(0); int iHeight = Seg.GetLength(1); Bitmap Result = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Rectangle rect = new Rectangle(0, 0, iWidth, iHeight); BitmapData bmpData1 = Result.LockBits(rect, ImageLockMode.ReadWrite, Result.PixelFormat); IntPtr iPtr1 = bmpData1.Scan0; int iBytes = iWidth * iHeight * 3; byte[] PixelValues = new byte[iBytes]; int iPoint = 0; for (int j = 0; j < iHeight; j++) { for (int i = 0; i < iWidth; i++, iPoint += 3) { if (Seg[i, j]> 0) { PixelValues[iPoint] = Convert.ToByte(255); PixelValues[iPoint + 1] = Convert.ToByte(255); PixelValues[iPoint + 2] = Convert.ToByte(255); } else { PixelValues[iPoint] = Convert.ToByte(0); PixelValues[iPoint + 1] = Convert.ToByte(0); PixelValues[iPoint + 2] = Convert.ToByte(0); } } } Marshal.Copy(PixelValues, 0, iPtr1, iBytes); Result.UnlockBits(bmpData1); return Result; }

    推荐阅读