I have some code I found somewhere on the Net.
unsafe static Bitmap SaveFrame(IntPtr pFrame, int width, int height)
{
try
{
int x, y;
int linesize = width * 3;
byte* scan0 = (byte*)Marshal.ReadIntPtr(pFrame);
IntPtr bufferPtr = Marshal.AllocHGlobal(linesize * height);
byte* buffer = (byte*)bufferPtr;
for (y = 0; y < height; y++)
{
for (x = 0; x < linesize; x = x + 3)
{
*buffer++ = (byte)*(scan0 + y * linesize + x + 2);
*buffer++ = (byte)*(scan0 + y * linesize + x + 1);
*buffer++ = (byte)*(scan0 + y * linesize + x);
}
}
Bitmap b = new Bitmap(width, height, linesize, System.Drawing.Imaging.PixelFormat.Format24bppRgb, bufferPtr);
return b;
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
This above code gives me a valid Bitmap, However I'm using WPF and want it to be in a BitmapImage
Without going through this process, Im trying the code
byte[] ptr = ....
Marshal.Copy(pFrame, ptr , 0, ptr .Length);
BitmapImage aBitmapImage = new BitmapImage();
aBitmapImage.BeginInit();
aBitmapImage.StreamSource = new MemoryStream(ptr); //FLastImageMemStream;//
aBitmapImage.EndInit();
which does not work...
I have also tried
System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
System.Windows.Media.PixelFormats.Rgb24, null, bufferPtr,linesize * height,
width * 3 ));
which also does not give me an image it seems (after assigning it to the Source property of Image)
Can anyone give me any hints? Thanks Allen