C# CUR类实现代码

2019-05-11 23:49:07王旭

{
int _Width = m_CurHead.ImageWidth;
int _Height = m_CurHead.ImageHeight;
m_CurImage = new Bitmap(_Width, _Height, PixelFormat.Format32bppArgb);
BitmapData _NewBitmapData = m_CurImage.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] _NewData = new byte[_NewBitmapData.Stride * _NewBitmapData.Height];
int _ReadIndex = _StarIndex;
int _MashIndex = 0;
switch (m_Piex)
{
case 1:
_MashIndex = (_Width * _Height / 8) + _ReadIndex;
Load1(p_FileBytes, _NewData, _ReadIndex, _Width, _Height, _MashIndex, _NewBitmapData);
break;
case 4:
_MashIndex = (_Width * _Height / 2) + _ReadIndex;
Load4(p_FileBytes, _NewData, _ReadIndex, _Width, _Height, _MashIndex, _NewBitmapData);
break;
case 8:
_MashIndex = _Width * _Height + _ReadIndex;
Load8(p_FileBytes, _NewData, _ReadIndex, _Width, _Height, _MashIndex, _NewBitmapData);
break;
case 24:
_MashIndex = _Width * _Height * 3 + _ReadIndex;
Load24(p_FileBytes, _NewData, _ReadIndex, _Width, _Height, _MashIndex, _NewBitmapData);
break;
case 32:
_MashIndex = _Width * _Height * 4 + _ReadIndex;
Load32(p_FileBytes, _NewData, _ReadIndex, _Width, _Height, _MashIndex, _NewBitmapData);
break;
default:
throw new Exception("不支持的格式");
}
Marshal.Copy(_NewData, 0, _NewBitmapData.Scan0, _NewData.Length);
m_CurImage.UnlockBits(_NewBitmapData);
}
public void SaveImage(string p_FileName)
{
if (m_CurList.Count == 0) return;
FileStream _File = new FileStream(p_FileName, FileMode.Create, FileAccess.Write);
m_CurHead = new CurHead();
_File.Write(new byte[]{0x00,0x00,0x02,0x00},0,4);
_File.Write(BitConverter.GetBytes((ushort)m_CurList.Count),0,2);
List<byte[]> _ImageByteList = new List<byte[]>();
m_CurHead.ImageRVA = (uint)m_CurList.Count * 16+6;
for (int i = 0; i != m_CurList.Count; i++)
{
if (m_CurList[i].Width > 255 || m_CurList[i].Height > 255)
{
_File.Close();
throw new Exception("图形文件过大!");
}
byte[] _ImageSize = GetImageBytes(i);
m_CurHead.ImageHeight = (byte)CurImage[i].Height;
m_CurHead.ImageWidth = (byte)CurImage[i].Width;
m_CurHead.ImageRVA += m_CurHead.ImageLength;
m_CurHead.ImageLength = (uint)_ImageSize.Length;
_ImageByteList.Add(_ImageSize);
_File.Write(m_CurHead.GetImageByte(), 0, 16);
}
for (int i = 0; i != _ImageByteList.Count; i++)
{
byte[] _Height = BitConverter.GetBytes((uint)(m_CurList[i].Height * 2));
_ImageByteList[i][8] = _Height[0];
_ImageByteList[i][9] = _Height[1];
_ImageByteList[i][10] = _Height[2];
_ImageByteList[i][11] = _Height[3];
_File.Write(_ImageByteList[i], 0, _ImageByteList[i].Length);
}
_File.Close();
}
public MemoryStream SaveImage()
{
if (m_CurList.Count == 0) throw new Exception("无图形可保存");
MemoryStream _Memory = new MemoryStream();
m_CurHead = new CurHead();
_Memory.Write(new byte[] { 0x00, 0x00, 0x02, 0x00 }, 0, 4);