如何在控制台窗口中使用 C++ 显示图像? [英] How to display an image using C++ right in console window?

查看:170
本文介绍了如何在控制台窗口中使用 C++ 显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 C++(Windows 10)在控制台窗口中显示图像(bmp).

不是按字符显示,因为我已经知道了,而是像普通图像那样逐个像素地显示图像.

不是通过启动另一个应用程序在另一个窗口中显示图像,而是直接在黑色控制台窗口中显示.

我已经在互联网上搜索了它,但似乎没有找到不使用 opencv 之类的东西的方法.没有opencv,有没有办法自己做?

解决方案

您的假设是错误的:通常,C++(或 C)程序甚至可能无法从某个控制台窗口"启动;(例如,它可能以批处理模式"启动).特别是,某些 C++ 实现可能会在没有任何屏幕(因此没有任何控制台窗口)的计算机上运行(例如数据中心的 Linux 服务器,它们是 Internet 上的大多数服务器).大多数 top500 超级计算机 没有您可以访问的屏幕,但在其上运行 HPC 软件(例如 Tripoli-4) 通常用 C++ 编码.嵌入式计算机(例如您的 RaspberryPi)可以用标准 C++(例如 C++11 或 C++14)编程) 没有任何屏幕或控制台窗口(例如在 2020 年使用为 GCC 配置的最新编译器https://en.wikipedia.org/wiki/Cross_compiler" rel="nofollow noreferrer">交叉编译)

(即使在我不知道也从未使用过的 Windows 上,我确信有一些方法可以在任何控制台窗口"之前启动程序——通过配置计算机来启动一些守护进程或服务器; 存在)

因此,您应该更准确地定义您的平台.一般来说,答案可能是特定于平台和操作系统的.另请参阅 OSDEV 以获取开源操作系统示例:FreeBSD 是一个著名的,并且有一个 GCC 或 <一个 href="https://clang.llvm.org/" rel="nofollow noreferrer">Clang 编译器,能够编译您的 C++ 应用程序.

我强烈建议使用可移植的图形框架,例如 Qt(特别是,因为它使您的代码更具可移植性,如果你仔细编码).当然,您随后需要您的用户拥有它,或者您分发包含 Qt 的代码.还可以考虑使用 FLTKFOXSFML 工具包.

C++14 标准(以及 C++11 和早期版本)不了解图像或控制台窗口,因此(通常,不提及任何平台或操作系统)您的问题毫无意义.

还研究以 C++ 编码的现有开源程序或库的源代码以获取灵感(在 githubgitlab),例如 fish 外壳,KDEGCCClang 静态分析器(它们都应该对您有用)、RefPerSys 等......

I need to display an image(bmp) in console window using C++ (Windows 10).

Not display by characters, for that I already knew how, but show the image pixel by pixel the way ordinary images appears to be.

And not by launch another application to show the image in another window, but right in the black console window.

I've searched it all over the internet, but didn't seem to find a way to do it without using things like opencv. Is there a way to do it by myself, without opencv?

解决方案

Your hypothesis is wrong: in general a C++ (or C) program might not even be started from some "console window" (e.g. it might be started in "batch mode"). In particular, some C++ implementations may run on computers (such as Linux servers in data centers, which are the majority of the servers on Internet) without any screen (so without any console window). Most top500 supercomputers don't have screens you could access, but HPC software running on them (e.g. Tripoli-4) is often coded in C++. And embedded computers (e.g. your RaspberryPi) can be programmed in standard C++ (e.g. C++11 or C++14) without any screen or console window (e.g. in 2020 using a recent GCC compiler configured for cross-compilation)

(even on Windows, which I don't know and never used, I am sure that there is some way to start programs -by configuring the computer to start some daemons or servers- before any "console window" exist)

So you should define more precisely what your platform is. In general the answer could be platform and operating system specific. See also OSDEV for examples of open source operating systems: FreeBSD is a famous one, and has a GCC or Clang compiler capable of compiling your C++ application.

I would strongly recommend using a portable graphical framework like Qt (in particular, because it makes your code more portable, if you code carefully). Of course you then requires your user to have it, or your distribute your code with Qt included. Consider also using FLTK or FOX or SFML toolkits.

The C++14 standard (and C++11 and earlier versions) does not know about images or console windows, so (in general, without mentioning any platform or operating system) your question is meaningless.

Study also for inspiration the source code of existing open source programs or libraries coded in C++ (on github or gitlab), such as the fish shell, KDE, GCC, the Clang static analyzer (and they both should be useful to you), RefPerSys, etc etc....

这篇关于如何在控制台窗口中使用 C++ 显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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