Merge two images with WinRT?

Sep 20, 2012 at 7:13 AM

Ist it possible to merge two pictures within WinRT?

I would like to use ona as background and a .png to be shown over that.

Coordinator
Sep 21, 2012 at 10:52 AM

 

Look into the Blit .method. You will find a couple of samples in the code repository that use it.

Sep 26, 2012 at 5:44 PM

Hi,

I got it working in WPF with Blit. But I get an exception withing WinRT.

AccessViolationException

Here my example:

 

            FileOpenPicker openPicker1 = new FileOpenPicker();
            openPicker1.ViewMode = PickerViewMode.Thumbnail;
            openPicker1.SuggestedStartLocation = PickerLocationId.Desktop;
            openPicker1.FileTypeFilter.Add(".jpg");
            openPicker1.FileTypeFilter.Add(".jpeg");
            openPicker1.FileTypeFilter.Add(".png");
            openPicker1.FileTypeFilter.Add(".bmp");

            WriteableBitmap pictureOne = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelWidth);
            StorageFile file1 = await openPicker1.PickSingleFileAsync();
            using (var chosenFile1 = await file1.OpenAsync(FileAccessMode.ReadWrite))
            {
                pictureOne.SetSource(chosenFile1);
            }

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".bmp");

            WriteableBitmap picture2 = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelWidth);
            StorageFile file = await openPicker.PickSingleFileAsync();
            using (var chosenFile = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                picture2.SetSource(chosenFile);
            }

            pictureOne.Blit(new Rect(0, 0, picture2.PixelWidth, picture2.PixelHeight), picture2, new Rect(0, 0, picture2.PixelWidth, picture2.PixelHeight), WriteableBitmapExtensions.BlendMode.Multiply); //draw the frame

            FileSavePicker picker = new FileSavePicker();
            picker.FileTypeChoices.Add("PNG File", new List<string>() { ".png" });
            StorageFile fileToSave = await picker.PickSaveFileAsync();

            using (IRandomAccessStream stream = await fileToSave.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                Stream pixelStream = pictureOne.PixelBuffer.AsStream();
                byte[] pixels = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)pictureOne.PixelWidth, (uint)pictureOne.PixelHeight, 96.0, 96.0, pixels);
                await encoder.FlushAsync();
            }

 

 

Jan 12, 2013 at 3:45 PM

Did you ever figure this out? I am getting the same exception. Only seems to fail for me in WinRT.

Jan 12, 2013 at 3:52 PM

No sorry, did not get it work.

Coordinator
Jan 12, 2013 at 6:09 PM
Edited Jan 12, 2013 at 6:10 PM

An AccessViolationEx is usually a sign that you are accesing non allocated memory. In the above code you use the SetSource , this one does not ensure that the image content was loaded after the next call. That's why the Blit call fails. I would recommend to use WBX awaitable' FromStream method to fill the WBs with the image data. There's a reason why I wrote the FromStream method. ;)

 

// The assign is important.

wb1 = await wb1.FromStream(myStream);

 

-rene