wxImage并绘制原始的rgb字节

编程入门 行业动态 更新时间:2024-10-26 20:25:49
本文介绍了wxImage并绘制原始的rgb字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将图像查看器编程为学校的任务,并且不能使用任何库来读取或处理图像。首先,我从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拥有该缓冲区的所有权。参见 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字节

本文发布于:2023-10-24 09:23:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   原始   wxImage   rgb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!