How to convert Ram data to BMP format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishp
    New Member
    • Feb 2008
    • 2

    How to convert Ram data to BMP format

    Hi,

    I need to convert raw data recieved from a device through C#.Net socket to BMP format.
    For some reason the image conversion is not happening proparly and I can see only blur image .

    Following is the code :
    Code:
    try
                                {
                                    string receivedPath = "c:\\NEW\\";
                                    int receivedBytesLen=0;
                                    int bufstart = 0;
                                   
                                    while (test > 0)
                                    {
    
                                        receivedBytesLen = clientSock.Receive(Recievbuf);
                                       
                                        int fileNameLen = BitConverter.ToChar(Recievbuf, 0);
                                        string fileName = "image1.text";//Encoding.ASCII.GetString(Recievbuf, 4, fileNameLen);
                                        // MessageBox.Show("Client:{0} connected & File {1} started received." + clientSock.RemoteEndPoint + fileName);
                                        bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append));
                                        //bWrite.Write(Recievbuf, (int)(352 * 240 * 2 - test), receivedBytesLen);
                                        bWrite.Write(Recievbuf, bufstart, receivedBytesLen);
                                        
                                        // MessageBox.Show("File: {0} received & saved at path: {1}" + fileName + receivedPath);
                                        
                                        bufstart += receivedBytesLen;
                                        test -= receivedBytesLen;    
                                      
                                        bWrite.Close();
                                    }
                                    clientSock.Close();
                                    
                                    
                                    stbImage = bMPCon(Recievbuf);
                                    stbImage.Save("c:\\NEW\\myimage.bmp", ImageFormat.Bmp);
    =============== ========
    Code:
    public Bitmap bMPCon(byte[] data)
             {
                //Here create the Bitmap to the know height, width and format
                Bitmap bmp = new Bitmap(352,240,PixelFormat.Format24bppRgb);
    
             
    
                
                //Create a BitmapData and Lock all pixels to be written 
                /*BitmapData bmpData = bmp.LockBits(
                                     new Rectangle(0, 0, bmp.Width, bmp.Height),
                                     ImageLockMode.WriteOnly, bmp.PixelFormat);*/
    
                BitmapData bmpData = bmp.LockBits(
                                     new Rectangle(0, 0, 352, 240),
                                     ImageLockMode.WriteOnly, bmp.PixelFormat);
    
              
    
                //Copy the data from the byte array into BitmapData.Scan0
                Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
               //Unlock the pixels
                bmp.UnlockBits(bmpData);
                //Return the bitmap 
                return bmp;
            }
    Following is the MFC code which does the job of conversion. Infact I have tried to use by creating dll but I am getting memory corruption exception.

    Code:
    #define GETALPHA(X) (X>>15)
    #define GETRED(X) ((X<<1+16)>>11+16)
    #define GETGREEN(X) ((X<<6+16)>>11+16)
    #define GETBLUE(X) ((X<<11+16)>>11+16)
    
    Conver2BMP(CString DestPATH,CString ImageName,CString ImageFileName)
    {
    
    	CString Filename;
    	CFile ImageFilePtr;
    	CFile ImagePtr;
    
    	BITMAPFILEHEADER hdr;
    	BITMAPINFOHEADER bmpHeader; 
    	BITMAPCOREHEADER chdr;
    
    	Filename.Empty();
    	Filename.Append(DestPATH);
    	Filename.Append("\\");
    	Filename.Append(ImageFileName);
    
    	ImageFilePtr.Open(Filename,CFile::modeRead,0);
    	unsigned long length = ImageFilePtr.GetLength();
    
    	ImageFilePtr.Read(bmpRaw,length);
    
    	for(int i=0; i<240; i++)
    	{
    		for(int j=0; j<352; j++)
    		{
    			unsigned long col1555 = bmpRaw[((i*352)+j)];
    			unsigned char cA = GETALPHA(col1555);
    			unsigned char cR = GETRED(col1555);
    			unsigned char cG = GETGREEN(col1555);
    			unsigned char cB = GETBLUE(col1555);
    
    			bmpData[(((((239-i)*352)+(j)))*3)+0] = cB*8;
    			bmpData[(((((239-i)*352)+(j)))*3)+1] = cG*8;
    			bmpData[(((((239-i)*352)+(j)))*3)+2] = cR*8;
    
    		}
    
    		chdr.bcBitCount = 24;
    		chdr.bcHeight = 240;
    		chdr.bcPlanes = 1;
    		chdr.bcSize = 240*352*3;
    		chdr.bcWidth = 352;
    
    		memset(&bmpHeader,0,sizeof(BITMAPINFOHEADER));
    		memset(&hdr,0,sizeof(BITMAPFILEHEADER));
    
    
    		bmpHeader.biPlanes = 1; 
    		bmpHeader.biXPelsPerMeter = 0; 
    		bmpHeader.biYPelsPerMeter = 0; 
    		bmpHeader.biClrImportant = 0;
    		bmpHeader.biCompression = BI_RGB;
    		bmpHeader.biBitCount = 24; 
    		bmpHeader.biSize = sizeof(BITMAPINFOHEADER);
    		bmpHeader.biHeight = 240;
    		bmpHeader.biWidth = 352;
    		bmpHeader.biSizeImage = 0;//bmpHeader.biWidth * bmpHeader.biHeight * 3;
    
    		hdr.bfType = 0x4d42; // 'BM' WINDOWS_BITMAP_SIGNATURE
    		hdr.bfSize = bmpHeader.biSize + 352*240*3+ 14 /*sizeof(BITMAPFILEHEADER)*/;
    		hdr.bfReserved1 = hdr.bfReserved2 = 0;
    		hdr.bfOffBits = 14 /*sizeof(BITMAPFILEHEADER)*/ + bmpHeader.biSize ;
    
    
    		Filename.Empty();
    		Filename.Append(DestPATH);
    		Filename.Append("\\");
    		Filename.Append(ImageName);
    
    		ImagePtr.Open(Filename,CFile::modeCreate|CFile::modeWrite);
    		ImagePtr.Write(&hdr,min(14,sizeof(BITMAPFILEHEADER)));
    		ImagePtr.SeekToEnd();
    		ImagePtr.Write(&bmpHeader,sizeof(BITMAPINFOHEADER));
    		ImagePtr.SeekToEnd();
    		ImagePtr.Write(bmpData,240 *352 * 3);
    		ImagePtr.Close();
    	}
    	ImageFilePtr.Close();
    
    	Filename.Empty();
    	Filename.Append(DestPATH);
    	Filename.Append("\\");
    	Filename.Append(ImageFileName);
    	//Delete the Txt File.....
    	DeleteFile(Filename);
    	return TRUE; 
    }
    Can somebody help me out either fixing the dll /or by C# code.

    Thanks,
    Krishna
    Last edited by Plater; Feb 20 '08, 06:35 PM. Reason: added CODE tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    How is the data coming in over the socket? Is it just the raw pixel data?
    .NET's Bitmap class supports creation via a stream of bytes.

    Comment

    • krishp
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by Plater
      How is the data coming in over the socket? Is it just the raw pixel data?
      .NET's Bitmap class supports creation via a stream of bytes.
      Yes it is just raw pixel

      Comment

      • romcab
        New Member
        • Sep 2007
        • 108

        #4
        Hi,

        I did this before in C language and there is a Structure that you need to follow which contains the format/fields that you need to supply/fill in order for you to convert raw pixel data to BMP format. I believe the Bitmap class contains the same structure and methods to do it for you. You can refer to a C language code.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If the data is raw pixel data comming in in the format you described, you should be able to use the .FromStream() (not sure if that is the functions name, but that is the concept).
          I didn't get to close into your pixel processing, but some bmp formats include padding bytes, to make the stride a nice number. I didn't research it that closely once I discovered the Bitmap class had a lot of usefull "do it for me" functions.

          Comment

          Working...