I found the DumpARGB32ToDisk in one of the forum threads and was wondering how that worked. (using System.Drawing.dll)
I tried it and it works. I think the Rows have to be Flipped Vertically because GetPixels32 starts reading from bottom left , and Bitmap reads it from top left.
Is this code below correct? Did i miss anything?
...
void Start ()
{
int texSize = 128;
int checkerSize = 64;
//int circles = 10;
Color color1 = new Color( .8f, .3f, .8f, 0.2f );
Color color2 = new Color( .3f, .2f, .3f, 0.2f );
Texture2D tex = new Texture2D(texSize, texSize, TextureFormat.ARGB32, false, true);
tex.alphaIsTransparency = true;
tex.FloodFillArea( texSize, texSize, new Color(0, 0, 0, 0) );
tex.DrawCheckers( texSize, checkerSize, color1, color2 );
int nRectSize = 64;
int nSteps = 16;
tex.DrawGradient_Linear( new Rect(0, 0, nRectSize, nRectSize), nSteps, color1, color2 );
tex.Apply();
renderer.material.mainTexture = tex;
Color32[] pix = tex.GetPixels32();
int cSize = System.Runtime.InteropServices.Marshal.SizeOf( typeof(Color32) );
byte[] pix4 = new byte[ pix.Length * cSize ];
int row, col, rowOffset;
int targetRow, targetRowStart;
int rowPixels = tex.width * cSize;
for( int i = 0; i < pix.Length; )
{
row = i / tex.width;
col = i % tex.width;
targetRow = tex.height - row;
targetRowStart = (targetRow - 1) * rowPixels;
for( int j = targetRowStart; j < targetRowStart + rowPixels; j += cSize, i++ )
{
pix4[j] = pix[i].r;
pix4[j+1] = pix[i].g;
pix4[j+2] = pix[i].b;
pix4[j+3] = pix[i].a;
}
}
DumpARGB32ToDisk( pix4, (uint) tex.width, (uint) tex.height );
}
public static void DumpARGB32ToDisk(byte[] argb, uint width, uint height)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap((int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(argb, 0, bmpData.Scan0, argb.Length);
bmp.UnlockBits(bmpData);
bmp.Save("Assets/_GeneratedImage.png", System.Drawing.Imaging.ImageFormat.Png);
}