热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

C#解析RAS文件(SUM光栅文件图象)我只实现了24位色和8位色这个结构也太简单了。只有文件头和数据区。
使用方法:
代码如下:

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
{
///
/// SUN光栅图形 RAS
/// zgke@sina.com
/// qq:116149
///

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 私有
///
/// 文件头 956AA659
///

private uint m_Mageic = 0x956AA659;
///
/// 宽
///

private uint m_Width = 0;
///
/// 高
///

private uint m_Height = 0;
///
/// 颜色深
///

private uint m_Depth = 0;
///
/// 图形区域数据大小
///

private uint m_Length = 0;
///
/// 数据类型
///

private uint m_Type = 0;
///
/// 色彩图形类型
///

private uint m_MapType = 0;
///
/// 色彩长度
///

private uint m_MapLength = 0;
///
/// 颜色表
///

private Color[] m_ColorList = new Color[256];
///
/// 图形
///

private Bitmap m_Image;
#endregion
///
/// 获取图形
///

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;
}
}
}
}
///
/// 获取数据
///

///
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);
}
///
/// 字节转换为UINT
///

/// 字节数组
/// 开始位置
///
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);
}
///
/// 获取反转的BYTES
///

///
///
private byte[] UintToBytes(uint p_Value)
{
byte[] _ValueBytes = BitConverter.GetBytes(p_Value);
Array.Reverse(_ValueBytes);
return _ValueBytes;
}
///
/// 获取图形数据
///

/// 文件留
/// RGB留开始位置
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);
}
///
/// 保存图形
///

///
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;
}
///
/// 保存图形到RAS文件
///

///
public void SaveRas(string p_File)
{
byte[] _Value = SaveImageOfRas();
if (_Value.Length != 0) File.WriteAllBytes(p_File, _Value);
}
}
}

推荐阅读
  • C#实现chart控件图表的漫游
    C#中的chart控件是非常强大的,可以轻松实现数据的可视化,用 ... [详细]
  • Linux之——五种IO模型
    一.同步与异步之前在对线程的谈论中提到了线程对临界资源访问的一个同步与互斥的关系,这里要强调,在IO模型中的同步与异步与线程的同步与互斥完全不是一回事。所谓同步,就是指当调用者发出 ... [详细]
  • 栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出(Last-in-first-out,LIFO)的原则。java本身是有自带Stack类包,为了达到学习目的已经 ... [详细]
  • s网页可见区域宽:document.body.clientWidth; s网页可见区域高:document.body.clientHeight; s网页可见区域宽:do ... [详细]
  • 钟声清禁才应彻,漏报仙闱俨已开。双阙薄烟笼菡萏,九成初日照蓬莱。朝时但向丹墀拜,仗下方从碧殿回。圣道逍遥更何事,愿将巴曲赞康哉。 ... [详细]
  • 绿莺庭院燕莺啼。绣帘垂。瑞烟霏。一片笙箫,声过彩云低。疑是蕊宫仙子降,翻玉袖,舞瑶姬。冰姿玉质自清奇。看孙枝。列班衣。画鼓新歌,喜映两疏眉。袖里蟠桃花露湿,应不惜,醉金卮 ... [详细]
  • windows环境下的eclipse操作虚拟机里面的hadoop相关配置
    当电脑的配置不是很高的时候,在虚拟机里面安装上编译软件进行编程的话,卡的要命,所以总结一下在windows环境下eclipse配置链接虚拟机中的hadoop在虚拟机中的hadoop ... [详细]
  • 1.蚯蚓蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓。蛐蛐国里现在共有\(n\)只蚯蚓(\(n\)为正整数)。每只蚯蚓拥有长度,我 ... [详细]
  • ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API
    本篇尝试在现有的ASP.NETMVC4项目上增加使用ASP.NETWebAPI。新建项目,选择ASP.NETMVC4Web应用程序。选择基本项目模版。在Controller ... [详细]
  • 微软的更新补丁在8月已经修补了由系统或安全软件。Itmaycauseuserstorestartthe0xc0000005,0xc0000008ebluescreenaft ... [详细]
  • 增强输出的电路——射极跟随器
    共发射极放大电路输出电阻高,容易受到作为负载所接电路的影响。射极跟随器(共集电极放大电路),其发射极跟随着输入信号(基极电位)进行工作,输入阻抗高,输出阻抗低。射极跟随器大多用在电 ... [详细]
  • 基本字义解释 ... [详细]
  • python基础一如何统计一个列表元素的频度
    如何统计一个列表元素的频度两个需求:两种方法:1,普通的for循环,结合前边python基础一如何在列表字典集合中根据条件筛选数据的内容2,自带库collections的count ... [详细]
  • 导读:很多朋友问到关于php怎么调用私有方法吗的相关问题,本文编程笔记就来为大家做个详细解答,供大家参考,希望对大家有所帮助!一起来看看吧!本文目录一览: ... [详细]
  • JS/TS算法—排序算法
    快速排序快速排序也许是最常用的排序算法了。它的复杂度为O(nlog(n)),且性能通常比其他复杂度为O(nlog(n))的排序算法要好。和归并排序一样,快速排序也使用分而治之的方法 ... [详细]
author-avatar
手机用户2602915241
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有