C++-如何得到具有相同背景的图片中的不同部分?
我想获取图片中不同的部分,应该如何做到? 我用dc来进行处理,但是得到的图失真了,代码如下:
// test1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "test1.h"
#include <WindowsX.h>
#include <wingdi.h>
#pragma comment(lib,"Msimg32.lib")
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST1));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX 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_TEST1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;//MAKEINTRESOURCE(IDC_TEST1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rcClient;
GetClientRect(hWnd,&rcClient);
// TODO: Add any drawing code here...
HDC hdcBk = CreateCompatibleDC(hdc);
HBITMAP hBmpBk = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP2));//bk.bmp
SelectBitmap(hdcBk,hBmpBk);
HDC hdcPic = CreateCompatibleDC(hdc);
HBITMAP hBmpPic = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1));//pic.bmp
SelectBitmap(hdcPic,hBmpPic);
BitBlt(hdcPic,0,0,646,368,hdcBk,0,0,SRCINVERT);
// BitBlt(hdc,0,0,646,368,hdcPic,0,0,SRCAND);
// BitBlt(hdc,0,0,646,368,hdcPic,0,0,SRCAND);
// BitBlt(hdc,0,0,646,368,hdcBk,0,0,SRCCOPY);
TransparentBlt(hdc,0,0,646,368,hdcPic,0,0,646,368,RGB(0,0,0));
DeleteBitmap(hBmpBk);
DeleteBitmap(hBmpPic);
DeleteDC(hdcBk);
DeleteDC(hdcPic);
EndPaint(hWnd, &ps);
}break;
//case WM_ERASEBKGND:break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题算是基本解决了, 但是感觉效率上不高效, 我是直接比较两个图片的像素,然后生成两张可以做SRCAND和SRCPAINT的位图, 然后就和网上常见的贴透明位图是一样的了, 我把方法放在了博客上, 代码没有经过优化, 需要的可以看一下:
http://www.cnblogs.com/likebeta/archive/2012/08/30/2663938.html
有没有试验过,用XOR计算。即将一个图片绘制上,另一个图片使用异或绘制。相同的背景部分,就是黑色的。