ImageMan ActiveX
Drawing on the Control

The ability of the image control to return a handle to a Windows Device Context (hDC) allows you to alter the actual image in memory. Uses of this would include redlining applications, drawing text on a form in the control, a paint application, and many others.

 

The hDC property can be used with the Windows GDI functions to manipulate the image. The property changes with each image so it must be obtained after loading an image. Also certain actions which change the internal image such as rotation and color reduction will cause the hDC property to change.

 

The following code draws a line on the image:

 

Private mvarMyDc As Long

Private Type POINTAPI

X As Long

Y As Long

End Type

 

Private Declare Sub DeleteObject Lib "gdi32" (ByVal hObject As Long)

Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long

Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long

Private Declare Function LineTo Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long

Private Declare Function MoveToEx Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long

 

Private Sub mnuFileOpen_Click()

ImageControl1.GetFilename

ImageControl1.Width = Form1.ScaleWidth

ImageControl1.Height = Form1.ScaleHeight

ImageControl1.Refresh

End Sub

 

Private Sub mnuLineDraw_Click()

Dim hPen As Long

Dim hOldPen As Long

Dim pTemp As POINTAPI

Dim nStore As Long

mvarMyDc = ImageControl1.hDC

hPen = CreatePen(0, 10, vbRed)

hOldPen = SelectObject(mvarMyDc, hPen)

nStore = MoveToEx(mvarMyDc, 0, 0, pTemp)

nStore = LineTo(mvarMyDc, 500, 500)

ImageControl1.Refresh

DeleteObject (SelectObject(mvarMyDc, hOldPen))

End Sub

 

After altering the image using the hDC property you must call the Refresh method to show the changes on the screen. It is also important not to use the hDC after the image has been removed from the control, as this may cause Windows to crash.

 

 

 


© 2014 Data Techniques, Inc. All rights reserved.

Submit feedback on this topic