Hello Everyone,
I am having a strange issue while trying to render a png texture. I am creating the image.png by reading the screen pixels and applying them to a new texture. I save out the image as a png then read it back in using the WWW class.
Below is how I am reading in the screen:
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (new Rect(0, 0, width, height), 0, 0);
tex.Apply ();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Destroy (tex);
File.WriteAllBytes(dir + "/SavedScreen.png", bytes);
This code works fine and this is the image it saves out:
Here is my code for reading in the png:
public IEnumerator SetTexture(string screenShot, GameObject _go)
{
if(screenShot.Length != 0)
{
//Load in the file path
wwwBack = new WWW(screenShot);
Debug.Log ("www: Screen Texture not set! Time: " + Time.time.ToString ());
// wait until loaded
yield return wwwBack;
//create texture to catch from file
tex = new Texture2D(GridScript.imageWidth, GridScript.imageHeight, TextureFormat.DXT1, false);
// load texture
wwwBack.LoadImageIntoTexture(tex);
//set texture
BackgroundMat.mainTexture = (Texture)Instantiate(tex);
/*if(!isFull)
Destroy (_go);*/
if(!isFull)
{
StartCoroutine (DestroyGameObject (_go));
}
if(openList.Count == 0)
{
isFull = true;
}
screenShot = "";
Debug.Log ("www: Screen Texture set! Time: " + Time.time.ToString ());
wwwBack.Dispose ();
wwwBack = null;
}
}
This code works fine in reading in the image but when it is set it looks like this:
Any ideas as to why the color gets all messed up?
Thanks in advance!