Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Useful functions with VFP9.0 and GDI+

Graphics

Useful functions with VFP9.0 and GDI+

by  Mike Gagnon  Posted    (Edited  )
&& Load an image onto a VFP form and rotate the image 90 degrees.
Code:
Public oform1

oform1=Newobject("form1")
oform1.Show
Return
Define Class form1 As Form
	Top = 2
	Left = 70
	Height = 481
	Width = 603
	DoCreate = .T.
	Caption = "Form1"
	nsavegraphicshandle = .F.
	Name = "Form1"
	Add Object command1 As CommandButton With ;
		Top = 396, ;
		Left = 288, ;
		Height = 27, ;
		Width = 84, ;
		Caption = "Get Image", ;
		Name = "Command1"
	Procedure command1.Click
		lcPic = Getpict()
		lnAngle = 90
		Public oGr As GpGraphics Of Home(1)+"ffc/_gdiplus.vcx"
		oGr = Newobject('GpGraphics',Home(1)+'ffc/_gdiplus.vcx')
		oGr.CreateFromHWND(Thisform.HWnd)
		Public oLogoImage As GpImage Of Home(1)+"ffc/_gdiplus.vcx"
		oLogoImage = Newobject('GpImage',Home(1)+'ffc/_gdiplus.vcx')
		oLogoImage.CreateFromFile(lcPic)
		oRect= Newobject( ;
			'GpRectangle',Home(1)+'ffc/_gdiplus.vcx',',5, 5, 289, 145)
		If  lnAngle > 0
			lnX = 100
			lnY = 100
			oGr.TranslateTransform(lnX, lnY, 0)
			oGr.RotateTransform(lnAngle, 0)
			oGr.TranslateTransform(-lnX, -lnY, 0)
		Endif
		oGr.DrawImageScaled( oLogoImage, oRect )
	Endproc
Enddefine

&& Write text on an angle on a form.
Code:
Public oform1
oform1=Newobject("form1")
oform1.Show
Return
Define Class form1 As Form
	Top = 2
	Left = 70
	Height = 481
	Width = 603
	DoCreate = .T.
	Caption = "Text with an angle"
	nsavegraphicshandle = .F.
	Name = "Form1"
	Add Object command1 As CommandButton With ;
		AutoSize = .T., ;
		Top = 396, ;
		Left = 288, ;
		Height = 27, ;
		Width = 71, ;
		Caption = "Click here", ;
		Name = "Command1"
	Add Object text1 As TextBox With ;
		Height = 25, ;
		Left = 180, ;
		Top = 24, ;
		Width = 325, ;
		Name = "Text1"
	Add Object label1 As Label With ;
		Caption = "Texte", ;
		Height = 17, ;
		Left = 108, ;
		Top = 24, ;
		Width = 40, ;
		Name = "Label1"
	Add Object spinner1 As Spinner With ;
		Height = 24, ;
		Increment =   5.00, ;
		Left = 372, ;
		SpinnerHighValue = 360.00, ;
		SpinnerLowValue =   0.00, ;
		Top = 72, ;
		Width = 121, ;
		Name = "Spinner1"
	Procedure command1.Click
		Local lcText, lnAngle
		lcText = Alltrim(This.Parent.text1.Value)
		lnAngle = This.Parent.spinner1.Value
		Local oGr As GpGraphics Of ffc/_gdiplus.vcx
		oGr = Newobject('GpGraphics',Home(1)+'ffc/_gdiplus.vcx')
		oGr.CreateFromHWND( Thisform.HWnd )
		oFont = Newobject('GpFont',Home(1)+'ffc/_gdiplus.vcx')
		oFont.Create( "Arial" ;       && font name
		, 20 ;                      && size in units below
		,1;   && attributes
		, 2 ;      && units
		)
		oBounds = Newobject( ;
			'GpRectangle',Home(1)+'ffc/_gdiplus.vcx',' ;
			, 200 ;
			, 50 ;
			, 289 ;
			, 145 ;
			)
		Local oFillColor As GpColor Of ffc/_gdiplus.vcx ;
			, oBrush As GpSolidBrush Of ffc/_gdiplus.vcx
		oFillColor = Newobject( ;
			'GpColor',Home(1)+'ffc/_gdiplus.vcx',' ;
			, 0,255,0 ) && green
		oBrush = Newobject( ;
			'GpSolidBrush', Home(1)+'ffc/_gdiplus.vcx', ' ;
			, m.oFillColor )
		oStringFormat = Newobject( ;
			'GpStringFormat',Home(1)+'ffc/_gdiplus.vcx')
		oStringFormat.Create( )
		oStringFormat.Alignment = 2
		oStringFormat.LineAlignment = 2
		oBounds.W = oBounds.W - 4
		oBounds.H = oBounds.H - 4
		oBounds.X = oBounds.X + 4
		oBounds.Y = oBounds.Y + 4
		oBrush.BrushColor = 0xA8000000
		oGr.RotateTransform(lnAngle)
		oGr.DrawStringA( lcText ;
			, oFont, oBounds, oStringFormat, oBrush )
	Endproc
Enddefine

&& Load an image and write some text in the left-hand corner (today's date in this example) and save it as a JPEG.
Code:
lcfile = GETPICT()
SET CENTURY on
Public oLogoImage As GpImage Of Home(1)+"ffc/_gdiplus.vcx"
oLogoImage = Newobject('GpImage',Home(1)+'ffc/_gdiplus.vcx')
oLogoImage.CreateFromFile(lcFile)
oRect= Newobject( ;
'GpRectangle',Home(1)+'ffc/_gdiplus.vcx',',5, 5, 289, 145)
Public oGr As GpGraphics Of Home(1)+"ffc/_gdiplus.vcx"
oGr = Newobject('GpGraphics',Home(1)+'ffc/_gdiplus.vcx')
oGr.CreateFromImage(oLogoImage)
Local oFillColor As GpColor Of ffc/_gdiplus.vcx ;
, oBrush As GpSolidBrush Of ffc/_gdiplus.vcx
oFillColor = Newobject( ;
'GpColor',Home(1)+'ffc/_gdiplus.vcx',' ;
, 200,200,200 ) && green
oBrush = Newobject( ;
'GpSolidBrush', Home(1)+'ffc/_gdiplus.vcx', ' ;
, m.oFillColor )
oBounds = Newobject( ;
'GpRectangle',Home(1)+'ffc/_gdiplus.vcx',' ;
, 0 ;
, 0 ;
, 289 ;
, 145 ;
)
oLineColor = Newobject( ;
'GpColor',Home(1)+'ffc/_gdiplus.vcx',', 0,0,100 )
oPen = Newobject('GpPen', Home(1)+'ffc/_gdiplus.vcx' )
oPen.Create( m.oLineColor, 3 )
oFont = Newobject('GpFont',Home(1)+'ffc/_gdiplus.vcx')
oFont.Create( "Arial" ;       && font name
, 20 ;                      && size in units below
,1;   && attributes
, 2 ;      && units
)
oStringFormat = Newobject( ;
'GpStringFormat',Home(1)+'ffc/_gdiplus.vcx')
oStringFormat.Create( )
oStringFormat.Alignment = 0
oStringFormat.Linealignment =0
oGr.DrawStringA(Dtoc(Date()), oFont, oBounds,oStringFormat,oBrush)
oLogoImage.Savetofile( "c:\example.jpg", ;
"image/jpeg", "quality=100" )

&& Load an image and create a thumbnail version
Code:
oImage= newobject('GpBitmap','ffc/_gdiplus.vcx') && Create and instance of an image
oImage.CreateFromFile(GETPICT()) && load up the image 
oImg=oImage.GetThumbnailImage(100,100)  && Reduce the image to 100 pixels to 100 pixels
oImg.SaveToFile('c:\image2.jpg',"image/jpeg",'quality=100')

&& Load an image an rotate it, and save the rotated version.
Code:
#define GDIPLUS_ROTATEFLIPTYPE_RotateNoneFlipNone 0
#define GDIPLUS_ROTATEFLIPTYPE_Rotate90FlipNone   1
#define GDIPLUS_ROTATEFLIPTYPE_Rotate180FlipNone  2
#define GDIPLUS_ROTATEFLIPTYPE_Rotate270FlipNone  3

#define GDIPLUS_ROTATEFLIPTYPE_RotateNoneFlipX    4
#define GDIPLUS_ROTATEFLIPTYPE_Rotate90FlipX      5
#define GDIPLUS_ROTATEFLIPTYPE_Rotate180FlipX     6
#define GDIPLUS_ROTATEFLIPTYPE_Rotate270FlipX     7

oImage= newobject(GpBitmap','ffc/_gdiplus.vcx')
oImage.CreateFromFile(GETpic())
oImage.RotateFlip(GDIPLUS_ROTATEFLIPTYPE_Rotate180FlipNone )
oImg=oImage.GetThumbnailImage(32,33)
oImg.SaveToFile('c:\ImgRot.jpg',"image/jpeg",'quality=100')

&& 3D effects on fonts.

Code:
Public oform1
oform1=Newobject("form1")
oform1.Show
Return
Define Class form1 As Form
	Top = 2
	Left = 70
	Height = 481
	Width = 603
	DoCreate = .T.
	Caption = "Texte avec un angle"
	BACKCOLOR = RGB(255,255,255)
	nsavegraphicshandle = .F.
	Name = "Form1"
	Add Object command1 As CommandButton With ;
		AutoSize = .T., ;
		Top = 396, ;
		Left = 288, ;
		Height = 27, ;
		Width = 71, ;
		Caption = "Clicker ici", ;
		Name = "Command1"
	Add Object text1 As TextBox With ;
		Height = 25, ;
		Left = 180, ;
		Top = 24, ;
		Width = 325, ;
		Name = "Text1"
	Add Object label1 As Label With ;
		Caption = "Texte", ;
		Height = 17, ;
		Left = 108, ;
		Top = 24, ;
		Width = 40, ;
		Name = "Label1"
	Procedure command1.Click
	Local lcText
	lcText = Alltrim(This.Parent.text1.Value)
	Local oGr As GpGraphics Of ffc/_gdiplus.vcx
	oGr = Newobject('GpGraphics',Home(1)+'ffc/_gdiplus.vcx')
	oGr.CreateFromHWND( Thisform.HWnd )
	oFont = Newobject('GpFont',Home(1)+'ffc/_gdiplus.vcx')
	oFont.Create( "Arial", 32,1, 2)
	oBounds = Newobject('GpRectangle',Home(1)+'ffc/_gdiplus.vcx',', 200, 50, 289, 145)
Local oFillColor As GpColor Of ffc/_gdiplus.vcx ;
		, oBrush As GpSolidBrush Of ffc/_gdiplus.vcx
	oFillColor = Newobject('GpColor',Home(1)+'ffc/_gdiplus.vcx',', 0,0,0 )
	oFillColor2 = Newobject( ;
		'GpColor',Home(1)+'ffc/_gdiplus.vcx',', 255,255,255 )
	oBrush = Newobject('GpSolidBrush', Home(1)+'ffc/_gdiplus.vcx', ', m.oFillColor )
	oBrush2 = Newobject('GpSolidBrush', Home(1)+'ffc/_gdiplus.vcx', ', m.oFillColor2 )
	oStringFormat = Newobject('GpStringFormat',Home(1)+'ffc/_gdiplus.vcx')
	oStringFormat.Create( )
	oStringFormat.Alignment = 2
	oStringFormat.LineAlignment = 2
	oGr.DrawStringA( lcText, oFont, oBounds, oStringFormat, oBrush )
	oBounds.X = oBounds.X + 1
	oGr.DrawStringA( lcText , oFont, oBounds, oStringFormat, oBrush2 )
	Endproc
Enddefine

&& Convert an JPEG from color to grayscale

Code:
cImage = Getpict('jpg')
ConvertToGrayscale(cImage)

Function ConvertToGrayscale(lcImage)
	Set Classlib To Home()+'ffc/_gdiplus.vcx' Additive
	oLogoImage = Newobject('gpBitmap',Home()+'ffc/_gdiplus.vcx')
	oLogoImage.CreateFromFile(lcImage)
	Private x,Y,nColor
	lnWidth = oLogoImage.imagewidth
	lnHeight = oLogoImage.imageheight
	For Y=0 To lnHeight-1
		For x=0 To lnwidth-1
		    WAIT WINDOW NOWAIT "Width in pixels: "+TRANSFORM(x)+", Height in pixels :" +TRANSFORM(y)
		    nColor = oLogoImage.GetPixel(x,Y)
		    oColor = Createobject("gpColor",nColor)
			oAvg = (oColor.Red+oColor.Green+oColor.Blue)/3
			oColor.set(INT(oAvg),INT(oAvg),INT(oAvg))
	   		oLogoImage.SetPixel(x,Y,oColor.ARGB)
		NEXT
	NEXT
	oLogoImage.SaveToFile("c:\ex.jpg","image/jpeg", "quality=100" )
Endfunc
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top