News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Gdi+ for cropping an image

Started by jj2007, July 10, 2022, 10:43:15 PM

Previous topic - Next topic

jj2007

I had hoped that fumbling the src* paras could be used for cropping, but it doesn't seem to work like that. Does anybody have experience with this?

   ;   GdipDrawImageRectRectI(GpGraphics *graphics,
   ;    GpImage *image,
   ;    INT dstx,
   ;    INT dsty,
   ;    INT dstwidth,
   ;    INT dstheight,
   ;    INT srcx,
   ;    INT srcy,
   ;    INT srcwidth,
   ;    INT srcheight,
   ;   GpUnit srcUnit,
   ;   GDIPCONST GpImageAttributes* imageAttributes,
   ;   DrawImageAbort callback,
   ;    VOID * callbackData)

QuoteThe source rectangle specifies the portion of the original image that will be drawn.

NoCforMe

Sorry, no experience here (yet). But according to MSDN,
QuoteThe rectangle specifies the destination for the drawing operation; that is, it specifies the rectangle in which the image will be drawn. If the size of the destination rectangle is different from the size of the original image, the image is scaled to fit the destination rectangle.
(Emphasis mine)

So apparently it scales the image, rather than cropping it. What are your results?

(Edit: stupid mistake on my part. You're altering the source rectangle, not the destination one. Which you would think might crop it. Nevermind.)

Ackshooly, that is so like Microshit documentation: The title of the article is "About cropping and scaling GDI+ images", but it doesn't once mention cropping, only scaling ...
Assembly language programming should be fun. That's why I do it.

six_L

Hi,jj2007
the GdipDrawImageRectRectI is not for cropping.
try to test this.
option casemap:none
option win64:7

include \UASM64\include\windows.inc
include \UASM64\include\gdiplus.inc
include \UASM64\include\commctrl.inc

includelib \UASM64\Lib\user32.lib
includelib \UASM64\Lib\kernel32.lib
includelib \UASM64\Lib\gdi32.lib
includelib \UASM64\Lib\gdiplus.lib

IDI_MAINICO equ 100
IDD_DIALOG1 equ 1000
IDC_STATIC1 equ 1001
BTN_ORIGINAL equ 1002
BTN_COMPRESSED equ 1003
BTN_EXPANDED equ 1004

.data?
hInstance dq ?
hWinMain dq ?
m_gdiplusToken dd ?
m_OlgStaticPrc dq ?
dqMethod dq ? ; 0:original , 1:compressed , 2:expanded

.data
szImageFile db "p111.png",0

.code

OnGdiPlusDraw proc hDC:HDC,_Method:qword
local @pGraphics:QWORD
local @image:QWORD
local @Width:DWORD
local @Height:DWORD
local @buf[32]:WORD

invoke GdipCreateFromHDC,hDC,addr @pGraphics
invoke MultiByteToWideChar,CP_ACP,0,addr szImageFile,-1,addr @buf,32
invoke GdipLoadImageFromFile,addr @buf,addr @image

invoke GdipGetImageWidth,@image,addr @Width
invoke GdipGetImageHeight,@image,addr @Height

.if _Method == 1
mov eax,@Width
shr eax,1 ; @Width = @Width/2
mov @Width,eax

mov eax,@Height
shr eax,1 ; @Height = @Height/2
mov @Height,eax
.elseif _Method == 2
mov eax,@Width
shl eax,1 ; @Width = @Width*2
mov @Width,eax

mov eax,@Height
shl eax,1 ; @Height = @Height*2
mov @Height,eax
.endif
invoke GdipDrawImageRectI,@pGraphics,@image,1,1,@Width,@Height

invoke GdipDisposeImage,@image
invoke GdipDeleteGraphics,@pGraphics
ret

OnGdiPlusDraw endp

InitGgdplus proc
local @gdis:GdiplusStartupInput

mov @gdis.GdiplusVersion,1
mov @gdis.DebugEventCallback,0
mov @gdis.SuppressBackgroundThread,0
mov @gdis.SuppressExternalCodecs,0
invoke GdiplusStartup,addr m_gdiplusToken,addr @gdis,NULL
ret

InitGgdplus endp

_StaticProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
local @ps:PAINTSTRUCT

mov eax,uMsg
.if eax == WM_PAINT
invoke BeginPaint,hWnd,addr @ps
invoke GetStockObject, WHITE_BRUSH ;LTGRAY_BRUSH
invoke FillRect,@ps.hdc,addr @ps.rcPaint,rax
invoke OnGdiPlusDraw,@ps.hdc,dqMethod
invoke EndPaint,hWnd,addr @ps
.else
invoke CallWindowProc,m_OlgStaticPrc,hWnd,uMsg,wParam,lParam
mov rax,FALSE
ret
.endif
mov rax,TRUE
ret

_StaticProc endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

mov eax,uMsg
.if eax == WM_INITDIALOG
invoke LoadIcon,hInstance,IDI_MAINICO
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,rax

invoke GetDlgItem,hWnd,IDC_STATIC1
invoke SetWindowLongPtr,rax,GWL_WNDPROC, addr _StaticProc
.if rax == NULL
invoke MessageBox,0,CStr("SetWindowLong error"),0,0
.else
mov m_OlgStaticPrc,rax
.endif

.elseif eax == WM_COMMAND
mov rax,wParam
.if ax==BTN_ORIGINAL
mov dqMethod,0
.elseif ax==BTN_COMPRESSED
mov dqMethod,1
.elseif ax==BTN_EXPANDED
mov dqMethod,2
.endif
invoke GetDlgItem,hWnd,IDC_STATIC1
invoke InvalidateRect,rax,0,1
.elseif eax == WM_CLOSE
invoke EndDialog,hWnd,0
.else
mov rax,FALSE
ret
.endif
mov rax,TRUE
ret

WndProc endp

WinMainCRTStartup Proc

invoke GetModuleHandle,NULL
mov hInstance,rax
invoke InitGgdplus
invoke DialogBoxParam,hInstance,IDD_DIALOG1,0,addr WndProc,0
invoke GdiplusShutdown,m_gdiplusToken
invoke ExitProcess,NULL

WinMainCRTStartup Endp


end


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <\UASM64\include\resource.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define IDI_MAINICO 100
#define IDD_DIALOG1 1000
#define IDC_TOOLBAR 1000
#define IDC_STATIC1 1001
#define BTN_ORIGINAL 1002
#define BTN_COMPRESSED 1003
#define BTN_EXPANDED 1004
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IDD_DIALOG1 DIALOG 0, 0, 310,200
STYLE DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "GDI-Test"
FONT 10, "MS Serif"
BEGIN
LTEXT "",IDC_STATIC1,1,1,308,180,WS_BORDER
    PUSHBUTTON  "Original", BTN_ORIGINAL, 2,183,50,14
    PUSHBUTTON  "Compressed", BTN_COMPRESSED,54,183,50,14
    PUSHBUTTON  "Expanded", BTN_EXPANDED, 106,183,50,14
END
100 ICON "main.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Say you, Say me, Say the codes together for ever.

jj2007

Quote from: NoCforMe on July 11, 2022, 05:40:54 AMYou're altering the source rectangle, not the destination one. Which you would think might crop it

It does, actually :cool:

Quote from: six_L on July 11, 2022, 08:05:49 AM
Hi,jj2007
the GdipDrawImageRectRectI is not for cropping.
try to test this.

Thanks, six_L. I am afraid your setup is not so compatible with my old Masm32 SDK, but thanks nonetheless :thup:

In the meantime, I checked twice and realised that I had already achieved cropping, using GdipDrawImageRectRectI :rolleyes:

In one of my old tests, I had moved images; and that's basically cropping, especially if you adjust width and height, too.

Sorry for bothering you with my confused ideas :sad:

six_L

Hi,jj2007

Fix my mistakes.
Quotethe GdipDrawImageRectRectI is for cropping.

OnGdiPlusDraw proc hDC:HDC
local @pGraphics:QWORD
local @image:QWORD
local @Width:DWORD
local @Height:DWORD
local @buf[32]:WORD

invoke GdipCreateFromHDC,hDC,addr @pGraphics
invoke MultiByteToWideChar,CP_ACP,0,addr szImageFile,-1,addr @buf,32
invoke GdipLoadImageFromFile,addr @buf,addr @image

invoke GdipGetImageWidth,@image,addr @Width
invoke GdipGetImageHeight,@image,addr @Height

invoke GdipDrawImageRectI,@pGraphics,@image,1,1,@Width,@Height
invoke GdipDrawImageRectRectI,@pGraphics,@image,402,3,85,105,112,62,85,105,2,NULL,NULL,NULL
;invoke GdipDrawImageRectRectI,@pGraphics,@image,402,3,@Width,@Height,112,62,85,85,2,NULL,NULL,NULL

invoke GdipDisposeImage,@image
invoke GdipDeleteGraphics,@pGraphics
ret

OnGdiPlusDraw endp
Say you, Say me, Say the codes together for ever.

jj2007