GPIMAGE  FAQS – Frequently asked questions

 

 

HOW CAN I RESIZE AN IMAGE ?

HOW CAN I GET THE PROPERTIES OF AN IMAGE ?

HOW CAN I CREATE A THUMBNAIL OF AN IMAGE ?

HOW CAN I CONVERT AN IMAGE FILE TO ANOTHER FORMAT ?

HOW CAN I DRAW STRINGS AND CONTROL ALIGNMENTS ?

HOW CAN I CONVERT AN IMAGE FILE TO GRAYSCALE ?

HOW CAN I CONVERT AN IMAGE FILE TO NEGATIVE ?

HOW CAN I INCREASE OR REDUCE A COLOR FROM AN IMAGE ?

HOW CAN I CAPTURE A VFP SCREEN AND SAVE IT AS A PICTURE ?

 

 

 

 

 

 

HOW CAN I RESIZE AN IMAGE ?

 

This example resizes an image to 320x240 pixels, a good way to send images attached to emails.

 

#INCLUDE gpImage.h

IF Not "gpImage" $ SET("Procedure")

   SET PROCEDURE TO gpImage ADDITIVE

ENDIF

 

gdip = CREATEOBJECT("gpInit")

img = CREATEOBJECT("gpImage")

 

lcFile = GETPICT()

img.Load(lcFile)

 

lcDestFile = "_" + JUSTSTEM(lcFile)

 

img.Resize(320, 240)

img.SaveAsJPEG(lcDestFile)

*** If we want to reduce image quality to get smaller files,

*** Could be also :

*** img.SaveAsJPEG(lcDestFile,50) && 100 = Best quality

 

img  = NULL

gdip = NULL

 

 

 

HOW CAN I GET THE PROPERTIES OF AN IMAGE ?

 

#DEFINE CRLF CHR(13) + CHR(10)

#INCLUDE gpImage.h

IF Not "gpImage" $ SET("Procedure")

   SET PROCEDURE TO gpImage ADDITIVE

ENDIF

 

gdip = CREATEOBJECT("gpInit")

img = CREATEOBJECT("gpImage")

 

lcFile = GETPICT()

img.Load(lcFile)

 

lcText = "Image Format : " + img.ImageFormat + CRLF + ;

         "Pixel Format : " + img.PixelFormat + CRLF + ;

         "Image Size : " + TRANSFORM(img.ImageWidth) + ;

         " x " + TRANSFORM(img.ImageHeight) + CRLF + ;

         "Horizontal Resolution : " + TRANSFORM(img.HorizontalResolution) + CRLF + ;

         "Vertical Resolution : " + TRANSFORM(img.VerticalResolution)

 

MESSAGEBOX(lcText,64,"Image Properties")

 

img  = NULL

gdip = NULL

 

 

 

HOW CAN I CREATE A THUMBNAIL OF AN IMAGE ?

 

#include gpImage.h

If Not "gpImage" $ Set("Procedure")

      Set Procedure To gpImage Additive

EndIf

 

gdip = CreateObject("gpInit")

img = CreateObject("gpImage")

 

lcFile = GETPICT()

img.Load(lcFile)

 

img.Thumbnail(96, 72)

 

lcDestFile = "_" + JUSTSTEM(lcFile)

img.SaveAsJPEG(lcDestFile)

 

 

 

HOW CAN I CONVERT AN IMAGE FILE TO ANOTHER FORMAT ?

 

The example below asks for an image and saves it in 5 formats supported by GDI+

 

#include gpImage.h
If Not "gpImage" $ Set("Procedure")
   Set Procedure To gpImage Additive
EndIf

gdip = CreateObject("gpInit")
img = CreateObject("gpImage")

lcFile = GETPICT()

img.Load(lcFile)

lcDestFile = "_" + JUSTSTEM(lcFile)

img.SaveAsJPEG(lcDestFile)
img.SaveAsTIFF(lcDestFile)
img.SaveAsBMP(lcDestFile)
img.SaveAsGIF(lcDestFile)
img.SaveAsPNG(lcDestFile)

img  = NULL
gdip = NULL

 

 

 

HOW CAN I DRAW STRINGS AND CONTROL ALIGNMENTS ?

 

*** STRINGALIGN.PRG

lcRoot = SYS(5) + SYS(2003)

CD (lcRoot)

SET DEFAULT TO (lcRoot)

 

#include gpImage.h

If Not "gpImage" $ Set("Procedure")

Set Procedure To gpImage Additive

EndIf

 

lcDest = "tmp" + SYS(2015) + ".jpg"

 

gdip = CreateObject("gpInit")

img  = CreateObject("gpImage")

 

** Creating and saving blank image

img.Create(250, 210)

white  = RGB(255,255,255)

 

lnHeight = img.ImageHeight

lnWidth  = img.ImageWidth

oGr = CreateObject("Graphics",img.getimage())

 

lcText = "Testing String alignment - specially for my friends that speak French"

 

oGr.SetBrush(white)

oGr.SetAlignment(GDIPLUS_STRINGALIGNMENT_Near)

oGr.SetRect(0,0, lnWidth, 70)

oGr.DrawString("LEFT - " + lcText, "Tahoma", 10, "B")

 

oGr.SetRect(0,70, lnWidth, 70)

oGr.SetAlignment(GDIPLUS_STRINGALIGNMENT_Center)

oGr.DrawString("CENTER - " + lcText, "Tahoma", 10, "B")

 

oGr.SetRect(0,140, lnWidth, 70)

oGr.SetAlignment(GDIPLUS_STRINGALIGNMENT_Far)

oGr.DrawString("RIGHT - " + lcText, "Tahoma", 10, "B")

 

img.SaveAsJPEG(lcdest,100)

 

LOCAL loForm as Form

loForm = CREATEOBJECT("MeuForm")

WAIT WINDOW (lcdest) NOWAIT

loForm.imgImage.Picture = lcdest

loForm.Show(1)

 

RETURN

 

*===========================

DEFINE CLASS MeuForm as Form

*===========================

 

    Top  = 0

    Left = 0

    Width = 250

    Height = 210

    Caption = "gpimage - TEXT ALIGNMENT"

 

  ADD OBJECT imgImage as Image WITH ;

    Top  = 0, ;

    Left = 0, ;

    Width = Thisform.Width, ;

    Height = Thisform.Height

   

  PROCEDURE Init

  ENDPROC

 

  PROCEDURE destroy

  oGr  = null

  img  = null

  gdip = null

  ENDPROC

 

ENDDEFINE

 

 

 

HOW CAN I CONVERT AN IMAGE FILE TO GRAYSCALE ?

 

#INCLUDE gpImage.h

IF Not "gpImage" $ SET("Procedure")

   SET PROCEDURE TO gpImage ADDITIVE

ENDIF

 

gdip = CREATEOBJECT("gpInit")

img = CREATEOBJECT("gpImage")

 

lcFile = GETPICT()

 

img.Load(lcFile)

oGr = CREATEOBJECT("graphics",img.GetImage())

oGr.ChangeColors(1)

 

lcDestFile = "_" + JUSTSTEM(lcFile)

 

img.SaveAsJPEG(lcDestFile)

 

oGr  = NULL

img  = NULL

gdip = NULL

 

 

 

HOW CAN I CONVERT AN IMAGE FILE TO NEGATIVE ?

 

That’s easy too.

Use the example above, changing the ChangeColors() parameter to 2

 

oGr.ChangeColors(2)

 

 

 

HOW CAN I INCREASE OR REDUCE A COLOR FROM AN IMAGE ?

 

Using the same prior example, the function “CHANGECOLORS” allows us to do that.

 

*** In this example, we are increasing GREEN in 20%

*** INSERT THIS PIECE OF CODE IN THE PRIOR EXAMPLE

LOCAL lnRed, lnGreen, lnBlue, lnAlpha

*** Allocate Values ranging from -100 to +100

*** 0 – ZERO means no changes

lnRed   = 0

lnGreen = 20

lnBlue  = 0

lnAlpha = 0

oGr.ChangeColors(lnRed, lnGreen, lnBlue, lnAlpha)

 

 

 

HOW CAN I CAPTURE A VFP SCREEN AND SAVE IT ?

 

Create a method in your form and call it CaptureForm

When you need to capture the image, just call Thisform.CaptureForm()

In The 2nd part of this code I use another very useful function, “CROP”, to cut from the captured image just the piece we are interested

 

 

#INCLUDE gpImage.h

IF Not "gpImage" $ SET("Procedure")

   SET PROCEDURE TO gpImage ADDITIVE

ENDIF

 

gdip = CREATEOBJECT("gpInit")

img = CREATEOBJECT("gpImage")

 

img.Capture(Thisform.HWnd)

Img.SaveasBMP("Captured")

 

 

*** You can also remove the Window borders and titles, if you want :

 

LOCAL lnTitleHeight, lnLeftBorder, lnTopBorder

lnTitleHeight = SYSMETRIC(9)

lnLeftBorder = SYSMETRIC(3)

lnTopBorder = SYSMETRIC(4)

 

Img.Crop(lnLeftBorder, lnTitleHeight + lnTopBorder, ;

      Img.ImageWidth - (lnLeftBorder * 2), ;

      Img.ImageHeight - (lnTitleHeight + (lnTopBorder * 2)))

 

Img.SaveasBMP("Captured_Clean")

 

Img = NULL

Gdip = NULL