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

 更新时间:2010年03月30日 12:04:56   作者:   我要评论
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
{
/// <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);
}
}
}

相关文章

  • DropDownList添加客户端下拉事件操作

    DropDownList添加客户端下拉事件操作

    我们知道,DropDownList下拉框是一个服务器控件,有时候,有些朋友为了方便绑定DropDownList下拉框的选项,但又想在DropDownList实现客户端的下拉事件,那该怎么实现呢?
    2015-09-09
  • .NET性能调优之一:ANTS Performance Profiler的使用介绍

    .NET性能调优之一:ANTS Performance Profiler的使用介绍

    本系列文章主要会介绍一些.NET性能调优的工具、Web性能优化的规则(如YSlow)及方法等等内容。成文前最不希望看到的就是园子里不间断的“哪个语言好,哪个语言性能高”的争论,不多说,真正的明白人都应该知道这样的争论有没有意义,希望我们能从实际性能优化的角度去讨论问题
    2013-01-01
  • asp.net Bundle功能扩展

    asp.net Bundle功能扩展

    发现这个东西确实非常实用,且功能强大,BundleTable.Bundles能够压缩合并js和CSS,但是目前的使用起来不是特别好需要修改BundleConfig的代码
    2012-11-11
  • .Net Core中使用Quartz.Net实践记录

    .Net Core中使用Quartz.Net实践记录

    Quartz.NET是一套很好的任务调度框架。下面这篇文章主要给大家介绍了关于.Net Core中使用Quartz.Net的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2018-09-09
  • jQuery AJax调用asp.net webservers的实现代码

    jQuery AJax调用asp.net webservers的实现代码

    代码是转载来的 本来今天写的 但是到现在还没搞懂,惭愧啊
    2009-12-12
  • asp.net DataGrid 中文字符排序的实现代码

    asp.net DataGrid 中文字符排序的实现代码

    在论坛上看到有位朋友希望对中文按拼音进行排序,刚好最近有点空,贴一份原来一个同事写的一个排序类,仅稍微改动了下下,拿出来分享下.
    2009-09-09
  • EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例

    EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例

    本篇文章主要是对EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-02-02
  • WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法

    WebService出现"因 URL 意外地以 结束,请求格式无法识别&q

    因 URL 意外地以“/GetReceivedInvoices”结束,请求格式无法识别。
    2009-01-01
  • .net 反序题目的详细解答

    .net 反序题目的详细解答

    在各种答案,以及平时面试过程中,这道题总归会有一些非常典型的错误发生。其中给老赵的感觉也非常有意思,不知其中的“思路”是否如老赵猜测那样。
    2009-06-06
  • ASP.NET在IIS上注册报0x800702e4错误解决方法

    ASP.NET在IIS上注册报0x800702e4错误解决方法

    报一个0x800702e4 请求的操作需要提升的错误。解决的方法和前面大同小异,给这个aspnet_regiis.exe创建一个快捷方式,给它的目标后面加上 一个-i,再右击这个快捷方式,以管理员身份运行,就行了
    2012-08-08

最新评论