public static Image GetSpriteZezenia(string file, int spriteId) // From TibiaAPI
{
#region variables
Bitmap bitmap = new Bitmap(1,1);
UInt32 sprites = 0;
UInt16 width = 0;
UInt16 height = 0;
UInt32 dataLength = 0;
UInt32 total = 0;
UInt32 processed = 0;
byte v1 = 0;
byte v2 = 0;
byte v3 = 0;
byte v4 = 0;
byte px_r;
byte px_g;
byte px_b;
byte px_a;
int x = 0;
int y = 0;
int W = 0;
int H = 0;
long aux = 0;
#endregion
long mover;
long move_aux;
using (BinaryReader reader = new BinaryReader(File.OpenRead(file)))
{
try
{
mover = 5;
reader.BaseStream.Seek(mover, SeekOrigin.Begin);
long allspr = reader.ReadUInt16();
reader.ReadUInt16();
for(int k =0;k<spriteId;k++)
{
reader.BaseStream.Seek(4, SeekOrigin.Current);
reader.BaseStream.Seek(move_aux = reader.ReadUInt32(), SeekOrigin.Current);
}
//mover = 5 + 4 + 2+ 2 + 4 + 4096 +2 +2 + 4 +262144 +2 +2 +4 + 16777216 + 2+ 2 + 4 + 4096;
//reader.BaseStream.Seek(mover, SeekOrigin.Begin);
//sprites = reader.ReadUInt32();
x = 0;
y = 0;
//inicia con el primero
width = reader.ReadUInt16();
height = reader.ReadUInt16();
dataLength= reader.ReadUInt32();
bitmap = new Bitmap(width,height);
W = Convert.ToInt16(width);
H = Convert.ToInt16(height);
total = Convert.ToUInt32(W * H * 4);
processed = 0;
#region while
while (dataLength > 0)
{
v1= reader.ReadByte();
v2 = reader.ReadByte();
// if the first pixel data has a 0x1 value we need to add v2 times of transparent pixels
//&& dataLength < total
if (v1 == 0x1)
{
// transparent pixel
px_r = 0x00;
px_g = 0x00;
px_b = 0x00;
px_a = 0x00;
for (int i = 0; i < v2; i++)
{
// saving in the bmp file
if(x >= width){x = 0; y = y+1;}
bitmap.SetPixel(x,y,Color.FromArgb(px_a, px_r, px_g, px_b));
x++;
processed += 4;
}
// update dataLenght left to process and offset
dataLength -= 2 * sizeof(byte);
}
// read the other 2 bytes and write the pixel rgba data
else
{
// reading the last 2 bytes of a pixel
v3= reader.ReadByte();
v4 = reader.ReadByte();
// case for the background image of the client it has a reserved size of 2048x2048, but the size of the image is less than this resolution
// so you can read until you find 0x0 0x0 0x0 0x0 for optimization
if (v1 == 0x0 && v2 == 0x0 && v3 == 0x0 && v4 == 0x0)
{
dataLength -= 4 * sizeof(byte);
// skipping all the zeros
reader.BaseStream.Seek(dataLength, SeekOrigin.Begin);
break;
}
// populate the pixel struct
px_r = v1;
px_g = v2;
px_b = v3;
px_a = v4;
// saving in the bmp file
if(x >= width){x = 0; y = y+1;}
bitmap.SetPixel(x,y,Color.FromArgb(px_b, px_g, px_r));
x++;
processed += 4;
// update processed/offset/datalength
dataLength -= 4 * sizeof(byte);
}
}//end while
#endregion
#region fill the rest
// fill the rest of sprite with transparent pixels
if (processed < total)
{
// creating the transparent pixel
px_r = 0x00;
px_g = 0x00;
px_b = 0x00;
px_a = 0x00;
for (int i = 0; i < total - processed; i++)
{
// write pixel in the bmp
if(x >= width){x = 0; y = y+1;}
bitmap.SetPixel(x,y,Color.FromArgb(px_a, px_r, px_g, px_b));
x++;
}
}
#endregion
}
catch {}
}
bitmap.Save(spriteId + ".png");
return bitmap;
}