C\#IFF 도형 구조 해석 코드
// RSB
ImageIFF _Iff = new ImageIFF(@"D:\temp\4.IFF");
pictureBox1.Image = _Iff.Image;
//
ImageIFF _Iff = new ImageIFF();
_Iff.Image = (Bitmap)Image.FromFile(@"d:\TEMP\1.bmp");
_Iff.SaveImage(@"d:\temp\ok.IFF");
전체 코드
/// <summary>
/// IFF
/// [email protected]
/// QQ:116149
/// </summary>
public class ImageIFF
{
private uint m_Heard = 0x4d524f46; //FROM
private uint m_FileSize = 0;
private uint m_FileType = 0x4d424c49; //ILBM
private IList<ImageIFFOfANNO> m_ANNO = new List<ImageIFFOfANNO>();
private ImageIffOfBMHD m_BMHD = new ImageIffOfBMHD();
private ImageIffOfCMAP m_CMAP = new ImageIffOfCMAP();
/// <summary>
///
/// </summary>
private Bitmap m_Bitmap;
/// <summary>
///
/// </summary>
public IList<ImageIFFOfANNO> ANNO { get { return m_ANNO; } set { m_ANNO = value; } }
/// <summary>
///
/// </summary>
public Bitmap Image { get { return m_Bitmap; } set { m_Bitmap = value; } }
public ImageIFF()
{
}
public ImageIFF(string p_FileName)
{
if (!File.Exists(p_FileName)) throw new Exception(" !");
byte[] _Bytes = File.ReadAllBytes(p_FileName);
if (BitConverter.ToInt32(_Bytes, 0) != m_Heard) throw new Exception(" IFF !");
m_FileSize = BytesToUint(_Bytes, 4);
if (m_FileSize != _Bytes.Length - 8) throw new Exception(" !");
if (BitConverter.ToInt32(_Bytes, 8) != m_FileType) throw new Exception(" !");
LoadData(_Bytes, 12);
}
/// <summary>
///
/// </summary>
/// <param name="p_FileBytes"></param>
/// <param name="p_Index"> </param>
private void LoadData(byte[] p_FileBytes, uint p_Index)
{
if (p_Index >= p_FileBytes.Length) return;
uint _ReadIndex = p_Index + 4;
string _TypeText = Encoding.ASCII.GetString(p_FileBytes, (int)p_Index, 4);
switch (_TypeText)
{
case "ANNO": //
m_ANNO.Add(new ImageIFFOfANNO(p_FileBytes, ref _ReadIndex));
break;
case "BMHD": //
m_BMHD = new ImageIffOfBMHD(p_FileBytes, ref _ReadIndex);
break;
case "CMAP": //
m_CMAP = new ImageIffOfCMAP(p_FileBytes, ref _ReadIndex);
break;
case "BODY": //
LoadImage(p_FileBytes, ref _ReadIndex);
break;
}
LoadData(p_FileBytes, _ReadIndex);
}
/// <summary>
///
/// </summary>
/// <param name="p_FileBytes"></param>
/// <param name="p_ReadIndex"></param>
private void LoadImage(byte[] p_FileBytes, ref uint p_ReadIndex)
{
uint _Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Bitmap = new Bitmap(m_BMHD.Width, m_BMHD.Height, m_BMHD.Pixel);
BitmapData _BitmapData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, m_Bitmap.PixelFormat);
byte[] _WriteBytes = new byte[_BitmapData.Stride * _BitmapData.Height];
uint _ReadStride = _Size / (uint)_BitmapData.Height / 8;
int _WriteIndex = 0;
byte[] _ReadBytes = new byte[_Size / (uint)_BitmapData.Height];
int _ReadBitArray = (int)_ReadStride * 8;
switch (m_Bitmap.PixelFormat)
{
default:
throw new Exception(" ");
case PixelFormat.Format8bppIndexed:
for (int i = 0; i != _BitmapData.Height; i++)
{
_WriteIndex = i * _BitmapData.Stride;
Array.Copy(p_FileBytes, p_ReadIndex, _ReadBytes, 0, _ReadBytes.Length); //
p_ReadIndex += (uint)_ReadBytes.Length; //
System.Collections.BitArray _ReadArray = new System.Collections.BitArray(_ReadBytes); //1 true 0 false
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(new byte[] { 0 }); //1 true 0 false
for (int z = 0; z != _BitmapData.Width; z++)
{
int _ReadIndex = ((z / 8) * 8) + 7 - (z % 8); // 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[0] = _ReadArray[_ReadIndex];
_WriteArray[1] = _ReadArray[_ReadBitArray + _ReadIndex];
_WriteArray[2] = _ReadArray[(_ReadBitArray * 2) + _ReadIndex];
_WriteArray[3] = _ReadArray[(_ReadBitArray * 3) + _ReadIndex];
_WriteArray[4] = _ReadArray[(_ReadBitArray * 4) + _ReadIndex];
_WriteArray[5] = _ReadArray[(_ReadBitArray * 5) + _ReadIndex];
_WriteArray[6] = _ReadArray[(_ReadBitArray * 6) + _ReadIndex];
_WriteArray[7] = _ReadArray[(_ReadBitArray * 7) + _ReadIndex];
_WriteArray.CopyTo(_WriteBytes, _WriteIndex + z);
}
}
#region
ColorPalette _Palette = m_Bitmap.Palette;
for (int i = 0; i != m_CMAP.ColorList.Length; i++)
{
_Palette.Entries[i] = m_CMAP.ColorList[i];
}
m_Bitmap.Palette = _Palette;
#endregion
break;
case PixelFormat.Format24bppRgb:
int _Stride = _ReadBitArray / 3; // (X/16)*16+(X%16==0?0:16);
_ReadBitArray = (int)_Size / _BitmapData.Height / 3 * 8; //
for (int i = 0; i != _BitmapData.Height; i++)
{
_WriteIndex = i * _BitmapData.Stride;
Array.Copy(p_FileBytes, p_ReadIndex, _ReadBytes, 0, _ReadBytes.Length); //
p_ReadIndex += (uint)_ReadBytes.Length; //
System.Collections.BitArray _ReadArray = new System.Collections.BitArray(_ReadBytes); //1 true 0 false
System.Collections.BitArray _WriteRedArray = new System.Collections.BitArray(new byte[] { 0 }); //1 true 0 false
System.Collections.BitArray _WriteGreenArray = new System.Collections.BitArray(new byte[] { 0 }); //1 true 0 false
System.Collections.BitArray _WriteBlueArray = new System.Collections.BitArray(new byte[] { 0 }); //1 true 0 false
for (int z = 0; z != _BitmapData.Width; z++)
{
int _ReadIndex = ((z / 8) * 8) + 7 - (z % 8); // 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteRedArray[0] = _ReadArray[_ReadIndex];
_WriteRedArray[1] = _ReadArray[_Stride + _ReadIndex];
_WriteRedArray[2] = _ReadArray[(_Stride * 2) + _ReadIndex];
_WriteRedArray[3] = _ReadArray[(_Stride * 3) + _ReadIndex];
_WriteRedArray[4] = _ReadArray[(_Stride * 4) + _ReadIndex];
_WriteRedArray[5] = _ReadArray[(_Stride * 5) + _ReadIndex];
_WriteRedArray[6] = _ReadArray[(_Stride * 6) + _ReadIndex];
_WriteRedArray[7] = _ReadArray[(_Stride * 7) + _ReadIndex]; //
_WriteGreenArray[0] = _ReadArray[_ReadBitArray + _ReadIndex];
_WriteGreenArray[1] = _ReadArray[_ReadBitArray + _Stride + _ReadIndex];
_WriteGreenArray[2] = _ReadArray[_ReadBitArray + (_Stride * 2) + _ReadIndex];
_WriteGreenArray[3] = _ReadArray[_ReadBitArray + (_Stride * 3) + _ReadIndex];
_WriteGreenArray[4] = _ReadArray[_ReadBitArray + (_Stride * 4) + _ReadIndex];
_WriteGreenArray[5] = _ReadArray[_ReadBitArray + (_Stride * 5) + _ReadIndex];
_WriteGreenArray[6] = _ReadArray[_ReadBitArray + (_Stride * 6) + _ReadIndex];
_WriteGreenArray[7] = _ReadArray[_ReadBitArray + (_Stride * 7) + _ReadIndex]; //
_WriteBlueArray[0] = _ReadArray[_ReadBitArray * 2 + _ReadIndex];
_WriteBlueArray[1] = _ReadArray[_ReadBitArray * 2 + _Stride + _ReadIndex];
_WriteBlueArray[2] = _ReadArray[_ReadBitArray * 2 + (_Stride * 2) + _ReadIndex];
_WriteBlueArray[3] = _ReadArray[_ReadBitArray * 2 + (_Stride * 3) + _ReadIndex];
_WriteBlueArray[4] = _ReadArray[_ReadBitArray * 2 + (_Stride * 4) + _ReadIndex];
_WriteBlueArray[5] = _ReadArray[_ReadBitArray * 2 + (_Stride * 5) + _ReadIndex];
_WriteBlueArray[6] = _ReadArray[_ReadBitArray * 2 + (_Stride * 6) + _ReadIndex];
_WriteBlueArray[7] = _ReadArray[_ReadBitArray * 2 + (_Stride * 7) + _ReadIndex]; //
_WriteRedArray.CopyTo(_WriteBytes, _WriteIndex + z * 3 + 2);
_WriteGreenArray.CopyTo(_WriteBytes, _WriteIndex + z * 3 + 1);
_WriteBlueArray.CopyTo(_WriteBytes, _WriteIndex + z * 3);
}
}
break;
}
Marshal.Copy(_WriteBytes, 0, _BitmapData.Scan0, _WriteBytes.Length);
m_Bitmap.UnlockBits(_BitmapData);
}
/// <summary>
///
/// </summary>
/// <param name="p_File"></param>
public void SaveImage(string p_File)
{
if (m_Bitmap == null) return;
m_ANNO.Add(new ImageIFFOfANNO("IFF-File C#ImageIFF [email protected]"));
System.IO.File.WriteAllBytes(p_File, GetFileBytes());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private byte[] GetFileBytes()
{
MemoryStream _Stream = new MemoryStream();
_Stream.Write(BitConverter.GetBytes(m_Heard), 0, 4);
_Stream.Write(new byte[] { 0, 0, 0, 0 }, 0, 4);
_Stream.Write(BitConverter.GetBytes(m_FileType), 0, 4);
byte[] _WriteBytes = new byte[0];
for (int i = 0; i != m_ANNO.Count; i++)
{
_WriteBytes = m_ANNO[i].GetBytes();
_Stream.Write(_WriteBytes, 0, _WriteBytes.Length);
}
_WriteBytes = m_BMHD.GetBytes(m_Bitmap);
_Stream.Write(_WriteBytes, 0, _WriteBytes.Length);
switch (m_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Stream.Write(new byte[] { 0x43, 0x4D, 0x41, 0x50 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((uint)768), 0, 4);
for (int i = 0; i != 256; i++)
{
_Stream.Write(new byte[] { m_Bitmap.Palette.Entries[i].R, m_Bitmap.Palette.Entries[i].G, m_Bitmap.Palette.Entries[i].B }, 0, 3);
}
break;
default:
break;
}
byte[] _ImageBytes = SaveImage();
_Stream.Write(new byte[] { 0x42, 0x4F, 0x44, 0x59 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((uint)_ImageBytes.Length), 0, 4);
_Stream.Write(_ImageBytes, 0, _ImageBytes.Length);
m_FileSize = (uint)_Stream.Length - 8;
_Stream.Position = 4;
_Stream.Write(ImageIFF.UintToBytes(m_FileSize), 0, 4);
return _Stream.ToArray();
}
private byte[] SaveImage()
{
Bitmap _NewBitmap;
BitmapData _Data;
byte[] _ReadBytes;
int _ReadStride = 0;
int _WriteBitArray = 0;
int _Stride = 0;
int _WidthOne = 0;
int _WidthAll = 0;
byte[] _WriteBytes;
byte[] _WriteRowBytes;
int _ReadIndex = 0;
int _WriteIndex = 0;
switch (m_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Data = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
_ReadBytes = new byte[_Data.Stride * _Data.Height];
_ReadStride = _Data.Stride;
Marshal.Copy(_Data.Scan0, _ReadBytes, 0, _ReadBytes.Length);
m_Bitmap.UnlockBits(_Data);
_WidthOne = (m_Bitmap.Width / 16) * 16 + (m_Bitmap.Width % 16 == 0 ? 0 : 16);
_WidthAll = _WidthOne * m_Bitmap.Height;
_WriteBytes = new byte[_WidthAll];
_WriteRowBytes = new byte[_WidthOne];
_WriteBitArray = _WidthOne;
for (int i = 0; i != m_Bitmap.Height; i++)
{
_ReadIndex = i * _ReadStride;
Array.Copy(_WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes, 0, _WriteRowBytes.Length);
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(_WriteRowBytes);
for (int z = 0; z != m_Bitmap.Width; z++)
{
System.Collections.BitArray _ColorArray = new System.Collections.BitArray(new byte[] { _ReadBytes[_ReadIndex + z] });
_WriteIndex = ((z / 8) * 8) + 7 - (z % 8); // 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[_WriteIndex] = _ColorArray[0];
_WriteArray[_WriteBitArray + _WriteIndex] = _ColorArray[1];
_WriteArray[(_WriteBitArray * 2) + _WriteIndex] = _ColorArray[2];
_WriteArray[(_WriteBitArray * 3) + _WriteIndex] = _ColorArray[3];
_WriteArray[(_WriteBitArray * 4) + _WriteIndex] = _ColorArray[4];
_WriteArray[(_WriteBitArray * 5) + _WriteIndex] = _ColorArray[5];
_WriteArray[(_WriteBitArray * 6) + _WriteIndex] = _ColorArray[6];
_WriteArray[(_WriteBitArray * 7) + _WriteIndex] = _ColorArray[7];
}
_WriteArray.CopyTo(_WriteRowBytes, 0);
Array.Copy(_WriteRowBytes, 0, _WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes.Length);
}
return _WriteBytes;
break;
default:
_NewBitmap = new Bitmap(m_Bitmap.Width, m_Bitmap.Height, PixelFormat.Format24bppRgb);
Graphics _Graphics = Graphics.FromImage(_NewBitmap);
_Graphics.DrawImage(m_Bitmap, new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height));
_Graphics.Dispose();
_Data = _NewBitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
_ReadBytes = new byte[_Data.Stride * _Data.Height];
_ReadStride = _Data.Stride;
Marshal.Copy(_Data.Scan0, _ReadBytes, 0, _ReadBytes.Length);
_NewBitmap.UnlockBits(_Data);
_WidthOne = (_NewBitmap.Width / 16) * 16 + (_NewBitmap.Width % 16 == 0 ? 0 : 16);
_WidthAll = _WidthOne * 3 * _NewBitmap.Height;
_WriteBytes = new byte[_WidthAll];
_WriteRowBytes = new byte[_WidthOne * 3];
_ReadIndex = 0;
_WriteIndex = 0;
_WriteBitArray = _WidthOne * 8;
_Stride = _WidthOne;
for (int i = 0; i != _NewBitmap.Height; i++)
{
_ReadIndex = i * _ReadStride;
Array.Copy(_WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes, 0, _WriteRowBytes.Length);
System.Collections.BitArray _WriteArray = new System.Collections.BitArray(_WriteRowBytes);
for (int z = 0; z != _NewBitmap.Width; z++)
{
byte[] _Color = new byte[] { _ReadBytes[_ReadIndex + (z * 3) + 2], _ReadBytes[_ReadIndex + (z * 3) + 1], _ReadBytes[_ReadIndex + (z * 3)] };
System.Collections.BitArray _ColorArray = new System.Collections.BitArray(_Color);
_WriteIndex = ((z / 8) * 8) + 7 - (z % 8); // 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 23
_WriteArray[_WriteIndex] = _ColorArray[0];
_WriteArray[_Stride + _WriteIndex] = _ColorArray[1];
_WriteArray[(_Stride * 2) + _WriteIndex] = _ColorArray[2];
_WriteArray[(_Stride * 3) + _WriteIndex] = _ColorArray[3];
_WriteArray[(_Stride * 4) + _WriteIndex] = _ColorArray[4];
_WriteArray[(_Stride * 5) + _WriteIndex] = _ColorArray[5];
_WriteArray[(_Stride * 6) + _WriteIndex] = _ColorArray[6];
_WriteArray[(_Stride * 7) + _WriteIndex] = _ColorArray[7]; //
_WriteArray[_WriteBitArray + _WriteIndex] = _ColorArray[8];
_WriteArray[_WriteBitArray + _Stride + _WriteIndex] = _ColorArray[9];
_WriteArray[_WriteBitArray + (_Stride * 2) + _WriteIndex] = _ColorArray[10];
_WriteArray[_WriteBitArray + (_Stride * 3) + _WriteIndex] = _ColorArray[11];
_WriteArray[_WriteBitArray + (_Stride * 4) + _WriteIndex] = _ColorArray[12];
_WriteArray[_WriteBitArray + (_Stride * 5) + _WriteIndex] = _ColorArray[13];
_WriteArray[_WriteBitArray + (_Stride * 6) + _WriteIndex] = _ColorArray[14];
_WriteArray[_WriteBitArray + (_Stride * 7) + _WriteIndex] = _ColorArray[15]; //
_WriteArray[_WriteBitArray * 2 + _WriteIndex] = _ColorArray[16];
_WriteArray[_WriteBitArray * 2 + _Stride + _WriteIndex] = _ColorArray[17];
_WriteArray[_WriteBitArray * 2 + (_Stride * 2) + _WriteIndex] = _ColorArray[18];
_WriteArray[_WriteBitArray * 2 + (_Stride * 3) + _WriteIndex] = _ColorArray[19];
_WriteArray[_WriteBitArray * 2 + (_Stride * 4) + _WriteIndex] = _ColorArray[20];
_WriteArray[_WriteBitArray * 2 + (_Stride * 5) + _WriteIndex] = _ColorArray[21];
_WriteArray[_WriteBitArray * 2 + (_Stride * 6) + _WriteIndex] = _ColorArray[22];
_WriteArray[_WriteBitArray * 2 + (_Stride * 7) + _WriteIndex] = _ColorArray[23]; //
}
_WriteArray.CopyTo(_WriteRowBytes, 0);
Array.Copy(_WriteRowBytes, 0, _WriteBytes, i * _WriteRowBytes.Length, _WriteRowBytes.Length);
}
_NewBitmap.Dispose();
return _WriteBytes;
break;
}
return new byte[0];
}
#region
/// <summary>
/// ANNO
/// </summary>
public class ImageIFFOfANNO
{
public ImageIFFOfANNO(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Text = Encoding.Default.GetString(p_FileBytes, (int)p_ReadIndex, (int)m_Size);
p_ReadIndex += m_Size;
}
public ImageIFFOfANNO(string p_Txt)
{
Text = p_Txt;
}
public ImageIFFOfANNO()
{
}
private uint m_Size = 0;
private string m_Text = "";
public string Text
{
get
{
return m_Text;
}
set
{
m_Text = value;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public byte[] GetBytes()
{
if (m_Text.Length == 0) return new byte[0];
MemoryStream _Stream = new MemoryStream();
_Stream.Write(new byte[] { 0x41, 0x4E, 0x4E, 0x4F }, 0, 4);
byte[] _TextBytes = Encoding.Default.GetBytes(m_Text);
int _Count = _TextBytes.Length;
if (_TextBytes.Length % 2 != 0) _Count++;
byte[] _ValueBytes = ImageIFF.UintToBytes((uint)_Count);
_Stream.Write(_ValueBytes, 0, _ValueBytes.Length);
_Stream.Write(_TextBytes, 0, _TextBytes.Length);
if (_TextBytes.Length % 2 != 0) _Stream.Write(new byte[] { 0x20 }, 0, 1);
return _Stream.ToArray();
}
}
/// <summary>
///
/// </summary>
private class ImageIffOfBMHD
{
/// <summary>
///
/// </summary>
private uint m_Size = 20;
private ushort m_Width = 0;
private ushort m_Height = 0;
private ushort m_X = 0;
private ushort m_Y = 0;
private PixelFormat m_Pixel = PixelFormat.Format24bppRgb;
private byte m_Masking = 0;
private byte m_Compression = 0;
private byte m_Padl = 0;
private ushort m_TColor = 0;
private byte m_XAspect = 0;
private byte m_YAspect = 0;
private ushort m_PWidth = 0;
private ushort m_PHeight = 0;
/// <summary>
/// 0 1
/// </summary>
public byte Compression { get { return m_Compression; } set { m_Compression = value; } }
/// <summary>
///
/// </summary>
public ushort Width { get { return m_Width; } set { m_Width = value; } }
/// <summary>
///
/// </summary>
public ushort Height { get { return m_Height; } set { m_Height = value; } }
/// <summary>
///
/// </summary>
public PixelFormat Pixel { get { return m_Pixel; } set { m_Pixel = value; } }
public ImageIffOfBMHD()
{
}
public ImageIffOfBMHD(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
m_Width = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_Height = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_X = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_Y = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
switch (p_FileBytes[p_ReadIndex])
{
case 8:
m_Pixel = PixelFormat.Format8bppIndexed;
break;
case 24:
m_Pixel = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception(" !");
}
p_ReadIndex++;
m_Masking = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_Compression = p_FileBytes[p_ReadIndex];
if (m_Compression != 0) throw new Exception(" RLE IFF ");
p_ReadIndex++;
m_Padl = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_TColor = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 2;
m_XAspect = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_YAspect = p_FileBytes[p_ReadIndex];
p_ReadIndex++;
m_PWidth = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex); ;
p_ReadIndex += 2;
m_PHeight = ImageIFF.BytesToShort(p_FileBytes, (int)p_ReadIndex); ;
p_ReadIndex += 2;
}
/// <summary>
///
/// </summary>
/// <param name="p_Bitmap"></param>
/// <returns></returns>
public byte[] GetBytes(Bitmap p_Bitmap)
{
if (p_Bitmap == null) return new byte[0];
MemoryStream _Stream = new MemoryStream();
_Stream.Write(new byte[] { 0x42, 0x4D, 0x48, 0x44 }, 0, 4);
_Stream.Write(new byte[] { 0, 0, 0, 0x14 }, 0, 4);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Width), 0, 2);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Height), 0, 2);
_Stream.Write(ImageIFF.UintToBytes(m_X), 0, 2);
_Stream.Write(ImageIFF.UintToBytes(m_Y), 0, 2);
switch (p_Bitmap.PixelFormat)
{
case PixelFormat.Format8bppIndexed:
_Stream.WriteByte(8);
break;
default:
_Stream.WriteByte(24);
break;
}
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.Write(ImageIFF.UintToBytes(m_TColor), 0, 2);
_Stream.WriteByte(0);
_Stream.WriteByte(0);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Width), 0, 2);
_Stream.Write(ImageIFF.UintToBytes((ushort)p_Bitmap.Height), 0, 2);
return _Stream.ToArray();
}
}
/// <summary>
///
/// </summary>
private class ImageIffOfCMAP
{
/// <summary>
///
/// </summary>
private uint m_Size = 20;
/// <summary>
///
/// </summary>
private Color[] m_ColorList = new Color[256];
/// <summary>
///
/// </summary>
public Color[] ColorList { get { return m_ColorList; } set { m_ColorList = value; m_Size = (uint)m_ColorList.Length * 3; } }
public ImageIffOfCMAP()
{
}
public ImageIffOfCMAP(byte[] p_FileBytes, ref uint p_ReadIndex)
{
m_Size = ImageIFF.BytesToUint(p_FileBytes, (int)p_ReadIndex);
p_ReadIndex += 4;
int _Count = (int)m_Size / 3;
m_ColorList = new Color[_Count];
for (int i = 0; i != _Count; i++)
{
m_ColorList[i] = Color.FromArgb(p_FileBytes[p_ReadIndex], p_FileBytes[p_ReadIndex + 1], p_FileBytes[p_ReadIndex + 2]);
p_ReadIndex += 3;
}
}
}
#endregion
#region
/// <summary>
/// UINT
/// </summary>
/// <param name="p_Value"> </param>
/// <param name="p_Index"> </param>
/// <returns></returns>
public static uint BytesToUint(byte[] p_Value, int p_Index)
{
byte[] _ValueBytes = new byte[4];
_ValueBytes[0] = p_Value[p_Index + 3];
_ValueBytes[1] = p_Value[p_Index + 2];
_ValueBytes[2] = p_Value[p_Index + 1];
_ValueBytes[3] = p_Value[p_Index];
return BitConverter.ToUInt32(_ValueBytes, 0);
}
/// <summary>
/// BYTES
/// </summary>
/// <param name="p_Value"></param>
/// <returns></returns>
public static byte[] UintToBytes(uint p_Value)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Value);
Array.Reverse(_ValueBytes);
return _ValueBytes;
}
/// <summary>
/// UINT
/// </summary>
/// <param name="p_Value"> </param>
/// <param name="p_Index"> </param>
/// <returns></returns>
public static ushort BytesToShort(byte[] p_Value, int p_Index)
{
byte[] _ValueBytes = new byte[2];
_ValueBytes[0] = p_Value[p_Index + 1];
_ValueBytes[1] = p_Value[p_Index];
return BitConverter.ToUInt16(_ValueBytes, 0);
}
/// <summary>
/// BYTES
/// </summary>
/// <param name="p_Value"></param>
/// <returns></returns>
public static byte[] UintToBytes(ushort p_Value)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Value);
Array.Reverse(_ValueBytes);
return _ValueBytes;
}
#endregion
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.