我有一些我在网上某处找到的代码。
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); }
}
上面的代码给了我一个有效的位图,但是我正在使用 WPF 并希望它在 BitmapImage 中
没有经过这个过程,我尝试了代码
byte[] ptr = ....
Marshal.Copy(pFrame, ptr , 0, ptr .Length);
BitmapImage aBitmapImage = new BitmapImage();
aBitmapImage.BeginInit();
aBitmapImage.StreamSource = new MemoryStream(ptr); //FLastImageMemStream;//
aBitmapImage.EndInit();
这不起作用...
我也试过
System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
System.Windows.Media.PixelFormats.Rgb24, null, bufferPtr,linesize * height,
width * 3 ));
这也没有给我一个看起来的图像(在将它分配给图像的 Source 属性之后)
谁能给我任何提示?谢谢艾伦