问题描述
我正在将图像查看器编程为学校的任务,并且不能使用任何库来读取或处理图像。首先,我从bmp格式开始。我已经创建了用于处理此类文件的类。作为GUI框架,我正在使用wxWidgets。
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.
所以我有普通的rgb字节数组,为wxImage构造函数准备了
So I have plain rgb bytes array, prepared for wxImage constructor
wxImage(int width, int height, unsigned char* data, bool static_data = false).
问题是,当我将其转换为wxBitmap并绘制为dc时,它忽略了rgb值,仅绘制黑色图片。我真的不知道可能是什么问题。这是我绘制图像的代码:
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);
}
这是绘画事件:
void OnPaint(wxPaintEvent& event)
{
wxAutoBufferedPaintDC dc(canvas);
dc.SetBackground( wxBrush(canvas->GetBackgroundColour()));
dc.Clear();
DrawImage(dc);
}
rgbArray填充了正确的值,我检查了多次。
rgbArray is filled with right values, I checked it multiple times.
感谢您的帮助:)
推荐答案
那是因为您可能在称呼它
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);
为了使代码异常安全,必须对其稍做重写。我不知道bmpFile是返回新缓冲区还是指向其自身数据缓冲区的指针。如果它不返回新的缓冲区,则必须创建自己的副本,因为wxImage拥有该缓冲区的所有权。参见
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);
}
或
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);
}
这篇关于wxImage并绘制原始的rgb字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!