473,766 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert Ram data to BMP format

2 New Member
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 2546
Plater
7,872 Recognized Expert Expert
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 New Member
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 New Member
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 Recognized Expert Expert
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
9768
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 numeric value according to an arbitrary regular expression.
3
2873
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 but I would like to know how to do it programatically. I don't have any idea if this can be done so if it can please could someone show the code to do it or provide links so I can read up on how to do it.
2
6851
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 as a value. Check out the following problem. I want to convert the value in <WpDatesXml> node to have a valid "<" and ">" instead of &lt or &gt format so that I can use this xml for another use. Please help! <NewDataSet> <Table1>
5
2576
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 computer. The application ran without problems on our customer's computer for 2-3 years. Then our customer bought a new computer and we had to reinstall the application. Everything was ok for approximately 6 months until our customer was "cleaning up"...
1
1896
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 ------------ --------------------------------------------------------------------------------- 100 <suresh>sfjfjfjfjf</suresh>.................................... 101
7
28039
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 bit rgb data. Thanks!!
3
1257
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. public class Page : Base_Page {
6
3015
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 "convert" in expression'
5
20840
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 import strptime f1.write("strptime(%r); locale: %r" % (olddate, locale.getlocale())) newdate = time.strptime(olddate, "%H:%M:%S %b %d, %Y %Z") mysqldate = time.strftime("%Y-%m-%d %H:%M:%S", newdate)
5
3363
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 project.......... plz help me.. thanks & regards Prabhat
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.