完整的使用GDI分割图片的win32程序
2009-02-20 13:47
417 查看
最近研究了一下gdi,想做个拼图游戏。以下就是用来图片分割的代码:
// sliptImage.cpp : 定义应用程序的入口点。
//
#include <windows.h>
#include <gdiplus.h>
#include <string>
#pragma comment(lib,"gdiplus")
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
using namespace std;
using namespace Gdiplus;
int gdiTest(HDC hdc, HWND hWnd);
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
GdiplusStartupInput input;
ULONG_PTR gdiPlusToken;
if(GdiplusStartup(&gdiPlusToken,&input,NULL)!=Ok)
{
return -1;
}
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GDITEST02, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GDITEST02);
//gdiTest();//
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GdiplusShutdown(gdiPlusToken);
return (int) msg.wParam;
}
/**/
int gdiTest(HDC hdc, HWND hWnd)
{
RECT rc;
GetWindowRect(hWnd,&rc);
RectF drawRect(rc.left ,rc.top ,(rc.right-rc.left) ,(rc.bottom-rc.top));
char* csurname,*csurPath,*ctexname;
csurPath="e://111//"; //原图路径
csurname="bg8.jpg"; //原图名称
ctexname="tex"; //分割图片前缀
int cNum=4; //分割次数
Image* ime;//
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);//得到图片类型
WCHAR w[100];
string s1(csurPath);
string s2(csurname);
s1+=s2;
MultiByteToWideChar(CP_ACP,MB_COMPOSITE,s1.c_str(),200,w,100);
Bitmap *bmp=new Bitmap(w);
int c=cNum;
int pwidth=bmp->GetWidth()/c; //分割图片宽度
int pheight=bmp->GetHeight()/c; //分割图片高度
Bitmap *bmpsplitted=new Bitmap(pwidth,pheight);
int index=1;
char cc[10];
string sj=".jpg";
string s(csurPath);
string stexname(ctexname);
s+=stexname;
const char *ctm=s.c_str();
for(int y=0; y<c; y++)
{
for(int x=0 ;x<c; x++)
{
bmpsplitted=bmp->Clone(RectF(x*pwidth,y*pheight,pwidth,pheight),PixelFormatDontCare); //在这里截取图片
_itoa(index+10000,cc,10);
string si(cc);
s+=si;
s+=sj;
MultiByteToWideChar(CP_ACP,MB_COMPOSITE,s.c_str(),200,w,100);
s=csurPath;
s+=stexname;
bmpsplitted->Save(w, &jpegClsid, NULL);
index+=1;
}
} /* */
ime=bmpsplitted->Clone(RectF(0,0,pwidth,pheight),PixelFormatDontCare);//
Graphics *g1=new Graphics(hdc);
g1->DrawImage(ime,drawRect);
return 0;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望在已添加到 Windows 95 的
// “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
// 才需要此函数及其用法。调用此函数
// 十分重要,这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GDITEST02);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_GDITEST02;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HANDLE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, NULL, NULL, hInstance, NULL);//CW_USEDEFAULT
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//phWnd=hWnd;
return TRUE;
}
//
// 函数: WndProc(HWND, unsigned, WORD, LONG)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
gdiTest(hdc,hWnd);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// sliptImage.cpp : 定义应用程序的入口点。
//
#include <windows.h>
#include <gdiplus.h>
#include <string>
#pragma comment(lib,"gdiplus")
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
using namespace std;
using namespace Gdiplus;
int gdiTest(HDC hdc, HWND hWnd);
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
GdiplusStartupInput input;
ULONG_PTR gdiPlusToken;
if(GdiplusStartup(&gdiPlusToken,&input,NULL)!=Ok)
{
return -1;
}
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GDITEST02, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GDITEST02);
//gdiTest();//
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GdiplusShutdown(gdiPlusToken);
return (int) msg.wParam;
}
/**/
int gdiTest(HDC hdc, HWND hWnd)
{
RECT rc;
GetWindowRect(hWnd,&rc);
RectF drawRect(rc.left ,rc.top ,(rc.right-rc.left) ,(rc.bottom-rc.top));
char* csurname,*csurPath,*ctexname;
csurPath="e://111//"; //原图路径
csurname="bg8.jpg"; //原图名称
ctexname="tex"; //分割图片前缀
int cNum=4; //分割次数
Image* ime;//
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);//得到图片类型
WCHAR w[100];
string s1(csurPath);
string s2(csurname);
s1+=s2;
MultiByteToWideChar(CP_ACP,MB_COMPOSITE,s1.c_str(),200,w,100);
Bitmap *bmp=new Bitmap(w);
int c=cNum;
int pwidth=bmp->GetWidth()/c; //分割图片宽度
int pheight=bmp->GetHeight()/c; //分割图片高度
Bitmap *bmpsplitted=new Bitmap(pwidth,pheight);
int index=1;
char cc[10];
string sj=".jpg";
string s(csurPath);
string stexname(ctexname);
s+=stexname;
const char *ctm=s.c_str();
for(int y=0; y<c; y++)
{
for(int x=0 ;x<c; x++)
{
bmpsplitted=bmp->Clone(RectF(x*pwidth,y*pheight,pwidth,pheight),PixelFormatDontCare); //在这里截取图片
_itoa(index+10000,cc,10);
string si(cc);
s+=si;
s+=sj;
MultiByteToWideChar(CP_ACP,MB_COMPOSITE,s.c_str(),200,w,100);
s=csurPath;
s+=stexname;
bmpsplitted->Save(w, &jpegClsid, NULL);
index+=1;
}
} /* */
ime=bmpsplitted->Clone(RectF(0,0,pwidth,pheight),PixelFormatDontCare);//
Graphics *g1=new Graphics(hdc);
g1->DrawImage(ime,drawRect);
return 0;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望在已添加到 Windows 95 的
// “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
// 才需要此函数及其用法。调用此函数
// 十分重要,这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GDITEST02);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_GDITEST02;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HANDLE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
0, 0, 800, 600, NULL, NULL, hInstance, NULL);//CW_USEDEFAULT
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//phWnd=hWnd;
return TRUE;
}
//
// 函数: WndProc(HWND, unsigned, WORD, LONG)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
gdiTest(hdc,hWnd);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
相关文章推荐
- 使用 Libpng 配合 GDI 完成对 png 图片的解析与显示
- 图片分割的程序
- 使用OpenCVForUnity分割图片
- jquery imgareaselect 使用利用js与程序结合实现图片剪切
- 新闻管理模块(在程序中可以使用的一套完整的增添、修改、查询数据的思路)
- 怎样在CSDN博客中使用程序结果的截图和win7自带的画图工具:可以将jpg图片转为bmp位图。win7的截图工具和绘图工具在哪?
- 在WIn32程序中使用MFC的CInternetSession运行异常,主要是因为获取目前应用程序名出错的解决办法
- 在Linux使用mingw32来编写win32程序
- 使用WIN32 API CreateProcess()以无窗口方式创建DOS程序
- Use Custom Background Image for Application-为自己的Windows Phone程序使用自定义背景图片
- libcurl开源库在Win32程序中使用下载文件显示进度条实例
- Win32程序资源的使用
- android开发中WebView的使用(附完整程序)
- 使用模态对话框搭Win32程序框架
- 使用Win32创建串口通讯程序
- GDI图片分割(C++)
- 使用VC6调试器源码级调试win32汇编程序
- vs 2008 win32程序使用大内存 (>2GB)设置
- VC的Win32控制台程序中使用MFC库文件
- 使用PHP写了一个图片分割等份工具,便于前台页面切图时使用。