In C, there are four ways to display images: 1. SDL (cross-platform); 2. Qt (cross-platform framework); 3. OpenCV (image processing and computer vision library); 4. Win32 API (Windows systems). The method chosen depends on the specific situation and application requirements.
How to display images in C
In C, there are several ways to display images:
1. SDL (Simple Direct Media Layer)
SDL is a cross-platform library that supports displaying images on different platforms:
#include int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_EVERYTHING); SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE); SDL_Surface* image = SDL_LoadBMP("image.bmp"); SDL_BlitSurface(image, NULL, screen, NULL); SDL_UpdateWindowSurface(screen); SDL_Delay(10000); // 等待 10 秒 SDL_Quit(); }
2. Qt
Qt is another cross-platform framework that can display images through the QWidget class:
#include #include #include int main(int argc, char* argv[]) { QApplication app(argc, argv); QLabel label; label.setPixmap(QPixmap("image.png")); label.show(); return app.exec(); }
3. OpenCV (Open Computer Vision Library)
OpenCV focuses on image processing and computer vision, providing functions for displaying images:
#include int main(int argc, char* argv[]) { cv::Mat image = cv::imread("image.jpg"); cv::imshow("Image", image); cv::waitKey(0); // 等待用户输入 return 0; }
4. Win32 API
In Windows systems, you can Using the Win32 API to display images:
#include int main(int argc, char* argv[]) { BITMAP bitmap; BITMAPINFO bitmapInfo; ZeroMemory(&bitmapInfo, sizeof(bitmapInfo)); bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo); bitmapInfo.bmiHeader.biWidth = 640; bitmapInfo.bmiHeader.biHeight = 480; bitmapInfo.bmiHeader.biPlanes = 1; bitmapInfo.bmiHeader.biBitCount = 32; void* bits; HDC hdc = GetDC(NULL); HBITMAP hbitmap = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0); HDC hdcMem = CreateCompatibleDC(hdc); HGDIOBJ oldObj = SelectObject(hdcMem, hbitmap); HBITMAP hbitmapImage = (HBITMAP)LoadImage(NULL, "image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); HDC hdcImage = CreateCompatibleDC(hdc); HGDIOBJ oldObjImage = SelectObject(hdcImage, hbitmapImage); BitBlt(hdcMem, 0, 0, 640, 480, hdcImage, 0, 0, SRCCOPY); SelectObject(hdcMem, oldObj); SelectObject(hdcImage, oldObjImage); DeleteObject(hbitmapImage); DeleteObject(hdcImage); DeleteDC(hdcMem); ReleaseDC(NULL, hdc); DeleteObject(hbitmap); return 0; }
Which method you choose depends on the situation and application requirements.
The above is the detailed content of How to display pictures in c++. For more information, please follow other related articles on the PHP Chinese website!