2017-02-04 1 views
1

코드는 그래서 사용하여 바이트에서 비트 맵을 만들려고 C# WPF 응용 프로그램에 잘 작동하지만 Form 응용 프로그램에서 쓰기 가능한 비트 맵를 만들 기운으로 Windows Form application에 제대로 이미지를 표시 할 수 없습니다 Lockbits 및 Marshal 복사 방법. 이미지 형식에 문제가 있습니까?넥트 V1 C#을 캔트 디스플레이 이미지가 제대로

enter image description here

이 내 현재 코드입니다. BytesToBitmap 바이트를 비트 맵으로 변환하여 PixelFormat.Format32bppRgb 형식의 함수를 추가했습니다.

코드 :

public partial class Form1 : Form 
    { 
     private KinectSensor sensor; 
     private Bitmap colorBmp; 
     private Bitmap depthBmp; 
     private byte[] colorPxl; 
     private byte[] depthPxl; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 


     public void formLoaded(object sender, EventArgs e) 
     { 
      foreach (var potenialK in KinectSensor.KinectSensors) 
      { 
       ... 
      } 

      if (null != this.sensor) 
      { 
       this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); 
       this.sensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30); 
       this.sensor.SkeletonStream.Enable(); 
       this.colorPxl = new byte[this.sensor.ColorStream.FramePixelDataLength]; 
       this.colorBmp = new Bitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight); 
       this.depthBmp = new Bitmap(this.sensor.DepthStream.FrameWidth, this.sensor.DepthStream.FrameHeight); 

       this.sensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(SensorAllFrameReady); 

       try 
       { 
        this.sensor.Start(); 
       } 
       catch (IOException) 
       { 
        this.sensor = null; 
       } 
      } 
     } 

     private void SensorAllFrameReady(object sender, AllFramesReadyEventArgs e) 
     { 
      using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
      { 
       if (colorFrame != null) 
       { 
        colorFrame.CopyPixelDataTo(this.colorPxl); 

        this.pictureBox1.Image = BytesToBitmap(colorPxl, 640, 480); 

       } 
      } 

      using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) 
      { 
       if (depthFrame != null) 
       { 
        depthPxl = GenerateColoredBytes(depthFrame); 

        this.pictureBox2.Image = BytesToBitmap(depthPxl, 320, 240); 

       } 
      } 
     } 

     private byte[] GenerateColoredBytes(DepthImageFrame depthFrame) 
     { 
      ... 

      return pixels; 
     } 

     public static Bitmap BytesToBitmap(byte[] pixelData, int height, int width) 
     { 
      var bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); 
      var bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); 
      var ptr = bitmapData.Scan0; 

      Marshal.Copy(pixelData, 0, ptr, pixelData.Length); 
      bitmap.UnlockBits(bitmapData); 

      return bitmap; 
     } 

답변

0

그래서이 일 것입니다. colorframeBytesToBitmap 함수에 직접 전달하면 올바르게 프레임을 볼 수 있습니다.

코드 :

public static Bitmap ImageToBitmap(ColorImageFrame Image) 
     { 
      byte[] pixeldata = new byte[Image.PixelDataLength]; 
      Image.CopyPixelDataTo(pixeldata); 
      Bitmap bmap = new Bitmap(Image.Width, Image.Height, PixelFormat.Format32bppRgb); 
      BitmapData bmapdata = bmap.LockBits(
       new Rectangle(0, 0, Image.Width, Image.Height), 
       ImageLockMode.WriteOnly, 
       bmap.PixelFormat); 
      IntPtr ptr = bmapdata.Scan0; 
      Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength); 
      bmap.UnlockBits(bmapdata); 
      return bmap; 
     }