当前位置: 移动技术网 > IT编程>开发语言>.net > C# 解析 RAS文件 SUM 光栅文件图象的代码

C# 解析 RAS文件 SUM 光栅文件图象的代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

cad2006教程,k747,惊门 笔趣阁

使用方法:
复制代码 代码如下:

imageras _ras = new imageras(@"d:\temp\test.ras");
picturebox1.image = _ras.image;
_ras.saveras(@"d:\temp\ok.ras");

我只实现了24位色和8位色 这个结构也太简单了。只有文件头和数据区 。就是8位色的色彩表有些特殊
先是红色表 绿色表 蓝色表 平时都是 rgb、rgb 这样放 这东西居然rrrr.....ggg......b....
不知道怎么想的。
项目多了很少有时间做这些东西了。下个目标是iff文件
全部代码
复制代码 代码如下:

using system;
using system.collections.generic;
using system.text;
using system.runtime.interopservices;
using system.drawing.imaging;
using system.drawing;
using system.io;
namespace zgke.myimage.imagefile
{
/// <summary>
/// sun光栅图形 ras
/// zgke@sina.com
/// qq:116149
/// </summary>
public class imageras
{
public imageras(string p_imagefile)
{
if (system.io.file.exists(p_imagefile))
{
loadimage(system.io.file.readallbytes(p_imagefile));
}
}
public imageras()
{
}
#region 私有
/// <summary>
/// 文件头 956aa659
/// </summary>
private uint m_mageic = 0x956aa659;
/// <summary>
/// 宽
/// </summary>
private uint m_width = 0;
/// <summary>
/// 高
/// </summary>
private uint m_height = 0;
/// <summary>
/// 颜色深
/// </summary>
private uint m_depth = 0;
/// <summary>
/// 图形区域数据大小
/// </summary>
private uint m_length = 0;
/// <summary>
/// 数据类型
/// </summary>
private uint m_type = 0;
/// <summary>
/// 色彩图形类型
/// </summary>
private uint m_maptype = 0;
/// <summary>
/// 色彩长度
/// </summary>
private uint m_maplength = 0;
/// <summary>
/// 颜色表
/// </summary>
private color[] m_colorlist = new color[256];
/// <summary>
/// 图形
/// </summary>
private bitmap m_image;
#endregion
/// <summary>
/// 获取图形
/// </summary>
public bitmap image
{
get
{
return m_image;
}
set
{
if (value != null)
{
m_image = value;
m_width = (uint)value.width;
m_height = (uint)value.height;
switch (value.pixelformat)
{
case pixelformat.format8bppindexed:
break;
case pixelformat.format32bppargb:
break;
default:
m_depth = 24;
break;
}
}
}
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="p_imagebytes"></param>
private void loadimage(byte[] p_imagebytes)
{
if (bitconverter.touint32(p_imagebytes, 0) != m_mageic) throw new exception("文件头不正确!");
m_width = bytestouint(p_imagebytes, 4);
m_height = bytestouint(p_imagebytes, 8);
m_depth = bytestouint(p_imagebytes, 12);
m_length = bytestouint(p_imagebytes, 16);
m_type = bytestouint(p_imagebytes, 20);
m_maptype = bytestouint(p_imagebytes, 24);
m_maplength = bytestouint(p_imagebytes, 28);
int _starindex = 32;
switch (m_maptype)
{
case 1:
int _colortable = (int)m_maplength / 3;
for (int i = 0; i != _colortable; i++)
{
m_colorlist[i] = color.fromargb(p_imagebytes[_starindex], p_imagebytes[_starindex + _colortable], p_imagebytes[_starindex + (_colortable * 2)]);
_starindex++;
}
_starindex += _colortable * 2;
break;
default:
break;
}
loaddata(p_imagebytes, _starindex);
}
/// <summary>
/// 字节转换为uint
/// </summary>
/// <param name="p_value">字节数组</param>
/// <param name="p_index">开始位置</param>
/// <returns></returns>
private uint bytestouint(byte[] p_value, int p_index)
{
byte[] _valuebytes = new byte[4];
_valuebytes[0] = p_value[p_index + 3];
_valuebytes[1] = p_value[p_index + 2];
_valuebytes[2] = p_value[p_index + 1];
_valuebytes[3] = p_value[p_index];
return bitconverter.touint32(_valuebytes, 0);
}
/// <summary>
/// 获取反转的bytes
/// </summary>
/// <param name="p_value"></param>
/// <returns></returns>
private byte[] uinttobytes(uint p_value)
{
byte[] _valuebytes = bitconverter.getbytes(p_value);
array.reverse(_valuebytes);
return _valuebytes;
}
/// <summary>
/// 获取图形数据
/// </summary>
/// <param name="p_valuebytes">文件留</param>
/// <param name="p_starindex">rgb留开始位置</param>
private void loaddata(byte[] p_valuebytes, int p_starindex)
{
pixelformat _format = pixelformat.format24bpprgb;
switch (m_depth)
{
case 8:
_format = pixelformat.format8bppindexed;
break;
case 24:
_format = pixelformat.format24bpprgb;
break;
default:
throw new exception("未实现!");
}
m_image = new bitmap((int)m_width, (int)m_height, _format);
bitmapdata _data = m_image.lockbits(new rectangle(0, 0, m_image.width, m_image.height), imagelockmode.readwrite, m_image.pixelformat);
byte[] _writebytes = new byte[_data.height * _data.stride];
int _starindex = 0;
int _writeindex = 0;
for (int i = 0; i != _data.height; i++)
{
_writeindex = i * _data.stride;
_starindex = i * ((int)m_length / (int)m_height) + p_starindex;
for (int z = 0; z != _data.width; z++)
{
switch (m_depth)
{
case 8:
_writebytes[_writeindex] = p_valuebytes[_starindex];
_writeindex++;
_starindex++;
break;
case 24:
_writebytes[_writeindex] = p_valuebytes[_starindex + 2];
_writebytes[_writeindex + 1] = p_valuebytes[_starindex + 1];
_writebytes[_writeindex + 2] = p_valuebytes[_starindex];
_writeindex += 3;
_starindex += 3;
break;
}
}
}
switch (m_depth)
{
case 8:
colorpalette _palette = m_image.palette;
for (int i = 0; i != m_colorlist.length; i++)
{
_palette.entries[i] = m_colorlist[i];
}
m_image.palette = _palette;
break;
default:
break;
}
marshal.copy(_writebytes, 0, _data.scan0, _writebytes.length);
m_image.unlockbits(_data);
}
/// <summary>
/// 保存图形
/// </summary>
/// <returns></returns>
private byte[] saveimageofras()
{
if (m_image == null) return new byte[0];
memorystream _stream = new memorystream();
_stream.write(bitconverter.getbytes(m_mageic), 0, 4);
_stream.write(uinttobytes(m_width), 0, 4);
_stream.write(uinttobytes(m_height), 0, 4);
switch (m_depth)
{
case 8:
bitmapdata _data256 = m_image.lockbits(new rectangle(0, 0, m_image.width, m_image.height), imagelockmode.readonly, m_image.pixelformat);
byte[] _databytes = new byte[_data256.stride * _data256.height];
int _stride = _data256.stride;
marshal.copy(_data256.scan0, _databytes, 0, _databytes.length);
m_image.unlockbits(_data256);
_stream.write(uinttobytes(8), 0, 4);
uint _widthcount = (uint)m_image.width;
if (m_image.width % 2 != 0) _widthcount = (uint)m_image.width + 1;
uint _allcount = _widthcount * (uint)m_image.height;
_stream.write(uinttobytes(_allcount), 0, 4);
_stream.write(uinttobytes(0), 0, 4);
_stream.write(uinttobytes(1), 0, 4);
_stream.write(uinttobytes(768), 0, 4);
byte[] _redbytes = new byte[256];
byte[] _greenbytes = new byte[256];
byte[] _bluebytes = new byte[256];
for (int i = 0; i != 256; i++)
{
_redbytes[i] = m_image.palette.entries[i].r;
_greenbytes[i] = m_image.palette.entries[i].g;
_bluebytes[i] = m_image.palette.entries[i].b;
}
_stream.write(_redbytes, 0, _redbytes.length);
_stream.write(_greenbytes, 0, _greenbytes.length);
_stream.write(_bluebytes, 0, _bluebytes.length);
byte[] _write = new byte[_widthcount];
for (int i = 0; i != m_image.height; i++)
{
array.copy(_databytes, i * _stride, _write, 0, _widthcount);
_stream.write(_write, 0, _write.length);
}
break;
default:
bitmap _newbitmap = new bitmap(m_image.width, m_image.height, pixelformat.format24bpprgb);
graphics _graphics = graphics.fromimage(_newbitmap);
_graphics.drawimage(m_image, new rectangle(0, 0, m_image.width, m_image.height));
_graphics.dispose();
bitmapdata _data24 = _newbitmap.lockbits(new rectangle(0, 0, _newbitmap.width, _newbitmap.height), imagelockmode.readonly, _newbitmap.pixelformat);
byte[] _databytes24 = new byte[_data24.stride * _data24.height];
int _stride24 = _data24.stride;
marshal.copy(_data24.scan0, _databytes24, 0, _databytes24.length);
_newbitmap.unlockbits(_data24);
_stream.write(uinttobytes(24), 0, 4);
uint _widthcount24 = (uint)_newbitmap.width;
if (_newbitmap.width % 2 != 0) _widthcount24 = (uint)_newbitmap.width + 1;
uint _allcount24 = _widthcount24 * (uint)_newbitmap.height * 3;
_widthcount24 = _widthcount24 * 3;
_stream.write(uinttobytes(_allcount24), 0, 4);
_stream.write(uinttobytes(0), 0, 4);
_stream.write(uinttobytes(0), 0, 4);
_stream.write(uinttobytes(0), 0, 4);
byte[] _write24 = new byte[0];
for (int i = 0; i != m_image.height; i++)
{
_write24 = new byte[_widthcount24];
int _writeindex = 0;
int _starindex = i * _stride24;
for (int z = 0; z != m_image.width; z++)
{
_write24[_writeindex] = _databytes24[_starindex + 2];
_write24[_writeindex + 1] = _databytes24[_starindex + 1];
_write24[_writeindex + 2] = _databytes24[_starindex];
_writeindex += 3;
_starindex += 3;
}
_stream.write(_write24, 0, _write24.length);
}
_newbitmap.dispose();
break;
}
byte[] _return = _stream.toarray();
return _return;
}
/// <summary>
/// 保存图形到ras文件
/// </summary>
/// <param name="p_file"></param>
public void saveras(string p_file)
{
byte[] _value = saveimageofras();
if (_value.length != 0) file.writeallbytes(p_file, _value);
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网