I have to make parallel work (of course async Task) that blur images selected from folder, so I have found this (BitmapImage), but the problem is what i do not know how to access bytes or pixels of this object, so that I can change them, blur or something else:
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.FileTypeFilter.Add(".jpg");
folderPicker.FileTypeFilter.Add(".jpeg");
folderPicker.FileTypeFilter.Add(".png");
var folder = await folderPicker.PickSingleFolderAsync();
var filesList = await folder.GetFilesAsync();
for (int i = 0; i < filesList.Count; i++)
{
using (var stream = await filesList[i].OpenAsync(Windows.Storage.FileAccessMode.Read))
{
//Here I will use some array of BitmapImage
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
//show Image on Form
imageSourceForm.Source = bitmapImage;
}
}
So is there any way to get pixels or bytes of this object, or maybe do you know what to use that I can do work, but with async Task. The most important part is that UI Thread has to be responsive, while the images are blurring. Thanks in advance.
You could use the following code to get bytes of an image:
var random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(filesList[i]).OpenReadAsync();
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
//Access pixel buffer in such a way
byte[] bytes = pixelData.DetachPixelData();
Related
I'm using this code to write a Byte Array inside a file BMP:
private async void ScriviBMP()
{
using (Stream stream = immagineBitmap.PixelBuffer.AsStream())
{
await stream.WriteAsync(arrayImmagine, 0, arrayImmagine.Length);
}
StorageFolder folder = KnownFolders.PicturesLibrary;
if (folder != null)
{
StorageFile file = await folder.CreateFileAsync("area2_128x128" + ".bmp", CreationCollisionOption.ReplaceExisting);
using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, storageStream);
var pixelStream = immagineBitmap.PixelBuffer.AsStream();
var pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)immagineBitmap.PixelWidth, (uint)immagineBitmap.PixelHeight, 48, 48, pixels);
await encoder.FlushAsync();
}
}
}
Then i'm using this code to display the BMP image in a Image object
private async void VisBMP()
{
var file = await KnownFolders.PicturesLibrary.GetFileAsync("area2_128x128.bmp");
using (var fileStream = (await file.OpenAsync(Windows.Storage.FileAccessMode.Read)))
{
var bitImg = new BitmapImage();
//bitImg.UriSource = new Uri(file.Path);
bitImg.SetSource(fileStream);
image.Source = bitImg;
}
}
these functions take about 400 milliseconds to complete the process, that's a lot of time.
Is there a way to avoid the usage of a BMP file and use only a stream to display the image on the image object?
It can be that debugging the program can slow the processes? I'm using Visual Studio 2015.
You can transfer the data buffer(arrayImmagine) to image in InMemoryRandomAccessStream.These codes take about 200ms.I tested with following pieces of code. In addition, you can reference this article to get more information.
BitmapImage biSource = new BitmapImage();
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await biSource.SetSourceAsync(stream);
}
image.Source = biSource;
I have an image that's loaded in XAML using a converter. Rather than load this image again, I want to take that image and find the dominant colour to be able to use for other graphics on the page. So far I have this:
var himage = (BitmapImage)image_home.Source;
using (var stream = await himage.OpenReadAsync()) //**can't open himage this way**
{
//Create a decoder for the image
var decoder = await BitmapDecoder.CreateAsync(stream);
//Create a transform to get a 1x1 image
var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
//Get the pixel provider
var pixels = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Ignore,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
//Get the bytes of the 1x1 scaled image
var bytes = pixels.DetachPixelData();
//read the color
var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
}
Obviously I can't open the BitmapImage himage using OpenReadAsync, what do I need to do there to be able to achieve this?
BitmapDecoder requires RandomAccessStream object to create a new instance. BitmapImage may not be directly extract as RandomAccessStream unless you know the original source. According to your comment, you are binding image Uri to the image control, so you could know the original source and you can get the RandomAccessStream from the BitmapImage's UriSource property by RandomAccessStreamReference class, you don't need load the Image again. Code as follows:
var himage = (BitmapImage)image_home.Source;
RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(himage.UriSource);
using (IRandomAccessStream stream = await random.OpenReadAsync())
{
//Create a decoder for the image
var decoder = await BitmapDecoder.CreateAsync(stream);
...
//read the color
var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
}
I am working on a Windows 8 Metro app that applies filters to images. I have a web version of the app and wanted to port it. But as we all know WinRT doesn't have all the good things .NET provides otherwise :/
Currently I am applying the filters on a byte array and I want to keep it that way, because it's super fast! So for the past few days I have been searching for ways to convert a StorageFile to byte[] and then byte[] to BitmapImage.
So far I have managed to do the first one (StorageFile to byte[]). Here is how I do it:
public async Task<Byte[]> ImageFileToByteArray(StorageFile file)
{
IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
return pixelData.DetachPixelData();
}
This piece of code returns a byte[] that contains the pixel data as BGRA.
And here comes the tricky part. I cannot successfully convert the byte array into a BitmapImage. I have searched all over the places and many people suggest using WriteableBitmap but that doesn't do much good to me. I have also found some pieces of code that should be working... but they don't.
One of the solutions I have tried is using InMemoryRandomAccessStream like this:
public async Task<BitmapImage> ByteArrayToBitmapImage(Byte[] pixels)
{
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(pixels.AsBuffer());
stream.Seek(0);
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
This one throws the following exception:
An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code
Additional information: The component cannot be found. (Exception from HRESULT: 0x88982F50)
I tried using this line instead:
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
But it did me no good since I keep getting that exception.
I have also tried this:
var bitmapImage = new BitmapImage();
var pixels = await ImageFileToByteArray(file);
ImageSource imgSource;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(pixels);
await writer.StoreAsync();
}
await bitmapImage.SetSourceAsync(ms);
imgSource = bitmapImage;
}
And get the same exception as the first piece of code.
I have also tried several other ways that include using a normal Stream then converting into a IRandomAccessStream but they didn't work either.
All of the above code seems fine to me. So my guess at the moment is that the problem is in the byte[]. I'm guessing that the format of the pixelData inside is not valid, so I tried changing it to RGBA but that didn't help either. Also the PixelHeight and PixelWidth of the BitmapImage are 0.
This is working for me,
private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
{
var bitmapImage = new BitmapImage();
var stream = new InMemoryRandomAccessStream();
await stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
bitmapImage.SetSource(stream);
return bitmapImage;
}
this is my first answer..hope it will help.
I had the exact same problem and I spand more then 6 hours trying to figur this out.
this is what i came up with:
what you said was right. there are 2 waye to convert image to byteArray:
First aproach(yours)
public async Task<byte[]> ImageFileToByteArrayAsync(StorageFile file)
{
IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
return pixelData.DetachPixelData();
}
Second aproach
public async Task<byte[]> ImageFileToByteArrayAsync(StorageFile file)
{
var inputStream = await file.OpenSequentialReadAsync();
var readStream = inputStream.AsStreamForRead();
var buffer = new byte[readStream.Length];
await readStream.ReadAsync(buffer, 0, buffer.Length);
return buffer;
}
if youll use the second aproach to decode the pic, with no Pixel, this converter will work:
public class ByteArrayToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null || !(value is byte[]))
return null;
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(stream.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])value);
writer.StoreAsync().GetResults();
}
BitmapImage image = new BitmapImage();
image.SetSource(stream);
return image;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
for the first aproach youll need to work with WriteableBitmap as you said.
With the following code I can get a IRandomAccessStreamWithContentType representing the Thumbal of a contact
var contactPicker = new ContactPicker();
contactPicker.SelectionMode = ContactSelectionMode.Contacts;
var contact = await contactPicker.PickSingleContactAsync();
var thumb = await contact.GetThumbnailAsync();
But what is the best way to handle the IRandomAccessStreamWithContentType to get a image instance and a imagefile ?
You should just handle it as an ordinary IRandomAccessStream containing the image:
using (var stream = await contact.GetThumbnailAsync())
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
BitmapFrame frame = await decoder.GetFrameAsync(0);
var bitmap = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight);
stream.Seek(0);
await bitmap.SetSourceAsync(stream);
}
From here on you can set the WriteableBitmap as asource to Image control or use it in any other way.
The Seek call is required because the stream is already read to get the image size so the position needs to be reset before reading the image itself.
I am working on an application for working with photos for Windows 8 Metro on C#. And now I'm faced with a strange problem.
First, I select the file over PickSingleFileAsync, then trying to get a thumbnail via GetThumbnailAsync:
FileOpenPicker openPicker = new FileOpenPicker ();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add (". jpg");
openPicker.FileTypeFilter.Add (". jpeg");
openPicker.FileTypeFilter.Add (". png");
StorageFile file = await openPicker.PickSingleFileAsync ();
if (file! = null)
{
BitmapImage bitmapImage = new BitmapImage ();
var thumb = await file.GetThumbnailAsync (ThumbnailMode.PicturesView, 150, ThumbnailOptions.UseCurrentScale);
if (thumb! = null) bitmapImage.SetSource (thumb);
}
This code is executed for files on my HDD or for any MTP-devices (tested camera and Android Tablet), but not for the iPhone. This part
var thumb = await file.GetThumbnailAsync (ThumbnailMode.PicturesView, 150, ThumbnailOptions.UseCurrentScale);
Performed for about 30 seconds, and returns null.
This code
BitmapImage bitmapImage = new BitmapImage ();
var stream = await file.OpenAsync (FileAccessMode.Read);
if (stream! = null) bitmapImage.SetSource (stream);
However, this is a complete picture, and I need a thumbnail. I tried to get it by changing the image size.
public static async Task <InMemoryRandomAccessStream> Resize (StorageFile file, uint height, uint width)
{
var fileStream = await file.OpenAsync (FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync (fileStream);
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream ();
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync (ras, decoder);
enc.BitmapTransform.ScaledHeight = height;
enc.BitmapTransform.ScaledWidth = width;
await enc.FlushAsync ();
ras.Seek (0);
return ras;
}
And there
BitmapDecoder decoder = await BitmapDecoder.CreateAsync (fileStream);
Exception "The image cannot be decoded" occurs with code 0x88982F61.
FileOpenPicker also does not display thumbnail images on the iPhone.
But standart Photos app for Metro shows all photos on iPhone without problems.
And this leads me to 2 question:
Does anyone have any suggestions to fix this problem?
Maybe, is there a function to retrieve all the thumbnails from device?