Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity texture to XAML Image

Discussion in 'Windows' started by DoomSamurai, Sep 5, 2017.

  1. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Hey all!

    I'm trying to load a texture from Unity's Resources folder and use it to create a XAML Image control. I'm doing this by loading the raw bytes from the Unity texture and sending the bytes in a method like this :

    Code (CSharp):
    1. private async void CreateXAMLImage(byte[] bytes, int width, int height, Canvas canvas)
    2. {
    3.     WriteableBitmap bitmap = new WriteableBitmap(width, height);
    4.     Stream stream = bitmap.PixelBuffer.AsStream();
    5.     await stream.WriteAsync(bytes, 0, bytes.Length);
    6.  
    7.     Image image = new Image();
    8.     image.Source = bitmap;
    9.    
    10.     canvas.Children.Add(image);
    11. }
    Here, Canvas is the XAML Canvas, not Unity Canvas.

    My issue is that this code generates oddities in the rendered texture. For example, this texture for a red button (white is transparent) :



    appears like this in XAML :



    In the actual texture I'm using in my project I have 3 single pixel wide lines that rise up from the top of the button, instead of having a red semi-transparent background.

    Anyone has an idea on what can be causing this? Is there a better way to add dynamically some images in XAML?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Could it be XAML is using different pixel format, like ARGB instead of RGBA?
     
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Yeah it is BGRA32 so I only need to invert R and B channels. What I use to get the pixel data from the Unity texture is actually GetPixels32 so from there I build a byte array and populate with BGRA32 format. I also invert the lines for each pixel because or some reason, the Image displays the texture upside down

    After those two operations I am left with the issue you can see above. I can post more code samples tomorrow but i gotta say when I load this new byte array I just created in a new Texture2D, I can see that it is the original texture upside down so it looks like my byte array is correct? Why would XAML render it differently?
     
    Last edited: Sep 6, 2017
  4. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Apparently I need to set the R, G and B channels to 0 when the alpha value is zero or else XAML displays some colors even if the alpha is set to zero. I'm not sure why there are non-zero values in these channels in the first place. I created my original texture with GIMP by creating a transparent background and drawing a circle in the center. There should not be any color in the surrounding pixels.