SOLVED: wxImage.LoadFile

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Nefarious
Knows some wx things
Knows some wx things
Posts: 28
Joined: Fri Feb 24, 2017 8:23 pm

SOLVED: wxImage.LoadFile

Post by Nefarious »

I am trying to use wxImage and wxBitmap so have been following the image sample. I can draw to a wxBitmap, and now I am trying to draw a jpg to a wxBitmap then I can draw it on the screen. I have a stream of jpeg data that I read and I thought the process would be something like:

Code: Select all

// mXPixels is the number of pixels in x
// mYPixels is the number of pixels in y

// allocate the data for the image
mImageSize = mXPixels * mYPixels * 3;
byte* bpImage = (byte*)malloc(mImageSize);

// Get the image from the stream
ReadImage(bpImage);

wxBitmap mBitmap(mXPixels, mYPixels);
wxImage mImage = bitmap.ConvertToImage();
mImage.SetType(wxBITMAP_TYPE_JPEG);
// pass the jpeg data to the image
mImage.SetData(bpImage);

// get a wxBitmap of the bImage
wxBitmap foo = wxBitmap(mImage);

// Draw as
dc.DrawBitmap(foo, 0, 0);
It of course didn't work because I don't really know what I am doing. So I tried to read in the jpg file from the image sample and that won't work. I can't tell why, but simply loading the jpg file from the image sample won't work.

Code: Select all

if (wxFile::Exists(wxT("horse.jpg")))
{
	wxLogError(wxT("JPG image Exists"));
}
else
{
	wxLogError(wxT("JPG image Doesn't Exist"));
}
reports that the image exists

but

Code: Select all

if (!hImage.LoadFile(wxT("horse.jpg"), wxBITMAP_TYPE_JPEG,))
{
	wxLogError(wxT("Can't load JPG image"));
}
always reports that it can't load the jpg file.

I put the file in the same directory as the executable, I put it in the same directory as the source file. I tried "./horse.jpg", I tried ".\\horse.jpg" since it is windows :(. I can't load the jpg file and I don't know why it won't load.

I searched for how to find errors, are log files created? Something has to be there, this code has been around a long time.

Sorry to be a bother.
Last edited by Nefarious on Sun Feb 26, 2017 4:50 am, edited 1 time in total.
Nefarious
Knows some wx things
Knows some wx things
Posts: 28
Joined: Fri Feb 24, 2017 8:23 pm

Re: wxImage.LoadFile

Post by Nefarious »

I never read the App::OnInit, never thought about it until I was pulling out my hair. Didn't see the call

wxInitAllImageHandlers();
buttonsrtoys
Experienced Solver
Experienced Solver
Posts: 58
Joined: Mon Jul 03, 2017 12:03 am

Re: SOLVED: wxImage.LoadFile

Post by buttonsrtoys »

+1. Solved my problem, too. Thanks for posting sol'n
Post Reply