OpenCV 筆記 5.1 OPENCV的Array 使用Bitmap在C#的 pictureBox (指標方式 BitmapData )

背景知識

OpenCV 筆記 5 OPENCV的Array 使用Bitmap在C#的 pictureBox (簡單用 For 迴圈)

簡單來講 就是用了 BitmapData 再用 Marshal.Copy 把byte[]印過去

 private Bitmap Load_RGB_img(ref byte[] Camera1_Buffer, int Width, int Height)
 {

     int Camera_len_Buffer = Width * Height * 3;

     Bitmap image0_Source = new Bitmap(
         Width, Height, 
         PixelFormat.Format24bppRgb);

     Rectangle rect = new Rectangle(
         0, 0, image0_Source.Width, image0_Source.Height);


     System.Drawing.Imaging.BitmapData bmpData = image0_Source.LockBits(
         rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, 
         image0_Source.PixelFormat);

     System.Runtime.InteropServices.Marshal.Copy(
         Camera1_Buffer, 0, bmpData.Scan0, 
         bmpData.Stride * image0_Source.Height);
    
     image0_Source.UnlockBits(bmpData);

    return image0_Source;

 }

LockBitsUnlockBits 是一對的記得要連在一起用