Search code examples
c#imageuwpbufferpixel

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


Hi all I want to convert pixel buffer to image and print it. This is information that I have in my program: PixelBuffer: int width, int height, IntPtr buffer, stride. This is how I cannot do it because of some assembly errors:

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)

Is there any way to do this without System.Windowxs.Media.Imaging? Using only Windows.UI.Xaml ?

Thanks!


Solution

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

    I'm not sure what your pixel buffer come from since your code only shows getting from pr obejct. In uwp app if you got it from Writeable​Bitmap instance, you may not need to get pixel buffer firstly, just set the WriteableBitmap as the source of image. Both Bitmap​Image and WriteableBitmap can be the image source, not only 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);
    

    If it doesn't from WriteableBitmap, you may need to create a WriteableBitmap with the known width, height, and write pixel data to it.

    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);
    

    If you need to edit the image before present it, please reference Create, edit, and save bitmap images.