/// <summary> /// Load PCX Image from Byte Array /// </summary> /// <param name="p_Bytes">PCX File Byte Array</param> private void Load(byte[] p_Bytes) { // Copy Reference byte[] _Bytes = p_Bytes; // Check Magic Byte if (_Bytes[0] != PCXMagicByte) throw new ArgumentException(string.Format("Magic Byte is {0} instead of {1}", _Bytes[0], PCXMagicByte), "p_Bytes"); // Get PCX header m_Head = new PCXHeader(_Bytes); var _Position = PCXHeader.HeaderSize; // Detect Depth PixelFormat _PixFormate = PixelFormat.Format24bppRgb; if (m_Head.Colour_Planes == (byte)PCXColorDepth.bpp8) _PixFormate = PixelFormat.Format8bppIndexed; // Create Image from Header Data PcxImage = new Bitmap(m_Head.Width, m_Head.Height, _PixFormate); BitmapData _Data = PcxImage.LockBits(new Rectangle(0, 0, PcxImage.Width, PcxImage.Height), ImageLockMode.ReadWrite, _PixFormate); byte[] _BmpData = new byte[_Data.Stride * _Data.Height]; for (int i = 0; i < m_Head.Height; i++) { byte[] _RowColorValue = new byte[0]; switch (m_Head.Colour_Planes) { case (byte)PCXColorDepth.bpp8: //256 _RowColorValue = LoadPCXLine8(_Bytes, ref _Position); break; default: //24 _RowColorValue = LoadPCXLine24(_Bytes, ref _Position); break; } int _Count = _RowColorValue.Length; Array.Copy(_RowColorValue, 0, _BmpData, i * _Data.Stride, _Count); } Marshal.Copy(_BmpData, 0, _Data.Scan0, _BmpData.Length); PcxImage.UnlockBits(_Data); switch (m_Head.Colour_Planes) { case (byte)PCXColorDepth.bpp8: ColorPalette _Palette = PcxImage.Palette; _Position = p_Bytes.Length - 256 * 3; for (int i = 0; i != 256; i++) { _Palette.Entries[i] = Color.FromArgb(p_Bytes[_Position], p_Bytes[_Position + 1], p_Bytes[_Position + 2]); _Position += 3; } PcxImage.Palette = _Palette; break; } }
/// <summary> /// Load PCX Image from Byte Array /// </summary> /// <param name="p_Bytes">PCX File Byte Array</param> private void Load(byte[] p_Bytes) { // Copy Reference byte[] _Bytes = p_Bytes; // Check Magic Byte if (_Bytes[0] != PCXMagicByte) { throw new ArgumentException(string.Format("Magic Byte is {0} instead of {1}", _Bytes[0], PCXMagicByte), "p_Bytes"); } // Get PCX header m_Head = new PCXHeader(_Bytes); var _Position = PCXHeader.HeaderSize; // Detect Depth PixelFormat _PixFormate = PixelFormat.Format24bppRgb; if (m_Head.Colour_Planes == (byte)PCXColorDepth.bpp8) { _PixFormate = PixelFormat.Format8bppIndexed; } // Create Image from Header Data PcxImage = new Bitmap(m_Head.Width, m_Head.Height, _PixFormate); BitmapData _Data = PcxImage.LockBits(new Rectangle(0, 0, PcxImage.Width, PcxImage.Height), ImageLockMode.ReadWrite, _PixFormate); byte[] _BmpData = new byte[_Data.Stride * _Data.Height]; for (int i = 0; i < m_Head.Height; i++) { byte[] _RowColorValue = new byte[0]; switch (m_Head.Colour_Planes) { case (byte)PCXColorDepth.bpp8: //256 _RowColorValue = LoadPCXLine8(_Bytes, ref _Position); break; default: //24 _RowColorValue = LoadPCXLine24(_Bytes, ref _Position); break; } int _Count = _RowColorValue.Length; Array.Copy(_RowColorValue, 0, _BmpData, i * _Data.Stride, _Count); } Marshal.Copy(_BmpData, 0, _Data.Scan0, _BmpData.Length); PcxImage.UnlockBits(_Data); switch (m_Head.Colour_Planes) { case (byte)PCXColorDepth.bpp8: ColorPalette _Palette = PcxImage.Palette; _Position = p_Bytes.Length - 256 * 3; for (int i = 0; i != 256; i++) { _Palette.Entries[i] = Color.FromArgb(p_Bytes[_Position], p_Bytes[_Position + 1], p_Bytes[_Position + 2]); _Position += 3; } PcxImage.Palette = _Palette; break; } }