Reputation: 1673
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.
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.
Where is the problem?? Whether the conversion to byte array is wrong or conversion of byte array to bitmap?
Upvotes: 3
Views: 1743
Reputation: 1673
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.
Upvotes: 1