News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to Resize an Image using GDI+

Started by alonsomm, February 25, 2005, 09:58:58 PM

Previous topic - Next topic

alonsomm

Greeting every body.

I have a problem and I want to know if some one can help me solve it!

I am working on a program that loads jpeg's pictures and display then in a static control which works fine, but I need to create a resized second picture so you can view it a control and the control doesn't have to resize to the full size of the picture. I found acouple of samples but they are all in .NET, C++ which I don't know how to use, so here is the code i have!

IF uMsg==WM_CREATE
INVOKE CreateWindowEx, WS_EX_CLIENTEDGE, addr StaticClass, NULL,SS_BITMAP or WS_CHILD or WS_VISIBLE,\
10, 50, 350,350, hWnd, NULL, hInstance, NULL
mov hImageTestL,eax
Invoke GdipCreateFromHWND,hImageTestL,ADDR grafico
invoke MultiByteToWideChar,CP_OEMCP,MB_PRECOMPOSED,addr NombreArchivo,-1,addr NombreW,255
invoke GdipLoadImageFromFile,ADDR NombreW,ADDR imagen1
invoke GdipDrawImage,grafico,imagen1,0,0
invoke GdipCreateBitmapFromGraphics,200,200,grafico,addr imagen2

The first part works perfectly but in last line is supposed to create a second image (imagen2) with a resize image of imagen1 but when I try to draw imagen2 it just show me a black box (no picture) does any body have an idea why?

dougiem

I think (?) you may have to use a separate Graphics object doing a copy that way. It may be better to use CloneImage and then draw it using thr DrawImageRectRect to resize. or there is a separate method called ThumbNail. There is c++ documentation from http://www.progdigy.com/gdiplus/  which can help
dougiem

alonsomm

Thanks dougiem for your help but i needed some masm samples because all the samples i found were C++, but any way I found how to do it, so here is it, take a look at it:

Create VB Picture box to draw imagen later On
     INVOKE     CreateWindowEx,WS_EX_CLIENTEDGE,addr StaticClass, NULL,SS_BITMAP or WS_CHILD or WS_VISIBLE,\
                10, 50, 350,350, hWnd, NULL, hInstance, NULL
; Save Control handle for later use   
    mov hImageTestL,eax
; Convert Image Filename to Wide character other wise it will return error
    invoke MultiByteToWideChar,CP_OEMCP,MB_PRECOMPOSED,addr NombreArchivo,-1,addr NombreW,255
; Load image from file and save bitmap to imagen1
    invoke GdipLoadImageFromFile,ADDR NombreW,ADDR imagen1
; Get original image pixel format (usually 32 Bit color) and save it to lFormat   
    invoke GdipGetImagePixelFormat,imagen1,addr lFormat 
; Get original image Width and save it to sngWidth
    invoke GdipGetImageWidth,imagen1,addr sngWidth
; Get original image Height and save it to sngHeight   
    invoke GdipGetImageHeight,imagen1,addr sngHeight
; Set W variable to 400 (pixels) this would be new picture width
    mov W,400
; Set H variable to 400 (pixels) this would be new picture height
    mov H,400
; C++ Bitmap::Bitmap(width, height, format) Create new bitmap with widht and height just set and same pixel format as the original image and save it to imagen2
    invoke GdipCreateBitmapFromScan0,W,H,0,lFormat,0,addr imagen2
; Set graphic interpolation mode to high quality output
;   Shrink the image using low-quality interpolation.(InterpolationModeNearestNeighbor)
;   Shrink the image using medium-quality interpolation. (InterpolationModeHighQualityBilinear)
;   Shrink the image using high-quality interpolation. (InterpolationModeHighQualityBicubic)
    invoke GdipSetInterpolationMode,grafico,InterpolationModeHighQualityBilinear  ; equ   6
; Get original image Horizontal resolution and save it to HorRes variable
    invoke GdipGetImageHorizontalResolution,imagen1,addr HorRes
; Get original image Vertical resolution and save it to VerRes variable
    invoke GdipGetImageVerticalResolution,imagen1,addr VerRes
; Set new image vertical and horizontal resolution to match orignal image resolution
    invoke GdipBitmapSetResolution,imagen2,HorRes,VerRes
; Create new graphics object from new image
    invoke GdipGetImageGraphicsContext,imagen2,addr grafico
;    RGB 0,0,0
; Set image background to Black color
    invoke GdipGraphicsClear,grafico,0
; Draw resized original image to graphic object of new bitmap
    invoke GdipDrawImageRectI,grafico,imagen1,0,0,W,H
; Destroy orignal image
    invoke GdipDisposeImage,imagen1
; Delete new image graphic object   
    invoke GdipDeleteGraphics,grafico
; Create standard GDI Bitmap from Gdi+ Bitmap and save bitmap handle in hBitmap variable     
; If you want to rotate image use:
;invoke GdipImageRotateFlip,imagen2,Rotate90FlipNone
    invoke GdipCreateHBITMAPFromBitmap,imagen2,addr hBitmap,0
; Set VB Picture box control image to our new resized image
    invoke SendMessage,hImageTestL,STM_SETIMAGE,IMAGE_BITMAP,hBitmap

Here is a sample code:


[attachment deleted by admin]

dougiem

hi alonsomm,
I hadn't considdered using GdipGetImageVerticalResolution for the size. The function GdipDrawImageRectRectI has a format parameter you can set to 'UnitPixel'. Also it allows you to resize to scale. I have written a few examples and posted them here in the topic "GDI+ Images". note: there are a couple of changes I've made to the structures include file.
dougiem.