title: unity texture2d EncodeToJPG transparent black
body: <p>I have a texture user drawn(on a mobile device), and I want to encode that to jpg so that user could share with that but I found the tranparent part will be black in the encoded jpg:</p>
  
  <p><a href="https://i.stack.imgur.com/lzFDt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lzFDt.jpg" alt="enter image description here"></a></p>
  
  <p>while I want the jpg to be like this:
  <a href="https://i.stack.imgur.com/HtiVK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HtiVK.jpg" alt="enter image description here"></a></p>
  
  <p>But I don't find any overridden method of <a href="https://docs.unity3d.com/ScriptReference/Texture2D.EncodeToJPG.html" rel="nofollow noreferrer"><code>texture2d.EncodeToJPG()</code></a> to do this.</p>
  
  <p>any ideas?</p>
  
  <h2>NOTE</h2>
  
  <p>the wing of the bird was drawn specifically to <code>Color.white</code> so it could be white in the encoded jpg. </p>
  
  <h2>Approach now</h2>
  
  <p>I finally manage to make this work by:</p>
  
  <pre><code> Color32[] pixels = text.GetPixels32();
   Color blackTransparent = Color.black;
   blackTransparent.a = 0;
   for(int i = 0;i &lt; pixels.Length; i++)
   {
      if(pixels[i] == blackTransparent)
      {
              pixels[i] = Color.white;
       }
    }
    text.SetPixels32(pixels);
    text.Apply();
  </code></pre>
  
  <p>But this will loop through all the pixels of the texture, if someone has a better method, plz let us know.</p>
  
  <h2>Problem with this approach:</h2>
  
  <p>we found that there will be some edge(when meeting with the black line) on the jpg using above code, maybe need some graphic processing knowledge to solve this?
  <a href="https://i.stack.imgur.com/IYSmQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IYSmQ.jpg" alt="enter image description here"></a></p>
  
  <h2>Here is How we load image to texture2d(as required by comment)</h2>
  
  <pre><code>  Texture2D text = new Texture2D(1, 1, TextureFormat.PVRTC_RGBA4, false);
    byte[] imagebytes = null;
    string path = "image/path/sample.png";
    if (System.IO.File.Exists(path))
    {
        Debug.Log(" really load file from " + path);
        imagebytes = System.IO.File.ReadAllBytes(path);
    }
    text.LoadImage(imagebytes, false);
  </code></pre>