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 :
=============== ========
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.
Can somebody help me out either fixing the dll /or by C# code.
Thanks,
Krishna
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;
}
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;
}
Thanks,
Krishna
Comment