1 Star 0 Fork 0

刘敬一如 / C++_exersize

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
1.图片的读取和导入 8.96 KB
一键复制 编辑 原始数据 按行查看 历史
---------------------------------图片绘制.CPP
// 图片绘制.cpp : 定义应用程序的入口点。
//
#pragma once
#include "framework.h"
#include "图片绘制.h"
#include "Canvas.h"
#include"Image.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
HWND hWnd;
HDC hDC;
HDC hMem;
int wWidth = 900;
int wHeight = 700;
GT::Canvas * _canvas = NULL;
GT::Image* _image = NULL;
void Render();
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
//+++++++++++++创建绘图所需的位图
void* buffer = 0;
BITMAPINFO bmpInfo;
hDC = GetDC(hWnd);
hMem = CreateCompatibleDC(hDC);
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = wWidth;
bmpInfo.bmiHeader.biHeight = wHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfo, DIB_RGB_COLORS, (void**)&buffer, 0, 0);
SelectObject(hMem, hBmp);
memset(buffer, 0, wWidth * wHeight * 4);
MSG msg;
//+++++++++++++创建绘图所需的位图
_canvas = new GT::Canvas( wWidth, wHeight, buffer);
_image = GT::Image::readFromFile("res/x.png");//此处为读入图片的了路径
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Render();
}
return (int) msg.wParam;
}
void Render()
{
_canvas->drawImage(0, 0, _image);
BitBlt(hDC, 0, 0, wWidth, wHeight, hMem, 0, 0, SRCCOPY);
}
//
// 函数: MyRegisterClass()
//
// 目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目标: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindowW(szWindowClass, szTitle, WS_POPUP,
CW_USEDEFAULT, 0, wWidth, wHeight, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目标: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Canvas.h
#pragma once
#include"GTMATH.hpp"
#include"Image.h"
namespace GT {
class Canvas
{
public:
int m_width;
int m_height;
RGBA* m_buffer;
Canvas(int _width, int _height, void* _buffer) {
if (_height <= 0 || _width <= 0)
{
m_height = -1;
m_width = -1;
m_buffer = nullptr;
}
m_width = _width;
m_height = _height;
m_buffer = (RGBA*) _buffer;
}
~Canvas() {}
void drawPoint(int x, int y, RGBA _color)
{
if (x<0 || y<0 || x>=m_width || y>=m_height )
{
return;
}
m_buffer[y * m_width + x]= _color;
}
void drawImage(int _x, int _y, Image* _image);
};
}
-------------------------------------------------Canvas.cpp
#include "Canvas.h"
namespace GT {
void Canvas::drawImage(int _x, int _y, Image* _image)
{
for (int u = 0; u < _image->GetWidth(); u ++) {
for (int v = 0; v < _image->GetHeight(); v++) {
RGBA _color = _image->GetColor(u, v);
drawPoint(_x + u, _y + v,_color);
}
}
}
}
------------------------------Image.h
#pragma once
#include"GTMATH.hpp"
#include"Image.h"
namespace GT {
class Canvas
{
public:
int m_width;
int m_height;
RGBA* m_buffer;
Canvas(int _width, int _height, void* _buffer) {
if (_height <= 0 || _width <= 0)
{
m_height = -1;
m_width = -1;
m_buffer = nullptr;
}
m_width = _width;
m_height = _height;
m_buffer = (RGBA*) _buffer;
}
~Canvas() {}
void drawPoint(int x, int y, RGBA _color)
{
if (x<0 || y<0 || x>=m_width || y>=m_height )
{
return;
}
m_buffer[y * m_width + x]= _color;
}
void drawImage(int _x, int _y, Image* _image);
};
}
-------------------------------------Image.cpp
#pragma once
#include"Image.h"
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
namespace GT {
Image* Image::readFromFile(const char* _fileName)
{
int _picType=0 ;
int _width= 0;
int _height=0 ;
stbi_set_flip_vertically_on_load(true);
byte* bits = stbi_load(_fileName, &_width, &_height, &_picType, STBI_rgb_alpha);
//调整颜色
for (int i = 0; i < _width * _height * 4; i += 4)
{
byte tmp = bits[i];
bits[i] = bits[i + 2];
bits[i + 2] = tmp;
}
//构造一个Image
Image* _image = new Image(_width, _height, bits);
stbi_image_free(bits);
return _image;
}
}
----------------GTMATH.hpp
#pragma once
namespace GT {
typedef unsigned char byte;
class RGBA {
public:
byte m_r;
byte m_g;
byte m_b;
byte m_a;
RGBA(byte _r = 255,
byte _g = 255,
byte _b = 255,
byte _a = 255)
{
m_b = _b;
m_g = _g;
m_r = _r;
m_a = _a;
}
~RGBA() {}
};
}
------stb.image.h:库下载地址:stb_image.h:https://github.com/nothings/stb/blob/master/stb_image.h
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liu-jingyiru/c--exersize.git
git@gitee.com:liu-jingyiru/c--exersize.git
liu-jingyiru
c--exersize
C++_exersize
master

搜索帮助