图像大小调整:我应该做哪些修改?(windows应用程序,vc++)
Image resizing: What corrections should I make? (Window application, VC++)
所以我已经被这个图像大小调整问题困扰了一段时间:程序显示图像,然后根据用户在拖动边界时提供的大小来调整它的大小,而不是在向内拖动边界时剪切图像,或者在向外拖动时保持图像不变。
我在主WinAPI中使用了显示窗口算法,并在WM_PAINT中提供了缩放算法实例的声明。
我必须链接到缩放算法从一个单独的类从一个单独的文件在同一项目,我已经包括在头文件(#include"scaling.h")。缩放图像后,我再次调用窗口的显示,但在拖动边界后大小没有任何变化。我必须对代码进行哪些更改?
#include <windows.h>
#include <windows.h>
#include <tchar.h>
#include "scaling.h"
HBITMAP hBitmap;
HDC localDC;
HBITMAP hOld;
BITMAP qB;
HDC hDC; // Handle (virtual memory pointer) to drawing characteristics
RECT rect;
PAINTSTRUCT ps;
HDC MemDC;
HBITMAP bmp;
BITMAP bm;
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:{
//MessageBox(hwnd,_T("Window Procedure Received WM_CREATE Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_LBUTTONDOWN: {
//MessageBox(hwnd,_T("Window Procedure Received WM_LBUTTONDOWN Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_PAINT: { //At program start up the whole window is invalid so must be drawn.
//TCHAR tmpText[]=_T("TempText");
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd,&ps);
BOOL qRetBlit = ::BitBlt(hDC,0,0,qB.bmWidth,qB.bmHeight,localDC,0,0,SRCCOPY);
::GetClientRect(hwnd, &rect);
// Create a memory device compatible with the above DC variable
MemDC = CreateCompatibleDC(hDC);
// Select the new bitmap
SelectObject(MemDC, hBitmap);
GetObject(hBitmap, sizeof(bm), &bm);
int w2,h2;
if(GetClientRect(hwnd, &rect)) //In order to obtain the current window's parameters
{
w2 = rect.right - rect.left;
h2 = rect.bottom - rect.top;
}
BYTE *pixels_old = (BYTE *)qB.bmBits;
scaling sc;
BYTE *pixels = sc.resizePixels(pixels_old, qB.bmWidth, qB.bmHeight, w2, h2);
qB.bmBits = pixels;
MemDC = CreateCompatibleDC(hDC);
SelectObject(MemDC, &qB);
hDC = BeginPaint(hwnd,&ps);
qRetBlit = ::BitBlt(hDC,w2,h2,qB.bmWidth,qB.bmHeight,localDC,0,0,SRCCOPY);
delete []pixels;
// Return Drawing Context To Original State
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY: {
::SelectObject(localDC,hOld);
::DeleteDC(localDC);
::DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iShow){
TCHAR szClassName[]=_T("Name");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof (WNDCLASSEX);
wc.style = 0;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.hIconSm = 0; //LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_SMALL));
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.lpszMenuName = NULL;
RegisterClassEx(&wc);
hBitmap = (HBITMAP)::LoadImage(NULL, _T("D:\Chrysanthemum.bmp"),IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
GetObject(reinterpret_cast<HGDIOBJ>(hBitmap),sizeof(BITMAP),reinterpret_cast<LPVOID>(&qB));
localDC = ::CreateCompatibleDC(hDC);
hOld = (HBITMAP)::SelectObject(localDC,hBitmap);
hWnd = CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,0,0,qB.bmWidth,qB.bmHeight,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0)){
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return (int)messages.wParam;
}
" scaling .h"中的缩放算法如下:(最近邻)
#include <iostream>
class scaling{
public:
BYTE* resizePixels(BYTE* pixels,int w1,int h1,int w2,int h2)
{
BYTE* retval = new BYTE[w2*h2] ;
// EDIT: added +1 to remedy an early rounding problem
int x_ratio = (int)((w1<<16)/w2) +1;
int y_ratio = (int)((h1<<16)/h2) +1;
//int x_ratio = (int)((w1<<16)/w2) ;
//int y_ratio = (int)((h1<<16)/h2) ;
int x2, y2 ;
for (int i=0;i<h2;i++) {
for (int j=0;j<w2;j++) {
x2 = ((j*x_ratio)>>16) ;
y2 = ((i*y_ratio)>>16) ;
retval[(i*w2)+j] = pixels[(y2*w1)+x2] ;
}
}
return retval;
}
};
提前感谢!
先读后问:图像缩放和旋转在C/c++http://www.cplusplus.com/forum/general/2615/
https://msdn.microsoft.com/ru-ru/library/windows/desktop/dd183402 (v = vs.85) . aspx
使用StretchBlt很容易。我认为这是你的拉伸算法中的一个错误。
https://msdn.microsoft.com/en-us/library/windows/desktop/dd145120 (v = vs.85) . aspx
相关文章:
- 如何从Windows应用程序输出到标准?
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 将通用Windows应用程序转换为VisualStudio 2019上的常规应用程序
- Windows 7 应用程序在未聚焦时运行速度较慢
- 在Linux上交叉编译Windows应用程序时如何链接到Winsock?
- 如何使用cmake和Visual Studio 2019编译简单的Windows应用程序?
- 在通用 Windows 应用程序C++上使用 win32 DLL 库
- 为没有qmake/VS的Qt Windows应用程序设置.exe图标
- 从 C# Windows 应用程序调用 C dll 会导致 svchost.exe 崩溃
- C++ Win32 列表框和滑块创建 Windows 应用程序
- 如何通过具有通信行参数的procdump启动Windows应用程序?
- 如何在我的 Windows 应用程序中捕获按下和释放的 CTRL 键?
- Windows 应用程序中的谷歌地图 API
- 如何在Windows应用程序中使用CMAKE获得管理员特权
- 如何使用PJSIP库在MINGW上安装NASM,以构建Windows应用程序的OpenH264库
- 用于创建 Windows 应用程序的 MSDN 应用程序教程
- 在通用Windows应用程序中使用OpenGL,角度:glLoadIdentity和glOrtho undefined
- 如何为 QT Windows 应用程序设置不同的桌面快捷方式图标和任务栏图标不同
- Nvidia TX2上的Windows应用程序
- 使用Outlook 2016:MAPISendMail在Windows应用程序中失败,并返回"MAPI_E_FAILURE"作为错误代码