Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapImage to byte array and vice versa

I am developing an image processing app in uwp windows 10. I am opening an image using file picker as shown below.

FileOpenPicker openPicker = new FileOpenPicker();           
           openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
           openPicker.ViewMode = PickerViewMode.Thumbnail;
           openPicker.FileTypeFilter.Clear();
           openPicker.FileTypeFilter.Add(".bmp");
           openPicker.FileTypeFilter.Add(".png");
           openPicker.FileTypeFilter.Add(".jpeg");
           openPicker.FileTypeFilter.Add(".jpg");
           StorageFile file = await openPicker.PickSingleFileAsync();
            if(file!=null)
            {
                IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(fileStream);
                myImage.Source = bitmapImage;
               
                // code to retrieve bytes of bitmap image
            }

Inside above if statement, I am retrieving bytes from this image like shown below.

//Fetching pixel data
            using (IRandomAccessStream fileStreams = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStreams);
                BitmapTransform transform = new BitmapTransform()
                {
                    ScaledWidth = Convert.ToUInt32(bitmapImage.PixelWidth),
                    ScaledHeight = Convert.ToUInt32(bitmapImage.PixelHeight)
                };
                PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Rgba8,
                    BitmapAlphaMode.Straight,
                    transform,
                    ExifOrientationMode.IgnoreExifOrientation,// This sample ignores Exif orientation
                    ColorManagementMode.DoNotColorManage
               );

                // byte[] , a global variable
                sourcePixels = pixelData.DetachPixelData();
                // uint , a global variable
                width = decoder.PixelWidth;
                // uint , a global variable
                height = decoder.PixelHeight;
            }
               

Now I need to manipulate this byte array for generating different effects. But for testing purpose, I am converting this byte array, again to bitmapimage and setting its value to main image source (in button click event). but it is not working correctly as

 WriteableBitmap scaledImage = new WriteableBitmap((int)width, (int)height);
            using (Stream stream = scaledImage.PixelBuffer.AsStream())
            {
                await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length);
                myImage.Source = scaledImage;
            }

when the image was opened, it was like this.

enter image description here

when applied again after changing it to byte array and byte array to image source. It changes the image colors, although i haven't changed any values of byte array.

enter image description here

Where is the problem?? Whether the conversion to byte array is wrong or conversion of byte array to bitmap?

like image 911
Umair Jameel Avatar asked Jun 25 '26 00:06

Umair Jameel


1 Answers

Solution:

Well, I have found the solution of this issue, it was that I was using BitmapPixelFormat.Rgba8 in PixelDataProvider (while fetching pixels data). Rather I should use BitmapPixelFormat.Bgra8.

like image 62
Umair Jameel Avatar answered Jun 27 '26 15:06

Umair Jameel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!