Display several images using CImg class on C++

情到浓时终转凉″ 提交于 2019-12-12 04:00:00

问题


How can one display several images of cards (52 cards) into a 'grid'? I am trying to create four piles in the upper left corner, four piles in the upper right corner, eight piles that make up the main table for my game with the use of CImg class.


回答1:


Updated Answer

You can use append() like this:

#include "CImg.h"
using namespace cimg_library;
int main() {
   // Load up all images into CImg structures
   CImg<unsigned char> c7("7.png");
   CImg<unsigned char> c9("9.png");
   CImg<unsigned char> c4("4.png");
   CImg<unsigned char> cjack("jack.png");

   // Declare output and intermediate variables
   CImg<unsigned char> row0,row1,grid;

   // Append horizontally into a row, you could append many - you are not restricted to 2
   row0 = c7.append(cjack,'x');

   // Append horizontally into a row
   row1 = c4.append(c9,'x');

   // Append vertically into a column
   grid = row0.append(row1,'y');

   grid.display();
}


Original Answer

The easiest way is probably to append images like this:

#include "CImg.h"
using namespace cimg_library;
int main() {
   CImg<unsigned char> c7("7.png");
   CImg<unsigned char> c9("9.png");
   CImg<unsigned char> cjack("jack.png");
   CImg<unsigned char> row;
   row = c7.append(cjack,'y').append(c9,'y');
   row.display();
}

which gives this:

If you change the 'y' axis parameter for append() to 'x' they will append side-by-side:

   CImg<unsigned char> col;
   col = c7.append(cjack,'x').append(c9,'x');
   col.display();

So, you should be able to see how to make a grid now.




回答2:


CImg itself has a very basic ability to display images in windows. All you can do is display multiple images, aligning them along one of the axes using the CImgDisplay struct. See the documentation for CImgDisplay::display() method. You can play with the 4 available axes and see if that fits your needs.

If that's not enough for you, you have to use some external library for display.



来源:https://stackoverflow.com/questions/44049080/display-several-images-using-cimg-class-on-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!