Win8 App Store - 如何获得PixelFormat?

时间:2013-10-08 00:06:10

标签: c# image-processing windows-8

在Win8 App Store中,给定一个流,如何从该流中获取图像的PixelFormat?我只能获得byte array, width, height, stride但我还需要像素格式类型(比如每个像素的位数,rgba32 ......)来创建某种BitmapSource

//Copy photo from camera to stream
var fPhotoStream = new InMemoryRandomAccessStream();
await mediaCaptureMgr.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), fPhotoStream);
await fPhotoStream.FlushAsync();
fPhotoStream.Seek(0);

//Convert stream to byte array
byte[] bytes = new byte[fPhotoStream.Size];
await fPhotoStream.ReadAsync(bytes.AsBuffer(), (uint)fPhotoStream.Size, InputStreamOptions.None); 

//Get pointer to byte array
GCHandle pinnedArray = GCHandle.Alloc(bytes, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();

//Get image width, height, stride
BitmapImage bmp = new BitmapImage();
bmp.SetSource(fPhotoStream);            
int imgWidth = bmp.PixelWidth;
int imgHeight = bmp.PixelHeight;
int stride = bmp.Width * 4;

//But how to get image pixel format?
int pixelFormat = ??????

我能这样做吗?

1 个答案:

答案 0 :(得分:0)

我发现我可以使用BitmapDecoder来获取PixelFormat信息: http://msdn.microsoft.com/en-us/library/windows/apps/jj709939.aspx