我需要帮助从C#中的Riped Binary文件创建图像预览。
文件在前48个字节中包含以下信息。
n签名; // 0x1234(4字节数据)
nXDPI; //图像XDPI(4字节数据)
nYDPI; //图像YDPI(4字节数据)
nBytesPerLine; // 1行数据长度/字节(4字节数据)
nHeight参数; //图像高度(4字节数据)
nWidth; //图像宽度(4字节数据)
nPaperWidth; //没用25:CaoXueShi(4字节数据)
NCOLORS; // 1 =单色4:CMYK(4字节数据)
NBITS; //不使用大多数打印机// Techwin:PassNum(4字节数据 )
nReserved [3]; // 0:传递数字,1:vsdMode // DWORD VsdMode;(DATA in 12字节)
在48字节信息之后存储颜色信息。
第一行
KKKKKKKKKKKKKKKKKKKK
CCCCCCCCCCCCCCCCCCCC
MMMMMMMMMMMM
YYYYYYYYYYYYYYYYYYYYYY
第二行
KKKKKKKKKKKKKKKKKKKK
CCCCCCCCCCCCCCCCCCCC
MMMMMMMMMMMM
YYYYYYYYYYYYYYYYYYYYYYY ....
nBytesPerLine 定义每行中的字节数,即k,c,m和y的字节数。
和 nHeight 定义颜色信息行的数量。
现在使用上述信息我想在C#中创建图像预览
您可以从此处下载二进制文件 Download
请帮助
答案 0 :(得分:2)
大家好,最后解决我的问题
首先创建一个类文件RGB
using System;
using System.ComponentModel;
namespace CMYK_RGB
{
public struct RGB
{
/// <summary>
/// Gets an empty RGB structure;
/// </summary>
public static readonly RGB Empty = new RGB();
#region Fields
private int red;
private int green;
private int blue;
#endregion
#region Operators
public static bool operator ==(RGB item1, RGB item2)
{
return (
item1.Red == item2.Red
&& item1.Green == item2.Green
&& item1.Blue == item2.Blue
);
}
public static bool operator !=(RGB item1, RGB item2)
{
return (
item1.Red != item2.Red
|| item1.Green != item2.Green
|| item1.Blue != item2.Blue
);
}
#endregion
#region Accessors
[Description("Red component."),]
public int Red
{
get
{
return red;
}
set
{
red = (value>255)? 255 : ((value<0)?0 : value);
}
}
[Description("Green component."),]
public int Green
{
get
{
return green;
}
set
{
green = (value>255)? 255 : ((value<0)?0 : value);
}
}
[Description("Blue component."),]
public int Blue
{
get
{
return blue;
}
set
{
blue = (value>255)? 255 : ((value<0)?0 : value);
}
}
#endregion
public RGB(int R, int G, int B)
{
red = (R>255)? 255 : ((R<0)?0 : R);
green = (G>255)? 255 : ((G<0)?0 : G);
blue = (B>255)? 255 : ((B<0)?0 : B);
}
#region Methods
public override bool Equals(Object obj)
{
if(obj==null || GetType()!=obj.GetType()) return false;
return (this == (RGB)obj);
}
public override int GetHashCode()
{
return Red.GetHashCode() ^ Green.GetHashCode() ^ Blue.GetHashCode();
}
#endregion
}
}
现在创建一个窗口窗体并添加图标框,标签,Filedailog和A按钮
private void button1_Click(object sender, EventArgs e)
{
int size = -1;
openFileDialog1.Title = "Open Prt File";
openFileDialog1.InitialDirectory = @"C:\";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
label1.Text = file;
try
{
FileStream stream = File.OpenRead(@file);
byte[] NumberOfByteprLine = new byte[4];
byte[] NumberOfLineInImage = new byte[4];
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
int j = 19;
Int64 decValue;
decValue = buffer[j--] * 256 * 256 * 256;
decValue += buffer[j--] * 256 * 256;
decValue += buffer[j--] * 256;
decValue += buffer[j--];
Int32 ImageHeight = Convert.ToInt32(decValue);
j = 15;
decValue = buffer[j--] * 256 * 256 * 256;
decValue += buffer[j--] * 256 * 256;
decValue += buffer[j--] * 256;
decValue += buffer[j--];
Int32 NumberOfBytePerLine = Convert.ToInt32(decValue);
j = 23;
decValue = buffer[j--] * 256 * 256 * 256;
decValue += buffer[j--] * 256 * 256;
decValue += buffer[j--] * 256;
decValue += buffer[j--];
Int32 ImageWidth = Convert.ToInt32(decValue);
string hexXResValue = string.Empty, hexYResValue = string.Empty;
for (int i = 11; i > 3; i--)
{
if (i > 7)
{
hexYResValue = hexYResValue + Convert.ToInt64(buffer[i]).ToString("X");
}
else
{
hexXResValue = hexXResValue + Convert.ToInt64(buffer[i]).ToString("X");
}
}
//Int32 ImageWidth = int.Parse(hexWValue, System.Globalization.NumberStyles.HexNumber);
//Int32 ImageHeight = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
Int32 XResolution = int.Parse(hexXResValue, System.Globalization.NumberStyles.HexNumber);
Int32 YResolution = int.Parse(hexYResValue, System.Globalization.NumberStyles.HexNumber);
Int64 StartByte = 48;
int width = NumberOfBytePerLine, hieght = ImageHeight;
Bitmap bmp = new Bitmap(width, hieght);
int value = 0;
for(int i=0; i<ImageHeight;i++)
{
for (Int32 k = 0; k < NumberOfBytePerLine; k++)
{
decValue = buffer[StartByte];
string hexValue = decValue.ToString("X");
int Y = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
decValue = buffer[StartByte + NumberOfBytePerLine];
hexValue = decValue.ToString("X");
int M = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
decValue = buffer[StartByte + NumberOfBytePerLine*2];
hexValue = decValue.ToString("X");
int C = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
decValue = buffer[StartByte + NumberOfBytePerLine*3];
hexValue = decValue.ToString("X");
int K = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
RGB rgb = CMYKtoRGB((double)C / 100.0, (double)M / 100.0, (double)Y / 100.0, (double)K / 100.0);
StartByte++;
bmp.SetPixel(k, i, Color.FromArgb(rgb.Red, rgb.Green, rgb.Blue));
value++;
}
StartByte = StartByte + (NumberOfBytePerLine * 3);
}
pictureBox1.Image = bmp;
string Path = "C:\\Users\\Abhishek Singh\\Desktop\\PrtFile\\prtfiles\\imageFile.bmp";
bmp.Save(Path);
}
catch { }
}
}
/// <summary>
/// Converts CMYK to RGB.
/// </summary>
/// <param name="c">Cyan value (must be between 0 and 1).</param>
/// <param name="m">Magenta value (must be between 0 and 1).</param>
/// <param name="y">Yellow value (must be between 0 and 1).</param>
/// <param name="k">Black value (must be between 0 and 1).</param>
public static RGB CMYKtoRGB(double c, double m, double y, double k)
{
int red = Convert.ToInt32((1.0 - c) * (1.0 - k) * 255.0);
int green = Convert.ToInt32((1.0 - m) * (1.0 - k) * 255.0);
int blue = Convert.ToInt32((1.0 - y) * (1.0 - k) * 255.0);
return new RGB(red, green, blue);
}