图像大小调整:我应该做哪些修改?(windows应用程序,vc++)

Image resizing: What corrections should I make? (Window application, VC++)

本文关键字:windows 应用程序 vc++ 修改 调整 我应该 图像      更新时间:2023-10-16

所以我已经被这个图像大小调整问题困扰了一段时间:程序显示图像,然后根据用户在拖动边界时提供的大小来调整它的大小,而不是在向内拖动边界时剪切图像,或者在向外拖动时保持图像不变。

我在主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