Hi,
Is that posible to save/convert wxImage to wxMemoryBuffer ?
Regards
Teh
wxImage and wxMemoryBuffer Topic is solved
Hi!
That depends on what info you want to store in the buffer.
The most easy case I can think of would be to store the RGB data of the image. You could simply do something like:
That depends on what info you want to store in the buffer.
The most easy case I can think of would be to store the RGB data of the image. You could simply do something like:
Code: Select all
unsigned int byteCount = image.GetWidth()*image.Height()*3;
wxMemoryBuffer buffer( byteCount );
buffer.AppendData(image.GetData(),bytecount);
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Hi,
After convert to wxMemoryBuffer , How can i convert/restore (RGB data) back
to wxImage ?
Now, I am able to write wxMemoryBuffer back to wxImage, If i read from file by below code.
wxFile FileIn(filename);
int nDataLength = FileIn.Length();
wxMemoryBuffer BufferIn;
int nBytesRead = FileIn.Read(BufferIn.GetWriteBuf(nDataLength), nDataLength);
wxMemoryInputStream mis(BufferIn.GetData(), BufferIn.GetBufSize());
if (!GetImage().LoadFile(mis) ) {
wxLogError(wxT("Can't load BMP image from stream"));
}
But I try to load the wxMemoryBuffer by the code your provided. It return "Can't load BMP image from stream".
Regards
Teh
After convert to wxMemoryBuffer , How can i convert/restore (RGB data) back
to wxImage ?
Now, I am able to write wxMemoryBuffer back to wxImage, If i read from file by below code.
wxFile FileIn(filename);
int nDataLength = FileIn.Length();
wxMemoryBuffer BufferIn;
int nBytesRead = FileIn.Read(BufferIn.GetWriteBuf(nDataLength), nDataLength);
wxMemoryInputStream mis(BufferIn.GetData(), BufferIn.GetBufSize());
if (!GetImage().LoadFile(mis) ) {
wxLogError(wxT("Can't load BMP image from stream"));
}
But I try to load the wxMemoryBuffer by the code your provided. It return "Can't load BMP image from stream".
Regards
Teh
Hi!
Honestly, I don't get what you want or expect to happen.
Your initial question was how to store the wxImage in a wxMemoryBuffer. Although this only stores the RGB data, I gave you a solution. Now you want to know how you can load that data??
LoadFile() won't work on this. It is simple RGB data that is stored, not data in a specific image format the LoadFile() or the wxImage handlers could parse...
So, please, explain what you want to do and what seems to be the problem.
Honestly, I don't get what you want or expect to happen.
Your initial question was how to store the wxImage in a wxMemoryBuffer. Although this only stores the RGB data, I gave you a solution. Now you want to know how you can load that data??
LoadFile() won't work on this. It is simple RGB data that is stored, not data in a specific image format the LoadFile() or the wxImage handlers could parse...
So, please, explain what you want to do and what seems to be the problem.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
wxImage::SetData
You use wxImage::GetData to store, then you should use wxImage::SetData to recreate the image.
But see docs, use malloc for creation of buffer and dont delete it (its owned by wxImage after SetData).
But see docs, use malloc for creation of buffer and dont delete it (its owned by wxImage after SetData).
You must use constructor not LoadFile - wxImage(mis)bcteh wrote:Hi,
After convert to wxMemoryBuffer , How can i convert/restore (RGB data) back
to wxImage ?
Now, I am able to write wxMemoryBuffer back to wxImage, If i read from file by below code.
wxFile FileIn(filename);
int nDataLength = FileIn.Length();
wxMemoryBuffer BufferIn;
int nBytesRead = FileIn.Read(BufferIn.GetWriteBuf(nDataLength), nDataLength);
wxMemoryInputStream mis(BufferIn.GetData(), BufferIn.GetBufSize());
if (!GetImage().LoadFile(mis) ) {
wxLogError(wxT("Can't load BMP image from stream"));
}
But I try to load the wxMemoryBuffer by the code your provided. It return "Can't load BMP image from stream".
Regards
Teh
Re: wxImage and wxMemoryBuffer
for save image You must use wxImage::SaveFile(wxOutputStream& stream, int type) and wxMemoryOutputStream, from wxMemoryOutputStream You can read data and store into wxMemoryBufferbcteh wrote:Hi,
Is that posible to save/convert wxImage to wxMemoryBuffer ?
Regards
Teh
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
Hi,
If all you want to do is load an image, modify the pixel data, then save it, I'm not sure why you don't just do the following:
Jim
PS. Using the code tags makes your post much easier to read and follow.
If all you want to do is load an image, modify the pixel data, then save it, I'm not sure why you don't just do the following:
Code: Select all
wxImage image;
wxString aFileToLoad(_T("myinfile.bmp"));
wxString aFileToSave(_T("myoutfile.bmp"));
image.LoadFile(aFileToLoad);
unsigned char *pixelData = image.GetData();
// modify the pixel data as you please...
image.SaveFile(aFileToSave);
Jim
PS. Using the code tags makes your post much easier to read and follow.
OS: Vista SP1, wxWidgets 2.8.7.