|
I'm not sure how I should go about approaching this problem, but is there any way to create a bitmap by loading a "file in memory"?
Let me explain. So, I initially have an encrypted .bmp on disk, that I load into a std::ifstream, decrypt, and dump back out to a char*. The char array essentially contains the entire file, character by character, meaning that if I were to dump it to an std::ofstream, I would get the decrypted version of the file I loaded into the std::ifstream (which would also happen to be a bitmap that I can view with any picture viewer). The problem is, I want to generate a bitmap from this "file in memory" stored as a char* and all the functions I know of either require a file name, or require loading it from an executable resource. The end result should be a Win32 BITMAP object. Is there any way of doing what I'd like to do?
Thanks.
|
|
|
|
|
|
This might work. I'm not inherently familiar with some of the parameters that have to be passed in, but I think I'll manage (and post back if I have issues).
Thanks again!
|
|
|
|
|
Cyrilix wrote: I'm not inherently familiar with some of the parameters that have to be passed in
Here's a clue...
The first structure is a BITMAPFILEHEADER[^].
You'll find an offset to the pixel data there.
Following that is a BITMAPINFO[^] structure,
within which you'll find a BITMAPINFOHEADER[^]
structure.
Bitmap Storage[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I've been looking around for code examples and trying to figure out the concepts in my head, and somehow, I think I'm using the wrong function (CreateDIBitmap) for what I want to do, since I have to pass in a device context. From what I've read, the device context is like a drawing surface, but I have no need for a drawing surface, since I don't actually plan on drawing the bitmap to a window. All I want to do is get a BITMAP which can then have its pixels altered (which in my mind, would mean setting bits contained in my BITMAP.bmBits structure to different values). What do you guys think? Am I just really confused or onto something?
To clarify, my plan was the following:
-Load encrypted file and store it as an encrypted memory stream
-Decrypt memory stream and convert to BITMAP type
-Do image processing on BITMAP type
-Convert BITMAP back to a decrypted memory stream and encrypt
-Save encrypted memory stream to file
|
|
|
|
|
If you're going to manipulate the pixel data directly, then
you can use a DIBSection ("a DIB that applications can
write to directly").
CreateDIBSection()[^]
The passed DC can be NULL for DIB_RGB_COLORS bitmaps.
If you don't use a file mapping to create the DIBSection, you'll need
to copy the pixel bits from your array to the allocated pixel bits pointer.
The number of bytes to copy can be determined something like the following
(psudocode):
LONG NumberOfPixelBytes;
if (0 != BITMAPINFOHEADER.biSizeImage)
NumberOfPixelBytes = BITMAPINFOHEADER.biSizeImage;
else
NumberOfPixelBytes = (((BITMAPINFOHEADER.biWidth * (long)BITMAPINFOHEADER.biBitCount + 31L) & (~31L)) / 8L) * BITMAPINFOHEADER.biHeight;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi Mark,
I've got a good idea of how to do this when it comes to initially loading the Bitmap. The only other issue I have is saving the Bitmap back. My impression is that from an HBITMAP that is returned from CreateDIBSection(), we can get a BITMAP by calling GetObject() in such a manner:
HBITMAP hBmp = CreateDIBSection(...);
BITMAP bmp;
GetObject(hBmp, sizeof(bmp), &bmp);
But, the BITMAP object itself does not seem to have enough information to recreate the BITMAP file. For instance, from the MSDN documentation, it states that BITMAP has the following members:
LONG bmType;
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;
WORD bmPlanes;
WORD bmBitsPixel;
LPVOID bmBits;
The BITMAP file specification, on the other hand, has (as your link described):
BITMAPFILEHEADER:
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
BITMAPINFO:
RGBQUAD bmiColors[1];
BITMAPINFOHEADER (inside BITMAPINFO):
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
RGBQUAD array: data that has to do with palettes
Color-index array: the raw image data itself
If you compare the two lists, it would seem like BITMAP contains enough information to recreate most of the variables in BITMAPINFOHEADER as well as the info needed for the color-index array, but what about the rest of the info? It would seem like if I'm trying to convert it back to a char array, I would have to do some fancy extrapolation myself, in addition to saving some of the info that I had when I loaded the image. I will have another careful look at all of the BITMAP related functions to see if I've missed something, but at the moment, I'm sort of stuck at this point.
Also, if I haven't mentioned it to you yet, your advice so far been much appreciated.
|
|
|
|
|
Cyrilix wrote: from an HBITMAP that is returned from CreateDIBSection(), we can get a BITMAP by calling GetObject() in such a manner:
No need to make it that complicated - just keep the original headers!
All the info you need is there. You're only changing the pixels, right?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, in a sense, that's true, but I'd also like to handle the case where I've gotten a BITMAP from somewhere else (i.e., just been passed a BITMAP from another function), and I'm asked to save it. Is that not possible to implement easily?
|
|
|
|
|
Cyrilix wrote: Is that not possible to implement easily?
BMP bitmaps are the easiest format to save. The Bitmap Storage[^]
shows the layout of the file.
Another option:
If you're not into writing the code yourself, and you can use an ATL
class, the CImage class[^] is a nice lightweight
wrapper around a DIBSecion bitmap and will load and save in other
formats beyond BMP. The member functions make it easy to do all
the things you've discussed so far.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
get this error for 133 times:
CxImage.lib(ximajpg.obj) : error LNK2019:unresolved external symbol "_jpeg_finish_decompress" in Function ""public: bool __thiscall CxImageJPG::Decode(class CxFile *)
"(?Decode@CxImageJPG@@QAE_NPAVCxFile@@@Z)".
followed by fatal error LNK1120
Please help, i have tried to search in google but no way .
|
|
|
|
|
Looks like you missed to link some required library. Have you read about all CxImage requirements?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Did you import a lib file to your project?
|
|
|
|
|
I do have a wicked anomaly regarding an "out of memory" situation which behaves differently in two of my applications.
Application 1, let it be called "tool", is an inhouse tool, MFC based with full MFC GUI and stuff.
Application 2, let it be called "client", is a multiplayer game client, using MFC but only for the network socket, the whole GUI being custom on top of OpenGL/DirectX.
Both applications are made with Visual C++ 2003.NET / v7.1.6030 unter WindowsXP. Only the "Release" mode is currently used.
For exception handling of win32 exceptions both have the following project properties set...
Project Properties -> C/C++ -> Code Generation -> Enable C++ Exceptions = No
Project Properties -> C/C++ -> Command Line = /EHa
...plus...
_set_se_translator(myfunction)
...is used (based on the information I found here[^]).
But the wicked thing comes with memory allocation and the throwing vs. not-throwing of a std::bad_alloc exception. If I put the following code into both applications...
Byte* pData = NULL;
try {
int Amount = 0;
--Amount;
pData = new Byte[Amount];
}
catch (std::bad_alloc& E) {
}
...they behave differently:
The "tool" behaves as I would expect and a std::bad_alloc exception is thrown, catched and my own error handling does the rest.
But in the "client" a Windows message box comes up, simply telling me "Out of memory." with a black exclamation mark in a yellow triangle plus an OK button and if I press the OK button the application continues to run - which actually gives me a big "WTF?" moment. One way or the other that behaviour is neither intended nor appreciated.
My best guess is that it depends on the compiler settings but I wasn't able yet to find any meaningful differences. I already experimented with optimization levels but couldn't change the application's behaviour doing so. Could anybody give me a hint on what the problem might be?
Thanks,
T.T.H.
|
|
|
|
|
Answering myself:
Both applications seem to use different implementations of the new operator - despite both using the MFC:
Application 1 uses the one from the CRT and throws a std::bad_alloc exception.
Application 2 uses the one from the MFC and throws a CMemoryException*.
According to "Binglong's Blog" (link see below) the actual choice which new operator will be used depends on (quote) "the operator new comes from the library the linker sees first". Ugh. What a mess. Unfortunately I was not able to define myself what "the linker sees first". If somebody has a handy hint, go ahead, will be very apprectiated!
In addition to that there seems to appear some "Microsoftic Magical Trick": in case a CException is thrown ""somebody"" checks whether it will be catched - if yes, it the exception will be thrown, if not, the application will not simply crash but a message box will be displayed and the application can run on. Please note that this is an assumption based on my observation and to be honest I don't consider it helpful to wastespend more time on examining that behaviour further.
Helpful links:
Binglong's space: Operator new, STL memory allocator, and MSVC[^]
Visual C++ 6.0: Don't Let Memory Allocation Failures Crash Your Legacy STL Application[^]
Visual C++ exception handling[^]
|
|
|
|
|
I know! I've posted this once before, but I didn't get my answer. Sorry! I'm quite new and I know this may seems funny; But the problem is:
I wanna send a file to a given eMail address "without displaying any dialog box from STRANGER applications like outlook express or anything".
Frankly speaking, I don't wanna fall into reading boring APIs So could you please direct me to a
free source or an utility or somethig ...
Thank you masters!
|
|
|
|
|
I'm not a great expert, but maybe this article can help:
http://www.codeproject.com/KB/IP/zsmtp.aspx[^]
You'd need to get the basic information (server name) at some point though. But I don't think I'd like an application that was completely stealthily sending emails from me. Who know what they could be saying? I'm sure you are an angel who spends all winter bringing the homeless blankets and dinner [*], but not all people are...
Iain.
[*] Beer smiley to anyone who gets the song quote (without google)
|
|
|
|
|
I feel kind of misunderstanding into your words. Any way I thank you for your attention.
For You ----->
Thank you masters!
|
|
|
|
|
Okay:
Jusef Marzbany wrote: I've posted this once before, but I didn't get my answer. Sorry!
Impatient/ungrateful (perhaps you should be more clear?)
Jusef Marzbany wrote: "without displaying any dialog box from STRANGER applications like outlook express or anything"
Bolding to try and get people's attention (is likely to do the opposite )
Jusef Marzbany wrote: Frankly speaking, I don't wanna fall into reading boring APIs
Lazy.
Hmm.. Looks like you should read the forum posting guidelines and do some work yourself
Regards,
--Perspx
"I've got my kids brainwashed: You don't use Google, and you don't use an iPod." - Steve Ballmer
"Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen an angry penguin charging at them in excess of 100mph." - Linus Torvalds
|
|
|
|
|
Answer me if you can, if not your're not forced to say nonsense
Thank you masters!
|
|
|
|
|
I don't think it's nonsense - it may help you to get answers to your questions in the future more easily; you're not forced to listen to my "nonsense" either; I just thought it may be of use.
Regards,
--Perspx
"I've got my kids brainwashed: You don't use Google, and you don't use an iPod." - Steve Ballmer
"Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen an angry penguin charging at them in excess of 100mph." - Linus Torvalds
|
|
|
|
|
I beg your pardon, I was angry with someone ...
Thank you masters!
|
|
|
|
|
Jusef Marzbany wrote: Answer me if you can
Actually his post is a valid answer.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I beg your pardon, I was angry with somebody ...
Thank you masters!
|
|
|
|
|
We have developed one IOCP server Application.
We got a socket close event when client application (safe/unsafe) close.
But when client machine restarts or network cable unplugged we can not get
any event of the socket close.
We have also tried to set socket timeout with setsockopt() ,but its not working with IOCP.
So ,Is there any method to get close event for this sockets? or any other method to set
timeout in IOCP.
Thank you.
|
|
|
|