C#图片提取特征时目标数组长度不够怎么解决啊 谢谢啊

wanghezhe 2014-03-31 11:29:23

public byte[] m_Data = new byte[128];
// 取得PCX文件标记。必须为0x0A;
public byte Manufacturer { get { return m_Data[0]; } }
// 取得/设置PCX版本:0:PC Paintbrush 2.5 版   2:PC Paintbrush 2.8 版  5:PC Paintbrush 3.0 版
public byte Version { get { return m_Data[1]; } set { m_Data[1] = value; } }
public byte Encoding { get { return m_Data[2]; } set { m_Data[2] = value; } }
public byte Bits_Per_Pixel { get { return m_Data[3]; } set { m_Data[3] = value; } }
public ushort Xmin { get { return BitConverter.ToUInt16(m_Data, 4); } set { SetUshort(4, value); } }
public ushort Ymin { get { return BitConverter.ToUInt16(m_Data, 6); } set { SetUshort(6, value); } }
public ushort Xmax { get { return BitConverter.ToUInt16(m_Data, 8); } set { SetUshort(8, value); } }
public ushort Ymax { get { return BitConverter.ToUInt16(m_Data, 10); } set { SetUshort(10, value); } }

//取得/设置 水平分辨率
public ushort Hres1 { get { return BitConverter.ToUInt16(m_Data, 12); } set { SetUshort(12, value); } }
public ushort Vres1 { get { return BitConverter.ToUInt16(m_Data, 14); } set { SetUshort(14, value); } }

//取得/设置16色图像的色表
public byte[] Palette
{
get
{
byte[] _Palette = new byte[48];
Array.Copy(m_Data, 16, _Palette, 0, 48);
return _Palette;
}
set
{
if (value.Length != 48) throw new Exception("错误的byte[]长度不是48");
Array.Copy(value, 0, m_Data, 16, 48);
}

}


public byte Reserved { get { return m_Data[64]; } set { m_Data[64] = value; } }


public byte Colour_Planes { get { return m_Data[65]; } set { m_Data[65] = value; } }


public ushort Bytes_Per_Line { get { return BitConverter.ToUInt16(m_Data, 66); } set { SetUshort(66, value); } }


public ushort Palette_Type { get { return BitConverter.ToUInt16(m_Data, 68); } set { SetUshort(68, value); } }


public byte[] Filler
{
get
{
byte[] m_Bytes = new byte[58];
Array.Copy(m_Data, 70, m_Bytes, 0, 58);
return m_Bytes;
}
}


public PCXHEAD(byte[] p_Data)
{
Array.Copy(p_Data, m_Data, 128);
}

//构造函数-2(新建数据)
public PCXHEAD()
{
m_Data[0] = 0xA;
Version = 0x5;
Encoding = 0x1;
Bits_Per_Pixel = 0x8;
Palette = new byte[] { 0x00, 0x00, 0xCD, 0x00, 0x90, 0xE7, 0x37, 0x01, 0x80, 0xF6, 0x95, 0x7C, 0x28, 0xFB, 0x95, 0x7C, 0xFF, 0xFF, 0xFF, 0xFF, 0x23, 0xFB, 0x95, 0x7C, 0xB3, 0x16, 0x34, 0x7C, 0x00, 0x00, 0xCD, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB8, 0x16, 0x34, 0x7C, 0x64, 0xF3, 0x37, 0x01, 0xD8, 0x54, 0xB8, 0x00 };
Reserved = 0x01;
Colour_Planes = 0x03;
Palette_Type = 1;
}


public int Width { get { return Xmax - Xmin + 1; } }

public int Height { get { return Ymax - Ymin + 1; } }


private void SetUshort(int p_Index, ushort p_Data)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Data);
m_Data[p_Index] = _ValueBytes[0];
m_Data[p_Index + 1] = _ValueBytes[1];
}
}

//定义类变量
private PCXHEAD m_Head = new PCXHEAD();
private int m_ReadIndex = 0; // 读取标记
private Bitmap m_Image = null;
private byte[] _BmpData = null;
private String filepath = null;


public Bitmap PcxImage { get { return m_Image; } set { m_Image = value; } }

public byte[] imageByte { get { return _BmpData; } set { _BmpData = value; } }

public int imageHeight { get { return m_Image.Height; } }
public int imageWidth { get { return m_Image.Width; } }
public string CurrentFilepath { get { return this.filepath; } set { this.filepath = value; } }
//public string CurrentFilename { get { return Global.getfilename(this.filepath); } }


public PCXImage(string p_FileFullName)
{
if (!File.Exists(p_FileFullName)) return;
this.filepath = p_FileFullName;

Load(File.ReadAllBytes(p_FileFullName));
}

//imagepcx类构造函数-2
public PCXImage(byte[] p_Data)
{
Load(p_Data);
}

//imagepcx类构造函数-3
public PCXImage()
{
if (this.filepath == null)
return;
if (!File.Exists(this.filepath))
return;
else
Load(File.ReadAllBytes(this.filepath));
}

private void Load(byte[] p_Bytes)
{
byte[] _Bytes = p_Bytes;
if (_Bytes[0] != 0x0A) return;
//构造文件头对象
m_Head = new PCXHEAD(_Bytes);
m_ReadIndex = 128;

PixelFormat _PixFormate = PixelFormat.Format24bppRgb;
if (m_Head.Colour_Planes == 1)
{
switch (m_Head.Bits_Per_Pixel)
{
case 8://8位图
_PixFormate = PixelFormat.Format8bppIndexed;
break;
case 1://1位图
_PixFormate = PixelFormat.Format1bppIndexed;
break;
}
}

m_Image = new Bitmap(m_Head.Width, m_Head.Height, _PixFormate);

BitmapData _Data = m_Image.LockBits(new Rectangle(0, 0, m_Image.Width, m_Image.Height), ImageLockMode.ReadWrite, _PixFormate);

_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 3: //24位
_RowColorValue = LoadPCXLine24(_Bytes);
break;
case 1: //256色
switch (m_Head.Bits_Per_Pixel)
{
case 8://每个像素8位表示
_RowColorValue = LoadPCXLine8(_Bytes);
break;
case 1://每个像素1位表示
_RowColorValue = LoadPCXLine1(_Bytes);
break;
}
break;
}
//每行数据的字节数
int _Count = _RowColorValue.Length;
//将处理好的行像素数据复制到位图数组中的对应位置
Array.Copy(_RowColorValue, 0, _BmpData, i * _Data.Stride, _Count);
}



Marshal.Copy(_BmpData, 0, _Data.Scan0, _BmpData.Length);
m_Image.UnlockBits(_Data);
switch (m_Head.Colour_Planes)
{
case 1:
if (m_Head.Bits_Per_Pixel == 8)
{
ColorPalette _Palette = m_Image.Palette;
m_ReadIndex = p_Bytes.Length - 256 * 3;
for (int i = 0; i != 256; i++)
{
_Palette.Entries[i] = Color.FromArgb(p_Bytes[m_ReadIndex], p_Bytes[m_ReadIndex + 1], p_Bytes[m_ReadIndex + 2]);
m_ReadIndex += 3;
}
m_Image.Palette = _Palette;
}
break;
}
}


public void Save(string p_FileFullName)
{
if (m_Image == null) return;
m_Head.Xmax = (ushort)(m_Image.Width - 1);
m_Head.Ymax = (ushort)(m_Image.Height - 1);
m_Head.Vres1 = (ushort)(m_Head.Xmax + 1);
m_Head.Hres1 = (ushort)(m_Head.Ymax + 1);
m_Head.Bytes_Per_Line = (ushort)m_Head.Width;

MemoryStream _SaveData = new MemoryStream();

switch (m_Image.PixelFormat)
{
#region 8位
case PixelFormat.Format8bppIndexed:
m_Head.Colour_Planes = 1;
BitmapData _ImageData = m_Image.LockBits(new Rectangle(0, 0, m_Head.Width, m_Head.Height), ImageLockMode.ReadOnly, m_Image.PixelFormat);
byte[] _ImageByte = new byte[_ImageData.Stride * _ImageData.Height];
Marshal.Copy(_ImageData.Scan0, _ImageByte, 0, _ImageByte.Length);
m_Image.UnlockBits(_ImageData);

m_SaveIndex = 0;
byte[] _RowBytes = SavePCXLine8(_ImageByte);
_SaveData.Write(_RowBytes, 0, _RowBytes.Length);

_SaveData.WriteByte(0x0C);
for (int i = 0; i != 256; i++)
{
_SaveData.WriteByte((byte)m_Image.Palette.Entries[i].R);
_SaveData.WriteByte((byte)m_Image.Palette.Entries[i].G);
_SaveData.WriteByte((byte)m_Image.Palette.Entries[i].B);
}
break;
#endregion
#region 其他都按24位保存
default:
...全文
55 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧