zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#CUR类实现代码

c#代码 实现
2023-06-13 09:14:10 时间

复制代码代码如下:


usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.IO;
usingSystem.Runtime.InteropServices;
namespaceZgke.MyImage.ImageFile
{
///<summary>
///CUR文件操作类
///zgke@sina.com
///qq:116149
///</summary>
publicclassImageCur
{
privateclassCurHead
{
privatebyte[]m_Retain=newbyte[2];
privatebyte[]m_Type=newbyte[]{0x02,0x00};
privatebyte[]m_ImageCount=newbyte[2];
privatebytem_ImageWidth;
privatebytem_ImageHeight;
privatebytem_ColorCount;
privatebyte[]m_NotUser=newbyte[5];
privatebyte[]m_ImageLength=newbyte[4];
privatebyte[]m_ImageRVA=newbyte[4];
///<summary>
///图形高
///</summary>
publicbyteImageHeight{get{returnm_ImageHeight;}set{m_ImageHeight=value;}}
///<summary>
///图象个数
///</summary>
publicushortImageCount{get{returnBitConverter.ToUInt16(m_ImageCount,0);}set{m_ImageCount=BitConverter.GetBytes(value);}}
///<summary>
///图形宽
///</summary>
publicbyteImageWidth{get{returnm_ImageWidth;}set{m_ImageWidth=value;}}
///<summary>
///色彩数(256以下色用)
///</summary>
publicbyteColorCount{get{returnm_ColorCount;}set{m_ColorCount=value;}}
///<summary>
///图象数据块的长度
///</summary>
publicuintImageLength{get{returnBitConverter.ToUInt32(m_ImageLength,0);}set{m_ImageLength=BitConverter.GetBytes(value);}}
///<summary>
///图象数据块相对于文件头部的偏移量
///</summary>
publicuintImageRVA{get{returnBitConverter.ToUInt32(m_ImageRVA,0);}set{m_ImageRVA=BitConverter.GetBytes(value);}}
publicCurHead()
{
}
publicCurHead(byte[]p_Data)
{
ImageCount=BitConverter.ToUInt16(p_Data,4);
ImageHeight=p_Data[7];
ImageWidth=p_Data[6];
ColorCount=p_Data[8];
ImageLength=BitConverter.ToUInt32(p_Data,14);
ImageRVA=BitConverter.ToUInt32(p_Data,18);
}
publicbyte[]GetByte()
{
byte[]_ReturnBytes=newbyte[22];
_ReturnBytes[0]=m_Retain[0];
_ReturnBytes[1]=m_Retain[1];
_ReturnBytes[2]=m_Retain[0];
_ReturnBytes[3]=m_Retain[1];
_ReturnBytes[4]=m_ImageCount[0];
_ReturnBytes[5]=m_ImageCount[1];
_ReturnBytes[6]=m_ImageWidth;
_ReturnBytes[7]=m_ImageHeight;
_ReturnBytes[8]=m_ColorCount;
_ReturnBytes[14]=m_ImageLength[0];
_ReturnBytes[15]=m_ImageLength[1];
_ReturnBytes[16]=m_ImageLength[2];
_ReturnBytes[17]=m_ImageLength[3];
_ReturnBytes[18]=m_ImageRVA[0];
_ReturnBytes[19]=m_ImageRVA[1];
_ReturnBytes[20]=m_ImageRVA[2];
_ReturnBytes[21]=m_ImageRVA[3];
return_ReturnBytes;
}
publicbyte[]GetImageByte()
{
byte[]_ReturnBytes=newbyte[16];
_ReturnBytes[0]=m_ImageWidth;
_ReturnBytes[1]=m_ImageHeight;
_ReturnBytes[2]=m_ColorCount;
_ReturnBytes[8]=m_ImageLength[0];
_ReturnBytes[9]=m_ImageLength[1];
_ReturnBytes[10]=m_ImageLength[2];
_ReturnBytes[11]=m_ImageLength[3];
_ReturnBytes[12]=m_ImageRVA[0];
_ReturnBytes[13]=m_ImageRVA[1];
_ReturnBytes[14]=m_ImageRVA[2];
_ReturnBytes[15]=m_ImageRVA[3];
return_ReturnBytes;
}
}
privateCurHeadm_CurHead;
privateBitmapm_CurImage;
privateIList<Image>m_CurList=newList<Image>();
publicImageCur(stringp_FileFullName)
{
byte[]_FileBytes=File.ReadAllBytes(p_FileFullName);
ImageCurBytes(_FileBytes);
}
publicImageCur(byte[]p_FileBytes)
{
ImageCurBytes(p_FileBytes);
}
privatevoidImageCurBytes(byte[]p_Bytes)
{
m_CurHead=newCurHead(p_Bytes);
for(inti=0;i!=m_CurHead.ImageCount;i++)
{
m_CurHead.ImageWidth=(byte)BitConverter.ToInt16(p_Bytes,(int)m_CurHead.ImageRVA+4);
m_CurHead.ImageHeight=(byte)(BitConverter.ToInt16(p_Bytes,(int)m_CurHead.ImageRVA+8)/2);
short_Piex=BitConverter.ToInt16(p_Bytes,(int)m_CurHead.ImageRVA+14);
LoadImgae(_Piex,p_Bytes,(int)m_CurHead.ImageRVA+40);
m_CurList.Add((Image)m_CurImage);
byte[]_Value=newbyte[4];
_Value[3]=p_Bytes[((i+1)*16)+18+3];
_Value[2]=p_Bytes[((i+1)*16)+18+2];
_Value[1]=p_Bytes[((i+1)*16)+18+1];
_Value[0]=p_Bytes[((i+1)*16)+18];
m_CurHead.ImageRVA=BitConverter.ToUInt32(_Value,0);
}
}
publicImageCur()
{
}
#regionRead
privatevoidLoad32(byte[]p_FileBytes,byte[]p_NewData,intp_ReadIndex,intp_Width,intp_Height,intp_MashIndex,BitmapDatap_NewBitmapData)
{
int_WriteIndex=0;
for(inti=p_NewBitmapData.Height-1;i!=-1;i--)
{
_WriteIndex=i*p_NewBitmapData.Stride;
for(intz=0;z!=p_NewBitmapData.Width;z++)
{
p_NewData[_WriteIndex+(z*4)]=p_FileBytes[p_ReadIndex];
p_NewData[_WriteIndex+(z*4)+1]=p_FileBytes[p_ReadIndex+1];
p_NewData[_WriteIndex+(z*4)+2]=p_FileBytes[p_ReadIndex+2];
p_NewData[_WriteIndex+(z*4)+3]=p_FileBytes[p_ReadIndex+3];
p_ReadIndex+=4;
}
}
}
privatevoidLoad24(byte[]p_FileBytes,byte[]p_NewData,intp_ReadIndex,intp_Width,intp_Height,intp_MashIndex,BitmapDatap_NewBitmapData)
{
int_WriteIndex=0;
int_MashStride=p_Width/8;
if(p_Width%8!=0)_MashStride++;
if(_MashStride%4!=0)_MashStride+=_MashStride%4;
byte[]_MashBytes=newbyte[_MashStride*p_Height];
Array.Copy(p_FileBytes,p_MashIndex,_MashBytes,0,_MashBytes.Length);
for(inti=0;i!=_MashBytes.Length;i++)
{
_MashBytes[i]=ConvertByte.OperateData.ReverseByte(_MashBytes[i]);
}
BitArray_MashArray=newBitArray(_MashBytes);
for(inti=p_NewBitmapData.Height-1;i!=-1;i--)
{
p_MashIndex=(p_Height-1-i)*(_MashStride*8);
_WriteIndex=i*p_NewBitmapData.Stride;
for(intz=0;z!=p_NewBitmapData.Width;z++)
{
p_NewData[_WriteIndex+(z*4)]=p_FileBytes[p_ReadIndex];
p_NewData[_WriteIndex+(z*4)+1]=p_FileBytes[p_ReadIndex+1];
p_NewData[_WriteIndex+(z*4)+2]=p_FileBytes[p_ReadIndex+2];
p_NewData[_WriteIndex+(z*4)+3]=0xFF;
if(_MashArray[p_MashIndex])p_NewData[_WriteIndex+(z*4)+3]=0x00;//
p_ReadIndex+=3;
p_MashIndex++;
}
}
}
privatevoidLoad8(byte[]p_FileBytes,byte[]p_NewData,intp_ReadIndex,intp_Width,intp_Height,intp_MashIndex,BitmapDatap_NewBitmapData)
{
int_WriteIndex=0;
Hashtable_ColorHashTable=newHashtable();
for(inti=0;i!=256;i++)
{
_ColorHashTable.Add(i.ToString(),Color.FromArgb(p_FileBytes[p_ReadIndex+3],p_FileBytes[p_ReadIndex+2],p_FileBytes[p_ReadIndex+1],p_FileBytes[p_ReadIndex]));
p_ReadIndex+=4;
}
p_MashIndex=p_Width*p_Height+p_ReadIndex;
int_MashStride=p_Width/8;
if(p_Width%8!=0)_MashStride++;
if(_MashStride%4!=0)_MashStride+=_MashStride%4;
byte[]_MashBytes=newbyte[_MashStride*p_Height];
Array.Copy(p_FileBytes,p_MashIndex,_MashBytes,0,_MashBytes.Length);
for(inti=0;i!=_MashBytes.Length;i++)
{
_MashBytes[i]=ConvertByte.OperateData.ReverseByte(_MashBytes[i]);
}
BitArray_MashArray=newBitArray(_MashBytes);
for(inti=p_NewBitmapData.Height-1;i!=-1;i--)
{
p_MashIndex=(p_Height-1-i)*(_MashStride*8);
_WriteIndex=i*p_NewBitmapData.Stride;
for(intz=0;z!=p_NewBitmapData.Width;z++)
{
byte_Index=p_FileBytes[p_ReadIndex];
Color_SetColor=(Color)_ColorHashTable[_Index.ToString()];
p_NewData[_WriteIndex+(z*4)]=(byte)_SetColor.B;
p_NewData[_WriteIndex+(z*4)+1]=(byte)_SetColor.G;
p_NewData[_WriteIndex+(z*4)+2]=(byte)_SetColor.R;
p_NewData[_WriteIndex+(z*4)+3]=0xFF;
if(_MashArray[p_MashIndex])p_NewData[_WriteIndex+(z*4)+3]=0x00;//
p_ReadIndex++;
p_MashIndex++;
}
}
}
privatevoidLoad4(byte[]p_FileBytes,byte[]p_NewData,intp_ReadIndex,intp_Width,intp_Height,intp_MashIndex,BitmapDatap_NewBitmapData)
{
int_WriteIndex=0;
Hashtable_ColorHashTable=newHashtable();
for(inti=0;i!=16;i++)
{
_ColorHashTable.Add(i.ToString(),Color.FromArgb(p_FileBytes[p_ReadIndex+3],p_FileBytes[p_ReadIndex+2],p_FileBytes[p_ReadIndex+1],p_FileBytes[p_ReadIndex]));
p_ReadIndex+=4;
}
p_MashIndex=(p_Width*p_Height/2)+p_ReadIndex;
int_MashStride=p_Width/8;
if(p_Width%8!=0)_MashStride++;
if(_MashStride%4!=0)_MashStride+=_MashStride%4;
byte[]_MashBytes=newbyte[_MashStride*p_Height];
Array.Copy(p_FileBytes,p_MashIndex,_MashBytes,0,_MashBytes.Length);
for(inti=0;i!=_MashBytes.Length;i++)
{
_MashBytes[i]=ConvertByte.OperateData.ReverseByte(_MashBytes[i]);
}
BitArray_MashArray=newBitArray(_MashBytes);
bool_Lith=true;
for(inti=p_NewBitmapData.Height-1;i!=-1;i--)
{
p_MashIndex=(p_Height-1-i)*(_MashStride*8);
_WriteIndex=i*p_NewBitmapData.Stride;
for(intz=0;z!=p_NewBitmapData.Width;z++)
{
byte_Index=p_FileBytes[p_ReadIndex];
if(_Lith)
{
_Index=(byte)((_Index&0xF0)>>4);
_Lith=false;
}
else
{
_Index=(byte)(_Index&0x0F);//后面的F
_Lith=true;
p_ReadIndex++;
}
Color_SetColor=(Color)_ColorHashTable[_Index.ToString()];
p_NewData[_WriteIndex+(z*4)]=(byte)_SetColor.B;
p_NewData[_WriteIndex+(z*4)+1]=(byte)_SetColor.G;
p_NewData[_WriteIndex+(z*4)+2]=(byte)_SetColor.R;
p_NewData[_WriteIndex+(z*4)+3]=0xFF;
if(_MashArray[p_MashIndex])p_NewData[_WriteIndex+(z*4)+3]=0x00;//
p_MashIndex++;
}
}
}
privatevoidLoad1(byte[]p_FileBytes,byte[]p_NewData,intp_ReadIndex,intp_Width,intp_Height,intp_MashIndex,BitmapDatap_NewBitmapData)
{
int_WriteIndex=0;
Hashtable_ColorHashTable=newHashtable();
for(inti=0;i!=2;i++)
{
_ColorHashTable.Add(i.ToString(),Color.FromArgb(p_FileBytes[p_ReadIndex+3],p_FileBytes[p_ReadIndex+2],p_FileBytes[p_ReadIndex+1],p_FileBytes[p_ReadIndex]));
p_ReadIndex+=4;
}
p_MashIndex=(p_Width*p_Height/8)+p_ReadIndex;
int_MashStride=p_Width/8;
if(p_Width%8!=0)_MashStride++;
if(_MashStride%4!=0)_MashStride+=_MashStride%4;
byte[]_MashBytes=newbyte[_MashStride*p_Height];
Array.Copy(p_FileBytes,p_MashIndex,_MashBytes,0,_MashBytes.Length);
for(inti=0;i!=_MashBytes.Length;i++)
{
_MashBytes[i]=ConvertByte.OperateData.ReverseByte(_MashBytes[i]);
}
BitArray_MashArray=newBitArray(_MashBytes);
int_Lith=7;
for(inti=p_NewBitmapData.Height-1;i!=-1;i--)
{
p_MashIndex=(p_Height-1-i)*(_MashStride*8);
_WriteIndex=i*p_NewBitmapData.Stride;
for(intz=0;z!=p_NewBitmapData.Width;z++)
{
byte_Index=p_FileBytes[p_ReadIndex];
BitArray_ColorIndex=newBitArray(newbyte[]{_Index});
if(_ColorIndex[_Lith])
{
_Index=1;
}
else
{
_Index=0;
}
if(_Lith==0)
{
p_ReadIndex++;
_Lith=7;
}
else
{
_Lith--;
}
Color_SetColor=(Color)_ColorHashTable[_Index.ToString()];
p_NewData[_WriteIndex+(z*4)]=(byte)_SetColor.B;
p_NewData[_WriteIndex+(z*4)+1]=(byte)_SetColor.G;
p_NewData[_WriteIndex+(z*4)+2]=(byte)_SetColor.R;
p_NewData[_WriteIndex+(z*4)+3]=0xFF;
if(_MashArray[p_MashIndex])p_NewData[_WriteIndex+(z*4)+3]=0x00;//
p_MashIndex++;
}
}
}
#endregion
privatevoidLoadImgae(shortm_Piex,byte[]p_FileBytes,int_StarIndex)
{
int_Width=m_CurHead.ImageWidth;
int_Height=m_CurHead.ImageHeight;
m_CurImage=newBitmap(_Width,_Height,PixelFormat.Format32bppArgb);
BitmapData_NewBitmapData=m_CurImage.LockBits(newRectangle(0,0,_Width,_Height),ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb);
byte[]_NewData=newbyte[_NewBitmapData.Stride*_NewBitmapData.Height];
int_ReadIndex=_StarIndex;
int_MashIndex=0;
switch(m_Piex)
{
case1:
_MashIndex=(_Width*_Height/8)+_ReadIndex;
Load1(p_FileBytes,_NewData,_ReadIndex,_Width,_Height,_MashIndex,_NewBitmapData);
break;
case4:
_MashIndex=(_Width*_Height/2)+_ReadIndex;
Load4(p_FileBytes,_NewData,_ReadIndex,_Width,_Height,_MashIndex,_NewBitmapData);
break;
case8:
_MashIndex=_Width*_Height+_ReadIndex;
Load8(p_FileBytes,_NewData,_ReadIndex,_Width,_Height,_MashIndex,_NewBitmapData);
break;
case24:
_MashIndex=_Width*_Height*3+_ReadIndex;
Load24(p_FileBytes,_NewData,_ReadIndex,_Width,_Height,_MashIndex,_NewBitmapData);
break;
case32:
_MashIndex=_Width*_Height*4+_ReadIndex;
Load32(p_FileBytes,_NewData,_ReadIndex,_Width,_Height,_MashIndex,_NewBitmapData);
break;
default:
thrownewException("不支持的格式");
}
Marshal.Copy(_NewData,0,_NewBitmapData.Scan0,_NewData.Length);
m_CurImage.UnlockBits(_NewBitmapData);
}
publicvoidSaveImage(stringp_FileName)
{
if(m_CurList.Count==0)return;
FileStream_File=newFileStream(p_FileName,FileMode.Create,FileAccess.Write);
m_CurHead=newCurHead();
_File.Write(newbyte[]{0x00,0x00,0x02,0x00},0,4);
_File.Write(BitConverter.GetBytes((ushort)m_CurList.Count),0,2);
List<byte[]>_ImageByteList=newList<byte[]>();
m_CurHead.ImageRVA=(uint)m_CurList.Count*16+6;
for(inti=0;i!=m_CurList.Count;i++)
{
if(m_CurList[i].Width>255||m_CurList[i].Height>255)
{
_File.Close();
thrownewException("图形文件过大!");
}
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(inti=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();
}
publicMemoryStreamSaveImage()
{
if(m_CurList.Count==0)thrownewException("无图形可保存");
MemoryStream_Memory=newMemoryStream();
m_CurHead=newCurHead();
_Memory.Write(newbyte[]{0x00,0x00,0x02,0x00},0,4);
_Memory.Write(BitConverter.GetBytes((ushort)m_CurList.Count),0,2);
List<byte[]>_ImageByteList=newList<byte[]>();
m_CurHead.ImageRVA=(uint)m_CurList.Count*16+6;
for(inti=0;i!=m_CurList.Count;i++)
{
if(m_CurList[i].Width>255||m_CurList[i].Height>255)
{
_Memory.Close();
thrownewException("图形文件过大!");
}
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);
_Memory.Write(m_CurHead.GetImageByte(),0,16);
}
for(inti=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];
_Memory.Write(_ImageByteList[i],0,_ImageByteList[i].Length);
}
return_Memory;
}
///<summary>
///CUR图形
///</summary>
publicIList<Image>CurImage{get{returnm_CurList;}set{m_CurList=value;}}
publicbyte[]GetImageBytes(intp_ImageIndex)
{
MemoryStream_Memory=newMemoryStream();
if(m_CurList[p_ImageIndex].PixelFormat!=PixelFormat.Format32bppArgb)
{
Bitmap_Image=newBitmap(m_CurList[p_ImageIndex].Width,m_CurList[p_ImageIndex].Height,PixelFormat.Format32bppArgb);
Graphics_Graphcis=Graphics.FromImage(_Image);
_Graphcis.Dispose();
_Image.Save(_Memory,ImageFormat.Bmp);
}
else
{
m_CurList[p_ImageIndex].Save(_Memory,ImageFormat.Bmp);
}
byte[]_Test=_Memory.ToArray();
byte[]_ImageBytes=newbyte[_Memory.Length-0x0E];
_Memory.Position=0x0E;
_Memory.Read(_ImageBytes,0,_ImageBytes.Length);
return_ImageBytes;
}
}
}