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 : -
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);
-
======================= -
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. -
-
#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
4 2513
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.
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 < and > format if
it's > or < tag.
Since I have sub xml data in a parent xml node...
|
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...
|
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
------------...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |