用CImg覆盖图像 [英] Overlaying images with CImg

查看:139
本文介绍了用CImg覆盖图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很容易,但是在CImg中使用draw_image()函数时遇到了很多问题.这是一段代码:

It seems like this would be very easy, but I am having a lot of problems using the draw_image() function in CImg. Here is a snippet of code:

CImg<unsigned char> landingScreen("desertSunset.bmp"), newGameBTN("newgame.pgm");
CImgDisplay main_disp(landingScreen,"Main Window");
landingScreen.draw_image(400,400,newGameBTN);

我已经尝试了draw_image()的大约8个重载函数,但都无济于事.当我运行该程序时,主窗口会弹出我的desertSunset图像,仅此而已.我正在尝试将newgame.pgm放在landingScreen的顶部,但是图像没有显示出来.这是深度方面的问题,还是我构造文件的方式?我查看了许多有关CImg的示例文件,但是它们并不是很有用,关于这些功能的文档充其量是最少的.如果有人可以举一个"Hello World"示例,说明如何在图像上绘制图像,或者告诉我我做错了什么,我将不胜感激.谢谢!

I have tried about 8 of the overloaded functions for draw_image(), all to no avail. When I run the program, Main Window pops up with my desertSunset image, but that's it. I am trying to lay newgame.pgm on top of landingScreen, but the image doesn't show up. Is this a problem with depth, or maybe the way I'm structuring the file? I've looked through a bunch of different example files for CImg but they aren't very helpful, and the documentation on these functions is minimal at best. If someone could give a 'Hello World' example of how to draw an image over an image, or tell me what I'm doing wrong, I would greatly appreciate it. Thanks!

推荐答案

我使用ImageMagick创建了两个这样的图像:

I created two images like this with ImageMagick:

convert -size 400x300 gradient:red-black PNG24:gradient.png

convert -size 100x100 xc:yellow overlay.png

然后运行以下CImg代码:

#define cimg_use_png
#define cimg_display 0
#include "CImg.h"
using namespace cimg_library;
int main() {
   CImg<unsigned char> gradient("gradient.png");
   CImg<unsigned char> overlay("overlay.png");
   gradient.draw_image(80,150,overlay);
   gradient.save_png("result.png");
}

得到了:

这篇关于用CImg覆盖图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆