9 Replies Latest reply on Mar 16, 2018 6:31 AM by jb455

    Video frame to bitmap




      I'm now starting to use the d435 camera.

      I know how to convert a video frame to BitmapSource but is there any function to convert a video frame to bitmap on C#?

      If not how should I approach this? I tried by doing the follow but with no success.

      var bytes = new byte[frame.Stride * frame.Height];
                  Bitmap bmpRGB = null;
                  unsafe {
                      fixed (byte* ptr = bytes)
                          using (Bitmap image = new Bitmap(frame.Width, frame.Height, frame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(ptr)))
                              bmpRGB = image;
                  return bmpRGB;




        • 1. Re: Video frame to bitmap

          Have you considered the 'Save-To-Disk' SDK 2.0 sample program that saves stream data as a PNG image file?


          librealsense/examples/save-to-disk at master · IntelRealSense/librealsense · GitHub

          • 2. Re: Video frame to bitmap

            What's the problem with what you have? Do you get an error or corrupted image?

            Does it work if you use frame.Data instead of new IntPtr(ptr)?

            Though I think if you do it that way, the bitmap you create will have the same lifespan as the original frame so if the frame is released the bitmap will corrupt. You could try doing "return new Bitmap(bmpRGB)" if this is an issue.

            • 3. Re: Video frame to bitmap



              The problem that I have know is that it throws me an exception.

              I tried your suggestions.

              In the attachments I send the project. Is the cs-tutorial-2-capture sample. Is the sample from Intel but I added the code to convert to bitmap and to display it as a bitmapSource.

              My intention is to use the image as bitmap to process it  using OpenCVSharp.




              • 4. Re: Video frame to bitmap
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hello VCS_5,

                It would be recommended for you to use this function for saving bitmaps: "int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);". 

                Here is an example on when this function is used: 

                Hope this function helps to clarify your problem.

                Best Regards, 
                Juan N. 

                • 5. Re: Video frame to bitmap

                  This works for me:


                  private System.Drawing.Bitmap FrameToBitmap(VideoFrame frame, System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                      if (frame.Width == 0) return null;
                      return new System.Drawing.Bitmap(frame.Width, frame.Height, frame.Stride, format, frame.Data);




                  Just realised what was probably wrong with yours: the bitmap object you create is in a using block so it gets disposed of almost as soon as it's created!

                  • 6. Re: Video frame to bitmap



                    I can't convert it back to bitmapSource. And when I try to convert to Mat I got this exception:

                    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

                    I can't even debug it because by only running the project it consumes 100% of my CPU. I'm using the D435 camera.

                    Any further ideas?




                    • 7. Re: Video frame to bitmap

                      Is that when using my code example?

                      That error suggests the bitmap data is no longer available. Make sure you're not disposing or releasing the bitmap before you use it again, and don't put a variable you'll want to use later as the subject of a using() block.


                      What method are you using to convert the image to a mat?

                      • 8. Re: Video frame to bitmap



                        Yes, I'm using your code see the project in the attachment.

                        To convert Bitmap to Mat I'm using a OpenCVSharp function.

                        var matImage = BitmapConverter.ToMat(image);

                        Yo run the project in the attachment you have to install OpenCVSharp 3.xxx via nugget.




                        • 9. Re: Video frame to bitmap

                          Ah, it's a pixelformat issue.

                          Change the format on line 43 to Format.Bgr8, then in the call on line 59 add ", System.Drawing.Imaging.PixelFormat.Format24bppRgb". (there's either a bug or difference in endianness which causes bgr and rgb to swap)


                          Should work now!