问题
I am programming image viewer as school task and I cant use any libraries for reading or manipulating images. First I started with bmp format. I have created class for handle this type of file. As GUI framework I am using wxWidgets.
So I have plain rgb bytes array, prepared for wxImage constructor
wxImage(int width, int height, unsigned char* data, bool static_data = false).
Problem is that when I convert it to wxBitmap and draw to dc it's ignoring rgb values a draw only black picture. I really do not know what could be a problem. This is my code for draw image:
DrawImage(wxDC &dc)
{
BYTE *rgbArray = bmpFile->GetRGB();
wxSize imageSize = wxSize(bmpFile->GetSize().w,bmpFile->GetSize().h);
wxImage image = wxImage(imageSize, &rgbArray);
wxBitmap bitmap = wxBitmap(image);
dc.DrawBitmap(bitmap,5,5, false);
}
And this is on paint event:
void OnPaint(wxPaintEvent& event)
{
wxAutoBufferedPaintDC dc(canvas);
dc.SetBackground( wxBrush(canvas->GetBackgroundColour()));
dc.Clear();
DrawImage(dc);
}
rgbArray is filled with right values, I checked it multiple times.
Thanks for any help :)
回答1:
Thats because you are probably calling this function, because you are passing a BYTE**.
wxImage (const wxSize &sz, bool clear=true)
to call the other overload, removing the & might help
wxImage image = wxImage(imageSize, rgbArray);
To make the code exception safe, it must be rewritten slightly. I don't know whether bmpFile returns a new buffer or a pointer to its own data buffer. If it doesn't return a new buffer then you must make your own copy because wxImage takes ownership of the buffer. see wxImage
DrawImage(wxDC &dc)
{
wxSize imageSize = wxSize(bmpFile->GetSize().w,bmpFile->GetSize().h);
wxImage image = wxImage(imageSize, bmpFile->GetRGB() );
wxBitmap bitmap = wxBitmap(image);
dc.DrawBitmap(bitmap,5,5, false);
}
or
DrawImage(wxDC &dc)
{
std::unique_ptr<BYTE> rgbData( bmpFile->GetRGB() );
wxSize imageSize = wxSize(bmpFile->GetSize().w,bmpFile->GetSize().h);
wxImage image = wxImage(imageSize, rgbData.release()) );
wxBitmap bitmap = wxBitmap(image);
dc.DrawBitmap(bitmap,5,5, false);
}
来源:https://stackoverflow.com/questions/14910748/wximage-and-draw-raw-rgb-bytes