This project is read-only.

Merge two images with WinRT?

Sep 20, 2012 at 8: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.

Sep 21, 2012 at 11: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 6:44 PM


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


Here my example:


            FileOpenPicker openPicker1 = new FileOpenPicker();
            openPicker1.ViewMode = PickerViewMode.Thumbnail;
            openPicker1.SuggestedStartLocation = PickerLocationId.Desktop;

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

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.Desktop;

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

            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 4: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 4:52 PM

No sorry, did not get it work.

Jan 12, 2013 at 7:09 PM
Edited Jan 12, 2013 at 7: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);