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 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.
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.
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.
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 numeric value according to an arbitrary regular
expression.
|
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.
|
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 as a value.
Check out the following problem. I want to convert the value in <WpDatesXml>
node to have a valid "<" and ">" instead of < or > format so that I can
use this xml for another use. Please help!
<NewDataSet>
<Table1>
|
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"...
|
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
| |
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!!
|
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
{
|
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'
|
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)
|
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
|
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...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |