您的位置:首页 > 编程语言 > C#

C# 解析 RSB图形结构

2010-04-10 10:13 393 查看
      文件结构非常简单 ,就是 16位图   R使用 5位  G使用6位  B使用5位
           //显示RSB文件
            ImageRsb _RSB = new ImageRsb(@"D:/temp/4.rsb");
            pictureBox1.Image = _RSB.Image;

            //保存
            ImageRsb _RSB = new ImageRsb();
            _RSB.Image = (Bitmap)Image.FromFile(@"d:/TEMP/1.bmp");
            _RSB.SaveImage(@"d:/temp/ok.rsb");

     这个文件不知道ACDSEE为什么标记为24位图形 实际只有16位。 如果谁有这个格式的文档请发到zgke@sina.com谢谢拉 

 

 

全部代码

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing;

namespace Zgke.MyImage.ImageFile
{
*/
/// <summary>
/// RSB文件结构
/// zgke@sina.com
/// QQ:116149
/// </summary>
public class ImageRsb
{
private Bitmap m_Bitmap;

public Bitmap Image { get { return m_Bitmap; } set { m_Bitmap = value; } }

private uint m_Heard = 0;

private uint m_Width = 0;

private uint m_Height = 0;

private uint m_No1 = 0;

private uint m_No2 = 5;

private uint m_No3 = 6;

private uint m_No4 = 5;

private uint m_No5 = 0;

public ImageRsb()
{
}

public ImageRsb(string p_FileName)
{
if (!File.Exists(p_FileName)) throw new Exception("文件不存在!");
byte[] _Bytes = File.ReadAllBytes(p_FileName);
if (BitConverter.ToUInt32(_Bytes, 0) != 0) throw new Exception("格式不正确!");
m_Width = BitConverter.ToUInt32(_Bytes, 4);
m_Height = BitConverter.ToUInt32(_Bytes, 8);
m_No1 = BitConverter.ToUInt32(_Bytes, 12);
m_No2 = BitConverter.ToUInt32(_Bytes, 16);
m_No3 = BitConverter.ToUInt32(_Bytes, 20);
m_No4 = BitConverter.ToUInt32(_Bytes, 24);
m_No5 = BitConverter.ToUInt32(_Bytes, 28);
if (m_No1 != 0 || m_No2 != 5 || m_No3 != 6 || m_No4 != 5 || m_No5 != 0) throw new Exception("未实现!");
if (m_Width * m_Height * 2 != _Bytes.Length - 32) throw new Exception("文件长度不正确!");
LoadImage(_Bytes, 32);
}

/// <summary>
/// 获取图形数据
/// </summary>
/// <param name="p_FileBytes"></param>
/// <param name="p_ReadIndex"></param>
private void LoadImage(byte[] p_FileBytes, int p_ReadIndex)
{
m_Bitmap = new Bitmap((int)m_Width, (int)m_Height, PixelFormat.Format16bppRgb565);
BitmapData _BitmapData = m_Bitmap.LockBits(new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), ImageLockMode.ReadWrite, m_Bitmap.PixelFormat);
byte[] _WriteBytes = new byte[_BitmapData.Stride * _BitmapData.Height];
int _StarReadIndex = p_ReadIndex;
int _RowCount = (int)m_Width * 2;
for (int i = 0; i != _BitmapData.Height; i++)
{
Array.Copy(p_FileBytes, _StarReadIndex, _WriteBytes, i * _BitmapData.Stride, _RowCount);
_StarReadIndex += _RowCount;
}
Marshal.Copy(_WriteBytes, 0, _BitmapData.Scan0, _WriteBytes.Length);
m_Bitmap.UnlockBits(_BitmapData);
}

/// <summary>
/// 保存数据到文件
/// </summary>
/// <param name="p_File"></param>
public void SaveImage(string p_File)
{
if (m_Bitmap == null) return;
System.IO.File.WriteAllBytes(p_File, GetFileBytes());
}

/// <summary>
/// 保存图形
/// </summary>
/// <returns></returns>
private byte[] GetFileBytes()
{
MemoryStream _Stream = new MemoryStream();
_Stream.Write(BitConverter.GetBytes(m_Heard), 0, 4);
_Stream.Write(BitConverter.GetBytes((uint)m_Bitmap.Width), 0, 4);
_Stream.Write(BitConverter.GetBytes((uint)m_Bitmap.Height), 0, 4);
_Stream.Write(BitConverter.GetBytes(m_No1), 0, 4);
_Stream.Write(BitConverter.GetBytes(m_No2), 0, 4);
_Stream.Write(BitConverter.GetBytes(m_No3), 0, 4);
_Stream.Write(BitConverter.GetBytes(m_No4), 0, 4);
_Stream.Write(BitConverter.GetBytes(m_No5), 0, 4);
byte[] _ImageBytes = SaveImage();
_Stream.Write(_ImageBytes, 0, _ImageBytes.Length);
return _Stream.ToArray();
}

/// <summary>
/// 获取图形数据区
/// </summary>
/// <returns></returns>
private byte[] SaveImage()
{
Bitmap _NewBitmap = new Bitmap(m_Bitmap.Width, m_Bitmap.Height, PixelFormat.Format16bppRgb565);
byte[] _WriteBytes = new byte[m_Bitmap.Width * m_Bitmap.Height * 2];
int _RowCount = m_Bitmap.Width * 2;

Graphics _Graphcis = Graphics.FromImage(_NewBitmap);
_Graphcis.DrawImage(m_Bitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
_Graphcis.Dispose();

BitmapData _Data = _NewBitmap.LockBits(new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height), ImageLockMode.ReadOnly, _NewBitmap.PixelFormat);
byte[] _ReadBytes = new byte[_Data.Stride * _Data.Height];
Marshal.Copy(_Data.Scan0, _ReadBytes, 0, _ReadBytes.Length);
_NewBitmap.UnlockBits(_Data);
for (int i = 0; i != _Data.Height; i++)
{
Array.Copy(_ReadBytes, i * _Data.Stride, _WriteBytes, i * _RowCount, _RowCount);
}

_NewBitmap.Dispose();
return _WriteBytes;
}

}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息