在UWP C#中从PixelBuffer转换为图像(仅使用Windows.UI.Xaml)

时间:2017-05-05 08:29:16

标签: c# image uwp buffer pixel

大家好我想将像素缓冲区转换为图像并打印出来。这是我在程序中的信息: PixelBuffer:int width,int height,IntPtr buffer,stride。这是我因为一些装配错误而无法做到的:

poll()

没有System.Windowxs.Media.Imaging有没有办法做到这一点?仅使用Windows.UI.Xaml?

谢谢!

1 个答案:

答案 0 :(得分:1)

  

我想将像素缓冲区转换为图像并打印出来。

我不确定你的像素缓冲区是什么来的,因为你的代码只显示来自pr obejct。在uwp应用程序中 如果从Writeable​Bitmap实例获得它,则可能不需要首先获取像素缓冲区,只需将WriteableBitmap设置为图像的source即可。 Bitmap​ImageWriteableBitmap都可以作为图片来源,而不仅仅是BitmapImage

StorageFile imagefile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/caffe1.jpg"));
WriteableBitmap writeableimage;
using (IRandomAccessStream stream = await imagefile.OpenAsync(FileAccessMode.Read))
{
    SoftwareBitmap softwareBitmap;
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
    softwareBitmap = await decoder.GetSoftwareBitmapAsync();
    writeableimage = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight);
    writeableimage.SetSource(stream);
}
Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
image.Source = writeableimage;
stackPanel.Children.Add(image);

如果不是WriteableBitmap,则可能需要创建一个WriteableBitmap,其中包含已知的宽度,高度和写入像素数据。

StorageFile imagefile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/caffe2.jpg"));
int width;
int height;
byte[] Inptrbuffer;
using (IRandomAccessStream stream = await imagefile.OpenAsync(FileAccessMode.Read))
{
    SoftwareBitmap softwareBitmap;
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
    softwareBitmap = await decoder.GetSoftwareBitmapAsync();
    width = softwareBitmap.PixelWidth;
    height = softwareBitmap.PixelHeight;
    Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
    Inptrbuffer = pixelData.DetachPixelData(); 
}
WriteableBitmap newfrompixel=new WriteableBitmap(width,height);
using (Stream stream = newfrompixel.PixelBuffer.AsStream())
{
    await stream.WriteAsync(Inptrbuffer, 0, Inptrbuffer.Length);
} 
Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image(); 
image.Source = newfrompixel; 
stackPanel.Children.Add(image);

如果您需要在呈现前修改图片,请参阅Create, edit, and save bitmap images