Create Image From A Binary File?

1.1k views Asked by At

I Need help to create an image preview from A Riped Binary File in C#.

The File Having the Following information in first 48 byte.

nSignature; // 0x1234 (DATA in 4-byte )

nXDPI; // Image XDPI (DATA in 4-byte )

nYDPI; // Image YDPI (DATA in 4-byte )

nBytesPerLine; //1 Line Data Length /Byte (DATA in 4-byte )

nHeight; //Image Height (DATA in 4-byte )

nWidth; //Image Width (DATA in 4-byte )

nPaperWidth; //No Use. 25: CaoXueShi (DATA in 4-byte )

nColors; //1=monochrome 4: CMYK (DATA in 4-byte )

nBits; //No Use for most printer //Techwin: PassNum (DATA in 4-byte )

nReserved[3]; //0: Pass Number, 1: vsdMode //DWORD VsdMode;(DATA in 12-byte )

after the 48 byte information the color information stored.

First line

KKKKKKKKKKKKKKKKKKKK

CCCCCCCCCCCCCCCCCCCC

MMMMMMMMMMMM

YYYYYYYYYYYYYYYYYYYYYY

Second line

KKKKKKKKKKKKKKKKKKKK

CCCCCCCCCCCCCCCCCCCC

MMMMMMMMMMMM

YYYYYYYYYYYYYYYYYYYYYY ….

the nBytesPerLine define the number of byte in each line that is how many bytes for k ,c,m and y.

and the nHeight define the numbers of line of color information.

now using the above information i want to create an image preview in C#

You may download a binary file from here Download

Please Help

1

There are 1 answers

0
Abhishek Singh On

Hi Every One Finally solve my Problem

First OF All Create A Class File 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
        } 
    }

Now Create A Window Form And Add picturbox,label,Filedailog and A Button

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);
        }

Output Screen

Reference