如何从函数返回 GDI::位图
How to return a GDI::Bitmap from a function
如何从函数返回位图?此代码不起作用:编译器错误
Gdiplus::Bitmap create()
{
Gdiplus::Bitmap bitmap(10,10,PixelFormat32bppRGB);
// fill image
return bitmap;
}
我不想返回指针,因为它会产生内存泄漏的机会。(或者如果有办法避免这种内存泄漏的机会)
从函数返回对象时,编译器需要生成代码以复制或(在 C++11 中)移动该对象。
Gdiplus::Bitmap
没有可访问的复制构造函数(并且早于 C++11,因此也不允许移动),因此不允许这样做。根据您使用的方式,您可以考虑改用std::unique_ptr
或std::shared_ptr
之类的东西。
或者,您可能希望在父级中创建Bitmap
,只需传递指向它的指针或引用,即可让函数根据需要填充它。
如果你想要代码。 这就是将指针传回的方式。
Gdiplus::Bitmap* create()
{
Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(10,10,PixelFormat32bppRGB);
// fill image
return bitmap;
}
这行不通吗?
void create(Gdiplus::Bitmap& bitmap)
{
bitmap = *(new Gdiplus::Bitmap(10,10,PixelFormat32bppRGB));
}
上下文类似于
int main()
{
Gdiplus::Bitmap bitmap; //ONLY WORKS IF IT HAS DEFAULT CONSTRUCTOR
create(bitmap);
}
我不熟悉 Gdiplus,所以如果没有默认构造函数,那么这将不起作用。
相关文章:
- 使用 GDI+ 旋转位图,然后转换为 HDC
- C++gdi::内存中的位图到PNG图像
- 如何从依赖于设备的 HBITMAP 构造 GDI+ 位图对象
- 将 GDI Plus 位图转换为 QPixmap
- 使用GDI+删除位图和CLSID对象时,C++内存管理失败
- WinAPI/GDI:如何使用 GetDIBits() 为位图合成颜色表
- 将opencv图像转换为gdi位图不起作用取决于图像大小
- c++ GDI 位图不想加载
- GDI+ 位图问题
- 如何从函数返回 GDI::位图
- 如何将 opencv 垫图像转换为 gdi 位图
- GDI - 是否可以在WM_PAINT调用之间将位图保存在内存中
- 如何在 GDI 中翻译简单的位图
- GDI+:绘制位图时出现未处理的异常
- GDI,关于alpha位图
- 从gdiplus (GDI+)中的设备上下文或图形对象中获取图像/位图
- OpenCV / Tesseract:如何用GDI+位图替换libpng, libtiff等(通过GDI+加载到cv::
- 绘制到GDI+位图
- 图像处理:位图旋转(c++ /GDI+)
- 可以闪存检测正在拍摄的屏幕截图(或GDI位副本)