when I run my program I am facing this exeption, can anyone help me please!
Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
An unhandled exception of type 'System.NotSupportedException' occurred in PresentationCore.dll
No information was found about this pixel format.
Unhandled Exception: System.NotSupportedException: No information was found about this pixel format.
at System.Windows.Media.PixelFormat.CreatePixelFormatInfo()
at System.Windows.Media.PixelFormat.get_InternalBitsPerPixel()
at System.Windows.Media.PixelFormat.get_BitsPerPixel()
at WpfApp5.MainWindow.<.ctor>g__ToBitmap|0_1(ColorFrame frame) in C:\Users\Student\source\repos\WpfApp5\WpfApp5\MainWindow.xaml.cs:line 96
at WpfApp5.MainWindow.<.ctor>g__Reader_MultiSourceFrameArrived|0_0(Object sender, MultiSourceFrameArrivedEventArgs e) in C:\Users\Student\source\repos\WpfApp5\WpfApp5\MainWindow.xaml.cs:line 56
at ContextEventHandler`1.SendOrPostDelegate(Object state)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at WpfApp5.App.Main()
The program '[10700] WpfApp5.exe: Program Trace' has exited with code 0 (0x0).
The program '[10700] WpfApp5.exe' has exited with code 0 (0x0).
my complete code is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Array pixels;
public MainWindow()
{
InitializeComponent();
KinectSensor _sensor;
MultiSourceFrameReader _reader;
_sensor = KinectSensor.GetDefault();
if (_sensor != null)
{
_sensor.Open();
}
_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color |
FrameSourceTypes.Depth |
FrameSourceTypes.Infrared);
_reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;
void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
// Get a reference to the multi-frame
var reference = e.FrameReference.AcquireFrame();
// Open color frame
using (var frame = reference.ColorFrameReference.AcquireFrame())
{
if (frame != null)
{
// Do something with the frame...
camera.Source = ToBitmap(frame);
}
}
// Open depth frame
using (var frame2 = reference.DepthFrameReference.AcquireFrame())
{
if (frame2 != null)
{
// Do something with the frame...
camera.Source = ToBitmap2(frame2);
}
}
// Open infrared frame
using (var frame1 = reference.InfraredFrameReference.AcquireFrame())
{
if (frame1 != null)
{
// Do something with the frame...
camera.Source = ToBitmap3(frame1);
}
}
}
ImageSource ToBitmap(ColorFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
byte[] pixels = new byte[width * height * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8)];
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(pixels);
}
else
{
frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
ImageSource ToBitmap2(DepthFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
ushort minDepth = frame.DepthMinReliableDistance;
ushort maxDepth = frame.DepthMaxReliableDistance;
ushort[] depthData = new ushort[width * height];
byte[] pixelData = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel + 7) / 8];
frame.CopyFrameDataToArray(depthData);
int colorIndex = 0;
for (int depthIndex = 0; depthIndex < depthData.Length; ++depthIndex)
{
ushort depth = depthData[depthIndex];
byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
pixelData[colorIndex++] = intensity; // Blue
pixelData[colorIndex++] = intensity; // Green
pixelData[colorIndex++] = intensity; // Red
++colorIndex;
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixelData, stride);
}
}
private ImageSource ToBitmap3(InfraredFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
ushort[] infraredData = new ushort[width * height];
byte[] pixelData = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel + 7) / 8];
frame.CopyFrameDataToArray(infraredData);
int colorIndex = 0;
for (int infraredIndex = 0; infraredIndex < infraredData.Length; ++infraredIndex)
{
ushort ir = infraredData[infraredIndex];
byte intensity = (byte)(ir >> 8);
pixelData[colorIndex++] = intensity; // Blue
pixelData[colorIndex++] = intensity; // Green
pixelData[colorIndex++] = intensity; // Red
++colorIndex;
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
}
}