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

Converting from PixelBuffer to Image in UWP C# (Using only Windows.UI.Xaml)

大家好,我想将像素缓冲区转换为图像并进行打印。这是我在我的程序中的信息: PixelBuffer:int 宽度,int 高度,IntPtr 缓冲区,步幅。由于一些装配错误,我无法做到这一点:

Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
System.Windows.Media.Imaging.BitmapFrame frame = System.Windows.Media.Imaging.BitmapFrame.Create(System.Windows.Media.Imaging.BitmapSource.Create(
                        (int)pr.pixelBuffer.width,
                        (int)pr.pixelBuffer.height,
                        96,
                        96,
                        System.Windows.Media.PixelFormats.Rgb24,
                        null,
                        pr.pixelBuffer.buffer,
                        (int)(pr.pixelBuffer.stride * pr.pixelBuffer.height),
                        (int)pr.pixelBuffer.stride));
Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(frame.BaseUri);
image.Source = bitmapImage;//frame;
stackPanel.Children.Add(image)

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

谢谢!

I want to convert pixel buffer to image and print it.

我不确定您的像素缓冲区来自什么,因为您的代码只显示从 pr 对象获取。在 uwp 应用程序中 如果你从 Writeable​Bitmap instance, you may not need to get pixel buffer firstly, just set the WriteableBitmap as the source of image. Both Bitmap​Image 得到它并且 WriteableBitmap 可以作为图像来源,而不仅仅是 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