473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to convert Ram data to BMP format

2
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 :
Expand|Select|Wrap|Line Numbers
  1. try
  2.                             {
  3.                                 string receivedPath = "c:\\NEW\\";
  4.                                 int receivedBytesLen=0;
  5.                                 int bufstart = 0;
  6.  
  7.                                 while (test > 0)
  8.                                 {
  9.  
  10.                                     receivedBytesLen = clientSock.Receive(Recievbuf);
  11.  
  12.                                     int fileNameLen = BitConverter.ToChar(Recievbuf, 0);
  13.                                     string fileName = "image1.text";//Encoding.ASCII.GetString(Recievbuf, 4, fileNameLen);
  14.                                     // MessageBox.Show("Client:{0} connected & File {1} started received." + clientSock.RemoteEndPoint + fileName);
  15.                                     bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append));
  16.                                     //bWrite.Write(Recievbuf, (int)(352 * 240 * 2 - test), receivedBytesLen);
  17.                                     bWrite.Write(Recievbuf, bufstart, receivedBytesLen);
  18.  
  19.                                     // MessageBox.Show("File: {0} received & saved at path: {1}" + fileName + receivedPath);
  20.  
  21.                                     bufstart += receivedBytesLen;
  22.                                     test -= receivedBytesLen;    
  23.  
  24.                                     bWrite.Close();
  25.                                 }
  26.                                 clientSock.Close();
  27.  
  28.  
  29.                                 stbImage = bMPCon(Recievbuf);
  30.                                 stbImage.Save("c:\\NEW\\myimage.bmp", ImageFormat.Bmp);
  31.  
=======================
Expand|Select|Wrap|Line Numbers
  1. public Bitmap bMPCon(byte[] data)
  2.          {
  3.             //Here create the Bitmap to the know height, width and format
  4.             Bitmap bmp = new Bitmap(352,240,PixelFormat.Format24bppRgb);
  5.  
  6.  
  7.  
  8.  
  9.             //Create a BitmapData and Lock all pixels to be written 
  10.             /*BitmapData bmpData = bmp.LockBits(
  11.                                  new Rectangle(0, 0, bmp.Width, bmp.Height),
  12.                                  ImageLockMode.WriteOnly, bmp.PixelFormat);*/
  13.  
  14.             BitmapData bmpData = bmp.LockBits(
  15.                                  new Rectangle(0, 0, 352, 240),
  16.                                  ImageLockMode.WriteOnly, bmp.PixelFormat);
  17.  
  18.  
  19.  
  20.             //Copy the data from the byte array into BitmapData.Scan0
  21.             Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
  22.            //Unlock the pixels
  23.             bmp.UnlockBits(bmpData);
  24.             //Return the bitmap 
  25.             return bmp;
  26.         }
  27.  
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #define GETALPHA(X) (X>>15)
  3. #define GETRED(X) ((X<<1+16)>>11+16)
  4. #define GETGREEN(X) ((X<<6+16)>>11+16)
  5. #define GETBLUE(X) ((X<<11+16)>>11+16)
  6.  
  7. Conver2BMP(CString DestPATH,CString ImageName,CString ImageFileName)
  8. {
  9.  
  10.     CString Filename;
  11.     CFile ImageFilePtr;
  12.     CFile ImagePtr;
  13.  
  14.     BITMAPFILEHEADER hdr;
  15.     BITMAPINFOHEADER bmpHeader; 
  16.     BITMAPCOREHEADER chdr;
  17.  
  18.     Filename.Empty();
  19.     Filename.Append(DestPATH);
  20.     Filename.Append("\\");
  21.     Filename.Append(ImageFileName);
  22.  
  23.     ImageFilePtr.Open(Filename,CFile::modeRead,0);
  24.     unsigned long length = ImageFilePtr.GetLength();
  25.  
  26.     ImageFilePtr.Read(bmpRaw,length);
  27.  
  28.     for(int i=0; i<240; i++)
  29.     {
  30.         for(int j=0; j<352; j++)
  31.         {
  32.             unsigned long col1555 = bmpRaw[((i*352)+j)];
  33.             unsigned char cA = GETALPHA(col1555);
  34.             unsigned char cR = GETRED(col1555);
  35.             unsigned char cG = GETGREEN(col1555);
  36.             unsigned char cB = GETBLUE(col1555);
  37.  
  38.             bmpData[(((((239-i)*352)+(j)))*3)+0] = cB*8;
  39.             bmpData[(((((239-i)*352)+(j)))*3)+1] = cG*8;
  40.             bmpData[(((((239-i)*352)+(j)))*3)+2] = cR*8;
  41.  
  42.         }
  43.  
  44.         chdr.bcBitCount = 24;
  45.         chdr.bcHeight = 240;
  46.         chdr.bcPlanes = 1;
  47.         chdr.bcSize = 240*352*3;
  48.         chdr.bcWidth = 352;
  49.  
  50.         memset(&bmpHeader,0,sizeof(BITMAPINFOHEADER));
  51.         memset(&hdr,0,sizeof(BITMAPFILEHEADER));
  52.  
  53.  
  54.         bmpHeader.biPlanes = 1; 
  55.         bmpHeader.biXPelsPerMeter = 0; 
  56.         bmpHeader.biYPelsPerMeter = 0; 
  57.         bmpHeader.biClrImportant = 0;
  58.         bmpHeader.biCompression = BI_RGB;
  59.         bmpHeader.biBitCount = 24; 
  60.         bmpHeader.biSize = sizeof(BITMAPINFOHEADER);
  61.         bmpHeader.biHeight = 240;
  62.         bmpHeader.biWidth = 352;
  63.         bmpHeader.biSizeImage = 0;//bmpHeader.biWidth * bmpHeader.biHeight * 3;
  64.  
  65.         hdr.bfType = 0x4d42; // 'BM' WINDOWS_BITMAP_SIGNATURE
  66.         hdr.bfSize = bmpHeader.biSize + 352*240*3+ 14 /*sizeof(BITMAPFILEHEADER)*/;
  67.         hdr.bfReserved1 = hdr.bfReserved2 = 0;
  68.         hdr.bfOffBits = 14 /*sizeof(BITMAPFILEHEADER)*/ + bmpHeader.biSize ;
  69.  
  70.  
  71.         Filename.Empty();
  72.         Filename.Append(DestPATH);
  73.         Filename.Append("\\");
  74.         Filename.Append(ImageName);
  75.  
  76.         ImagePtr.Open(Filename,CFile::modeCreate|CFile::modeWrite);
  77.         ImagePtr.Write(&hdr,min(14,sizeof(BITMAPFILEHEADER)));
  78.         ImagePtr.SeekToEnd();
  79.         ImagePtr.Write(&bmpHeader,sizeof(BITMAPINFOHEADER));
  80.         ImagePtr.SeekToEnd();
  81.         ImagePtr.Write(bmpData,240 *352 * 3);
  82.         ImagePtr.Close();
  83.     }
  84.     ImageFilePtr.Close();
  85.  
  86.     Filename.Empty();
  87.     Filename.Append(DestPATH);
  88.     Filename.Append("\\");
  89.     Filename.Append(ImageFileName);
  90.     //Delete the Txt File.....
  91.     DeleteFile(Filename);
  92.     return TRUE; 
  93. }
  94.  
Can somebody help me out either fixing the dll /or by C# code.

Thanks,
Krishna
Feb 20 '08 #1
4 2513
Plater
7,872 Expert 4TB
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.
Feb 20 '08 #2
krishp
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.
Yes it is just raw pixel
Feb 21 '08 #3
romcab
108 100+
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.
Feb 22 '08 #4
Plater
7,872 Expert 4TB
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.
Feb 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: Jon S via DotNetMonster.com | last post by:
Hi all, Is there a way to convert an Access.mdb from one format (say Access '97 or Access 2000) to Access 2002 format using ADO.NET and C#??? I know how to do it manually using the Access DBMS...
2
by: andrew007 | last post by:
I do xml / xslt transformation using asp.net but I found any value (w/xml format) in xml node html-encoded to &lt and &gt format if it's > or < tag. Since I have sub xml data in a parent xml node...
5
by: melickas | last post by:
We designed a custom application using Office Developer Tools '97 which included a Run-time version of Access--- so it would not matter if our customer even had any version of Access on their...
1
by: surya | last post by:
hi sir i have table hh .it has two columns one is hhno is integer datatype another hhdoc is xml data type like hh table hhno hhdoc ------------...
7
by: anuragkhanna8 | last post by:
Hi, I am trying to convert a 16 bit rgb value to 32 bit, however the new color generated is different from the 16 bit rgb data. Please let me know the formula to convert an 16 bit rgb data to 32...
3
by: JJ | last post by:
I've got an n-layer application that fetches data from an SQL database using stored procedures. This information is read into various classes which represent a hierachical structure:. eg. ...
6
by: tshad | last post by:
Apparently, I can't do: Dim da2 As New OleDb.OleDbDataAdapter("Select PR, Convert(varchar,getchar(),1),F1, F2, F5, Sum(F4) from temp .... I am getting this error. 'undefined function...
5
by: rouven | last post by:
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS. the code i tried was: from datetime import datetime from time...
5
by: sonu | last post by:
hey good morning ...... how to convert a video file in .flv format in php for linux hosting......is there any package whis provide this facility . Can i use ffmpeg for linux hosting...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.