說明
顯示一張圖片,非常容易
例子
wxImage image(wxT("C:/timg.bmp"), wxBITMAP_TYPE_BMP);
wxBitmap bitmap(image);
wxBufferedPaintDC dc(this);
dc.DrawBitmap(bitmap, 0, 0, true);
問題
嘗試調用Create函數指定圖片的原始數據,當前使用bmp位圖爲例子,bmp的偏移54個字節之後是調色板,但是當前使用24色
位圖,所以沒有調色板,後面直接是圖片數據,生辰位圖出錯,記錄如下:
FILE * fileFd;
long fileLen;
fileFd = fopen ("C:/timg.bmp","rb");
if (fileFd==NULL)
{
return ;
}
else
{
fseek (fileFd, 0, SEEK_END);
fileLen=ftell (fileFd);
}
char* pRawData = (char*)malloc(fileLen-54);
fseek (fileFd, 54, SEEK_SET);
fputs(pRawData, fileFd);
fclose(fileFd);
wxBitmap bitmap;
bitmap.Create(pRawData, wxBITMAP_TYPE_BMP, 1024, 626);
wxBufferedPaintDC dc(this);
dc.DrawBitmap(bitmap, 0, 0, true);
說明
使用AGG渲染,存在一塊渲染緩存區域,實際上就是一塊內存區域保存每一個像素的RGBA數據。在已有的章節中,可以使用Win32 API,但是從整一個過程可以看到,過程比較繁瑣,並且會導致已有的wxClientDC等不能使用DrawText函數的問題。如果wxBitmap能夠綁定一塊渲染緩存,或許能夠完美的解決該問題
測試結果
實際上,調用wxBitmap的Create函數返回值永遠是false,並且跟蹤源代碼bitmap.cpp發現,通過調用基類wxBitmapHandler的派生類Create函數來實現,但是派生類的的具體實現只有一句話:return false; 說明實現的並不完整,因此下面的測試代碼不能實現基本功能
測試代碼
char* pData = (char*)malloc(width*height*4);
memset(pData, 0xff, width*height*4);
wxBitmap bitmap;
bool ret = bitmap.Create(pData, wxBITMAP_TYPE_BMP_RESOURCE, width, height, 1);//wxBITMAP_TYPE_BMP
m_rbuf.attach((unsigned char*)pData, width, height, -width*4); // Use negative stride in order
m_pixf.attach(m_rbuf);
m_renb.attach(m_pixf);
m_ren.attach(m_renb);
m_renb.clear(agg::rgba8(0, 0, 0, 255));
m_ras.move_to_d(100, 34.15);
m_ras.line_to_d(398.23, 123.43);
m_ras.line_to_d(165.45, 401.87);
// Setting the attrribute (color) & Rendering
m_ren.color(agg::rgba8(80, 90, 60));
agg::render_scanlines(m_ras, m_sl, m_ren);
wxMemoryDC memDC;
memDC.SelectObject(bitmap);
memDC.SetPen(*wxRED_PEN);
memDC.DrawRectangle((wxRect(10, 10, 100, 100)));
wxBufferedPaintDC destDC(this);
destDC.SetBackground(*wxWHITE_BRUSH);
destDC.Clear();
destDC.Blit(0, 0,
bitmap.GetWidth(), bitmap.GetHeight(),
&memDC,
0, 0,
wxCOPY,
true);
memDC.SelectObject(wxNullBitmap);
return;