Win32显示jpg图像

44 篇文章 3 订阅
3 篇文章 1 订阅
//第一步 加入stbi的两个头文件

#define STB_IMAGE_IMPLEMENTATION
#include "stbi/stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stbi/stb_image_write.h"
//第二步 读取jpg图片和绘制到hdc设备上

void show_jpg(HDC& hdc)
{
	//读取图像
	int width, height, channel;
	unsigned char* image_data = stbi_load("h:\\test.jpg", &width, &height, &channel, 3);
	unsigned char* image_buffer = new unsigned char[width * height *channel];

	//颜色通道数的翻转和图像像素的翻转
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			image_buffer[((height - 1 - i) * width + j) *channel + 0] = image_data[(i* width + j) * channel + 2];
			image_buffer[((height - 1 - i) * width + j) *channel + 1] = image_data[(i* width + j) * channel + 1];
			image_buffer[((height - 1 - i) * width + j) *channel + 2] = image_data[(i* width + j) * channel + 0];
		}
	}

	//基本信息
	BITMAPINFOHEADER info{ 0 };
	info.biSize = sizeof(info);
	info.biPlanes = 1;
	info.biBitCount = channel * 8;
	info.biWidth = width;
	info.biHeight = height;
	info.biSizeImage = width * height * channel;

	//设置绘制模式位拉伸模式
	SetStretchBltMode(hdc, HALFTONE);

	//绘制
	StretchDIBits(hdc, 0, 0, width, height, 0, 0, width, height,
		image_buffer, (LPBITMAPINFO)&info, DIB_RGB_COLORS, SRCCOPY);

	//释放内存
	delete[] image_data;
	delete[] image_buffer;
}
//第三步 更新图像数据到窗口

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
			show_jpg(hdc);
            EndPaint(hWnd, &ps);
        }

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用C++中的GDI+库来实现在win32窗口中显示绝对路径的图片,具体实现代码可以参考以下示例: #include <Windows.h> #include <gdiplus.h> #pragma comment(lib, "gdiplus.lib") using namespace Gdiplus; LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); WNDCLASSEX wcex; ZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszClassName = L"GDIPlusWindowClass"; RegisterClassEx(&wcex); HWND hWnd = CreateWindow( L"GDIPlusWindowClass", L"Win32窗口中显示绝对路径图片", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL ); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } GdiplusShutdown(gdiplusToken); return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hWnd, &ps); Graphics graphics(hdc); Image image(L"C:/image.jpg"); graphics.DrawImage(&image, 0, 0); EndPaint(hWnd, &ps); break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值