我有一张jpg图片,但是太大了。我想通过编程将其按比例压缩成一张高度和宽度较小的图片保存。(因为我想把所有大小不一的图片都缩放成同样规格的图片显示)。能不能给一段代码,让我学习学习!谢谢

解决方案 »

  1.   

    BOOL StretchBlt(
      HDC hdcDest,      // handle to destination DC
      int nXOriginDest, // x-coord of destination upper-left corner
      int nYOriginDest, // y-coord of destination upper-left corner
      int nWidthDest,   // width of destination rectangle
      int nHeightDest,  // height of destination rectangle
      HDC hdcSrc,       // handle to source DC
      int nXOriginSrc,  // x-coord of source upper-left corner
      int nYOriginSrc,  // y-coord of source upper-left corner
      int nWidthSrc,    // width of source rectangle
      int nHeightSrc,   // height of source rectangle
      DWORD dwRop       // raster operation code
    );
    The following example code is taken from an application that displays an image either at its original size or at twice its original size. (This application uses the default stretch mode.)     hdcScaled = CreateCompatibleDC(hdcScreen); 
     
        hbmScaled = CreateCompatibleBitmap(hdcScreen, 
                        GetDeviceCaps(hdcScreen, HORZRES) * 2, 
                        GetDeviceCaps(hdcScreen, VERTRES) * 2); 
     
        if (hbmScaled == 0) 
            errhandler("hbmScaled", hwnd); 
     
        // Select the bitmaps into the compatible DC. 
     
        if (!SelectObject(hdcScaled, hbmScaled)) 
            errhandler("Scaled Bitmap Selection", hwnd); 
     
    case WM_COMMAND:     // message: command from application menu 
        switch(wParam) 
        { 
            case IDM_SCALEX1: 
                if (fBlt)
                { 
                     fScaled = FALSE; 
                     hdcWin = GetDC(hwnd); 
                     BitBlt(hdcWin, 
                        0,0, 
                        bmp.bmWidth, bmp.bmHeight, 
                        hdcCompatible, 
                        0,0, 
                        SRCCOPY); 
                     ReleaseDC(hwnd, hdcWin); 
                } 
                break; 
     
            case IDM_SCALEX2: 
                if (fBlt)
                { 
                     fScaled = TRUE; 
                     StretchBlt(hdcScaled, 
                         0, 0, 
                         bmp.bmWidth * 2, bmp.bmHeight * 2, 
                         hdcCompatible, 
                         0, 0, 
                         bmp.bmWidth, bmp.bmHeight, 
                         SRCCOPY); 
     
                     hdcWin = GetDC(hwnd); 
                     BitBlt(hdcWin, 
                        0,0, 
                        bmp.bmWidth, bmp.bmHeight, 
                        hdcScaled, 
                        0,0, 
                        SRCCOPY); 
                     ReleaseDC(hwnd, hdcWin); 
                } 
                break; 
      

  2.   

    JPG不知道左面操作,你可以把他转换为BMP然后调用DIB API进行缩放,MSDN里有 有关DIB函数的SAMPLE
      

  3.   

    jpg已经是压缩的了,如果再压缩,肯定要损失信息。网上有个CXImage的库,里面压缩的算法,代码,示例都有.