title: Xamarin.ios: Objective-C to C#
body: <p>I am porting an application from iOS to a Xamarin project. The Objective-C class is working but i don't really know how to write the C# version of it. If someone sees something wrong i would be grateful.</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput
  didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
         fromConnection:(AVCaptureConnection *)connection
  {
  
      CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  
      CVPixelBufferLockBaseAddress(imageBuffer,0);
  
      baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0);
  
      int pixelFormat = CVPixelBufferGetPixelFormatType(imageBuffer);
  
      switch (pixelFormat)
       {
          case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
  
              bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);
              width = bytesPerRow;
              height = CVPixelBufferGetHeightOfPlane(imageBuffer,0);
              break;
  
          case kCVPixelFormatType_422YpCbCr8:
  
              bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);
              width = CVPixelBufferGetWidth(imageBuffer);
              height = CVPixelBufferGetHeight(imageBuffer);
              int len = width*height;
              int dstpos=1;
              for (int i=0;i&lt;len;i++)
              {
                  baseAddress[i]=baseAddress[dstpos];
                  dstpos+=2;
              }
  
              break;
          default:
  
              break;
      }
  
      unsigned char *pResult=NULL;
  
      int resLength = MWB_scanGrayscaleImage(baseAddress,width,height, &amp;pResult);
  
  .................
  }
  </code></pre>
  
  <p>This is the part of my code where i try to port it for Xamarin platform:</p>
  
  <pre><code>public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
  {
     using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer)
     {
         pixelBuffer.Lock (0);
  
         var baseAddress = pixelBuffer.BaseAddress;
         int bytesPerRow = pixelBuffer.BytesPerRow;
         int width = pixelBuffer.Width;
         int height = pixelBuffer.Height;
  
      //this is where i have to make a conversion because i need a byte[] from baseAddress
         byte [] managedArray = new byte[width*height];
         Marshal.Copy(baseAddress, managedArray, 0, width*height);
  
         byte [] rawResult = new byte[3000];
  
      // When it hits this line the app comes to a stop without any error message
      // Declaration: public static extern int MWB_scanGrayscaleImage (byte[] gray, int width, int height, out byte[] result);
         int resLength = BarcodeScannerClass.MWB_scanGrayscaleImage(managedArray,width,height,out rawResult);
  
  ..............
  }
  </code></pre>