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

标签 c# image uwp buffer pixel

大家好,我想将像素缓冲区转换为图像并进行打印。这是我在我的程序中的信息: 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 得到的例如,您可能不需要先获取像素缓冲区,只需将 WriteableBitmap 设置为 source图像。两者 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 .

关于c# - 在 UWP C# 中从 PixelBuffer 转换为图像(仅使用 Windows.UI.Xaml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43800119/

相关文章:

android - 通过相机获取最近拍摄的图像的路径和图像名称

javascript - react native 图像可拖动裁剪

xaml - VC++ Converter 不是命名空间的成员

c# - 选择列表中特定元素位于列表首位的列表子集

c# - 分析代码覆盖消失

c# - 如何使用隐藏的 CMD 从 C# 运行 CMD

javascript - 如何使用javascript检查图像url是否为404

c# - UWP CommandBar 底部有空隙

c# - 使用 UWP 将信息保存到 SOAP

C# 类与其他 .NET 语言互操作