且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C ++中加载图像

更新时间:2023-01-31 22:36:04

您无法在控制台应用程序中加载图像,需要创建Windows应用程序并使用各种位图功能显示图像.您可以先在项目资源中添加一个简单的位图,然后使用 ^ ].
You cannot load images in console apps, you need to create a Windows app and use the various Bitmap functions to display images. You could start by adding a simple bitmap to the resources of your project and use LoadImage()[^].


我通常从控制台应用程序加载图像.执行诸如去偏斜和色彩还原之类的操作.

在控制台中显示1位图像也是一项琐碎的任务.

不久前,我放弃了LoadImage-发现增加的GDI +支持的格式值得过渡.


我时不时地忘记初始化GDI +,并且应用程序在启动时崩溃.否则,以下功能可以满足我的需求.


I commonly load images from console apps. Performing operations such as deskewing and colour reduction.

It''s also a trivial task to display 1 bit images in the console..

I gave-up on LoadImage a looong time ago - finding that the increased number of formats supported by GDI+ was worth the transition.


Every now and again I forget to initialize GDI+, and the app crashes upon startup. Otherwise, the following function performs adequetly for my needs.


// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP mLoadImg(WCHAR *szFilename)
{
   HBITMAP result=NULL;

   Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
   bitmap->GetHBITMAP(NULL, &result);
   delete bitmap;
   return result;
}



使用此功能之前,必须调用以下初始化代码:



Before you use this function you must call the following init-code:

static Gdiplus::GdiplusStartupInput gdiplusStartupInput;
static ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);



之后,您必须关闭GDI +



Afterwards, you have to shut-down GDI+

Gdiplus::GdiplusShutdown(gdiplusToken);




您需要包含gdiplus.h头文件并链接gdiplus库




You''ll need to include the gdiplus.h header file and link the gdiplus library