Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am doing Kinect programming and when I enable the color stream, I defined to use Bayer 30 frames per second format (ColorImageFormat.RawBayerResolution640x480Fps30).

When rendering the image, I use the following code:
C#
PixelFormat format = PixelFormats.Bgr32;

// Bitmap source to write to.
WriteableBitmap src = new WriteableBitmap((int)_kinectSensor.ColorStream.FrameWidth, (int)_kinectSensor.ColorStream.FrameHeight, 96, 96, format, null);

// Stride for image.
var stride = (int)_kinectSensor.ColorStream.FrameWidth * format.BitsPerPixel / 8;

// Write pixels to bitmap source.
src.WritePixels(new Int32Rect(0, 0, (int)imgPic.Width, (int)imgPic.Height), rawBayerPixels, stride, 0);

// Set XAML image source = Bitmap source.
imgPic.Source = src;

How do I workout the PixelFormat without hardcoding as above, based on the selected ColorImageFormat? So convert one enum to the other?

Thanks in advance!
Posted

1 solution

You can't directly cast one enum to another but you can use the fact that they are based on integer values as a work around. If the enumeration VALUES are the same, you could probably do something with IsDefined to figure out if a given values matches another one and then return that.

I haven't looked at those particular enums to know how they match up but more than likely you are going to have to "manually" match them up. I would suggest a function that returns the desired enum type and takes the source type as a parameter. Maybe you can use some of the functions on System.Enum to do some dynamic matching but even worse case, you could write a switch statement to return one value for another.

At any rate, I would check the MSDN docs on the enum class. There are lots of helper functions there that may assist you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900