C # cur Class implementation code _ Practical skills

Source: Internet
Author: User

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Collections;
Using System.Text;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.IO;
Using System.Runtime.InteropServices;
Namespace Zgke.MyImage.ImageFile
{
<summary>
Cur file Action Class
Zgke@sina.com
qq:116149
</summary>
public class Imagecur
{
Private Class Curhead
{
Private byte[] M_retain = new byte[2];
Private byte[] M_type = new byte[] {0x02, 0x00};
Private byte[] M_imagecount = new byte[2];
Private byte M_imagewidth;
Private byte M_imageheight;
Private byte M_colorcount;
Private byte[] M_notuser = new BYTE[5];
Private byte[] M_imagelength = new Byte[4];
Private byte[] M_imagerva = new Byte[4];
<summary>
Graphic height
</summary>
Public byte ImageHeight {get {m_imageheight;} set {m_imageheight = value;}}
<summary>
Number of images
</summary>
Public ushort Imagecount {get {return bitconverter.touint16 (m_imagecount, 0);} set {M_imagecount = Bitconverter.getbyt ES (value); } }
<summary>
Graphics width
</summary>
Public byte ImageWidth {get {m_imagewidth;} set {m_imagewidth = value;}}
<summary>
Number of colors (under 256 colors)
</summary>
Public byte Colorcount {get {m_colorcount;} set {m_colorcount = value;}}
<summary>
Length of image data block
</summary>
Public UINT Imagelength {get {return Bitconverter.touint32 (m_imagelength, 0);} set {m_imagelength = Bitconverter.getby TES (value); } }
<summary>
The offset of the image block relative to the head of the file
</summary>
Public UINT Imagerva {get {return Bitconverter.touint32 (M_imagerva, 0);} set {M_imagerva = Bitconverter.getbytes (value ); } }
Public Curhead ()
{
}
Public Curhead (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);
}
Public byte[] GetByte ()
{
byte[] _returnbytes = new BYTE[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;
}
Public byte[] Getimagebyte ()
{
byte[] _returnbytes = new BYTE[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;
}
}
Private Curhead M_curhead;
Private Bitmap m_curimage;
Private ilist<image> m_curlist =new list<image> ();
Public Imagecur (String p_filefullname)
{
byte[] _filebytes = file.readallbytes (p_filefullname);
Imagecurbytes (_filebytes);
}
Public Imagecur (byte[] p_filebytes)
{
Imagecurbytes (p_filebytes);
}
private void Imagecurbytes (byte[] p_bytes)
{
M_curhead = new Curhead (p_bytes);
for (int i = 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 = new Byte[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);
}
}
Public Imagecur ()
{
}
#region Read
private void Load32 (byte[] p_filebytes, byte[] p_newdata, int p_readindex, int p_width, int p_height, int p_mashindex, Bit MapData p_newbitmapdata)
{
int _writeindex = 0;
for (int i = p_newbitmapdata.height-1 i!=-1; i--)
{
_writeindex = i * p_newbitmapdata.stride;
for (int z = 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;
}
}
}
private void Load24 (byte[] p_filebytes, byte[] p_newdata, int p_readindex, int p_width, int p_height, int p_mashindex, Bit MapData p_newbitmapdata)
{
int _writeindex = 0;
int _mashstride = P_WIDTH/8;
if (p_width% 8!= 0) _mashstride++;
if (_mashstride% 4!= 0) _mashstride + = _mashstride% 4;
byte[] _mashbytes = new byte[_mashstride * P_height];
Array.copy (P_filebytes, P_mashindex, _mashbytes, 0, _mashbytes.length);
for (int i = 0; I!= _mashbytes.length; i++)
{
_mashbytes[i] = ConvertByte.OperateData.ReverseByte (_mashbytes[i]);
}
BitArray _masharray = new BitArray (_mashbytes);
for (int i = p_newbitmapdata.height-1 i!=-1; i--)
{
P_mashindex = (P_height-1-i) * (_mashstride * 8);
_writeindex = i * p_newbitmapdata.stride;
for (int z = 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++;
}
}
}
private void Load8 (byte[] p_filebytes, byte[] p_newdata, int p_readindex, int p_width, int p_height, int p_mashindex, BITM Apdata p_newbitmapdata)
{
int _writeindex = 0;
Hashtable _colorhashtable = new Hashtable ();
for (int i = 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 = new byte[_mashstride * P_height];
Array.copy (P_filebytes, P_mashindex, _mashbytes, 0, _mashbytes.length);
for (int i = 0; I!= _mashbytes.length; i++)
{
_mashbytes[i] = ConvertByte.OperateData.ReverseByte (_mashbytes[i]);
}
BitArray _masharray = new BitArray (_mashbytes);
for (int i = p_newbitmapdata.height-1 i!=-1; i--)
{
P_mashindex = (P_height-1-i) * (_mashstride * 8);
_writeindex = i * p_newbitmapdata.stride;
for (int z = 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++;
}
}
}
private void Load4 (byte[] p_filebytes, byte[] p_newdata, int p_readindex, int p_width, int p_height, int p_mashindex, BITM Apdata p_newbitmapdata)
{
int _writeindex = 0;
Hashtable _colorhashtable = new Hashtable ();
for (int i = 0; I!= 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 = new byte[_mashstride * P_height];
Array.copy (P_filebytes, P_mashindex, _mashbytes, 0, _mashbytes.length);
for (int i = 0; I!= _mashbytes.length; i++)
{
_mashbytes[i] = ConvertByte.OperateData.ReverseByte (_mashbytes[i]);
}
BitArray _masharray = new BitArray (_mashbytes);
bool _lith = true;
for (int i = p_newbitmapdata.height-1 i!=-1; i--)
{
P_mashindex = (P_height-1-i) * (_mashstride * 8);
_writeindex = i * p_newbitmapdata.stride;
for (int z = 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); Behind the 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++;
}
}
}
private void Load1 (byte[] p_filebytes, byte[] p_newdata, int p_readindex, int p_width, int p_height, int p_mashindex, BITM Apdata p_newbitmapdata)
{
int _writeindex = 0;
Hashtable _colorhashtable = new Hashtable ();
for (int i = 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 = new byte[_mashstride * P_height];
Array.copy (P_filebytes, P_mashindex, _mashbytes, 0, _mashbytes.length);
for (int i = 0; I!= _mashbytes.length; i++)
{
_mashbytes[i] = ConvertByte.OperateData.ReverseByte (_mashbytes[i]);
}
BitArray _masharray = new BitArray (_mashbytes);
int _lith = 7;
for (int i = p_newbitmapdata.height-1 i!=-1; i--)
{
P_mashindex = (P_height-1-i) * (_mashstride * 8);
_writeindex = i * p_newbitmapdata.stride;
for (int z = 0; z!= p_newbitmapdata.width; z++)
{
byte _index = P_filebytes[p_readindex];
BitArray _colorindex = new BitArray (new byte[] {_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
private void Loadimgae (short M_piex, byte[] p_filebytes,int _starindex)
{
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 ("Unsupported format");
}
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 ("Graphics file is too big!") ");
}
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 ("No Graphics to save");
MemoryStream _memory = new MemoryStream ();
M_curhead = new Curhead ();
_memory.write (new byte[] {0x00, 0x00, 0x02, 0x00}, 0, 4);
_memory.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)
{
_memory.close ();
throw new Exception ("Graphics file is too big!") ");
}
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 (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];
_memory.write (_imagebytelist[i], 0, _imagebytelist[i]. Length);
}
return _memory;
}
<summary>
Cur graphics
</summary>
Public ilist<image> Curimage {get {return m_curlist;} set {m_curlist = value;}}
Public byte[] Getimagebytes (int p_imageindex)
{
MemoryStream _memory = new MemoryStream ();
if (M_curlist[p_imageindex). PixelFormat!= Pixelformat.format32bppargb)
{
Bitmap _image = new Bitmap (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 = new BYTE[_MEMORY.LENGTH-0X0E];
_memory.position = 0x0e;
_memory.read (_imagebytes, 0, _imagebytes.length);
return _imagebytes;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.