Writeablebitmap from UIElement with transparent background (WP and Silverlight)
Asked Answered
U

0

8

Creating an Image from an UIElement is covered in another question. I am however trying to make an image of an UIElement that has the non-filled background kept as transparent. This is currently done in my code like this (NOT working for transparency):

var imageBrush_Final = new ImageBrush
{
    ImageSource = bi,
    Stretch = Stretch.None
};
FinalCanvas.Background.Opacity = 0;
FindChildShieldCanvas_FinalCanvas(FinalCanvas, imageBrush_Final);
FinalCanvas.InvalidateArrange();
FinalCanvas.UpdateLayout();
WriteableBitmap writeableBitmap_Finals = new WriteableBitmap(FinalCanvas, null);
String tempPNG = results.Username + "My_Crest.png";
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream(tempPNG, System.IO.FileMode.OpenOrCreate, isf))
    {
        await Cimbalino.Toolkit.Extensions.WriteableBitmapExtensions.SavePngAsync(writeableBitmap_Finals, imageStream);
    }
}

The image is created is of a canvas that has one shape added inside. The shape is filled with an ImageBrush, specifically imageBrush_Final. The canvas is only partially filled by the internal shape painter that has been painted with imageBrush_Final. I would therefore like that the created writeableBitmap_Finals, is only filled with the shape and the rest is fully transparent.

Does anyone know how I can achieve this, and why is it not saved when the UIElement, i.e. canvas has set its background to Opacity to 0?

Underwent answered 16/10, 2016 at 17:43 Comment(7)
why would you want to change the elements opacity to zero and render the element?Doble
@ChiragShah I had to render the element for the constructor to work properly. The opacity I inserted was to try and force the black background to be transparent. But did not work. If you have another idea I can try let me know :)Underwent
So you are not getting transparent color in your rendered image? I guess saving it As png could be the problem. Anyway, ill try the code.Doble
@ChiragShah no I get no transparency, it is just black. But all other colors are correct, so I did not suspect any image channel issue. Great thx!Underwent
I'm confused if you still want me to try the code.Doble
@ChiragShah if you want to I would appreciate it. Since I do not know how to get the transparency.Underwent
Might want to look here: #16154045Bismuthic

© 2022 - 2024 — McMap. All rights reserved.