You are on page 1of 6

1.

Create Color Picker/Shading Effect to a picture

This tutorial of vb6 explain how a programmer can create both a picture color
picker and color shader.
In Visual Basic ,a development environment, a coder can create picture color
picker, in which user can pick the color of the picture and apply it to another .This
program use some of the built in functions.

I think this is a good source for both students and teachers as well.

To do the work we have to SetPixel and GetPixel built in Functions


Add these declaration as follows to the General section of the Code Module

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As
Long, ByVal Y As Long) As Long

Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As
Long, ByVal Y As Long, ByVal crColor As Long) As Long

Secondly we have to create the Shadepicture Function , to this Please Add


these code to the Module
ShadePicture

Public Sub ShadePicture(PicSource As PictureBox, PicTarget As PictureBox,


WithColor As Long, Thickness As Integer)

On Error Resume Next

Dim sRate, Col As Long


Dim X, Y As Single

Dim XMax, YMax As Single

Dim cBlue, cGreen, cRed As Double 'Determines the pixel color

Dim sBlue, sGreen, sRed As Double 'Determines the SHADING color

'Getting the RGB values of selected color

sBlue = Fix((WithColor / 256) / 256)

sGreen = Fix((WithColor - ((sBlue * 256) * 256)) / 256)

sRed = Fix(WithColor - ((sBlue * 256) * 256) - (sGreen * 256))

'Calculate screen height & width of the image

XMax = PicSource.Width / Screen.TwipsPerPixelX - 1

YMax = PicSource.Height / Screen.TwipsPerPixelY - 1

'Initialising Shading
PicTarget.Cls

sRate = Thickness / 10

'Process all pixels and alter them accordingly

For X = 0 To XMax

For Y = 0 To YMax

Col = GetPixel(PicSource.hdc, X, Y)

If Not Col = 0 Then 'Because black colors are usually the borders of an

image and never change border color.It will affect the clarity.

'Getting the RGB values of current pixel

cBlue = Fix((Col / 256) / 256)

cGreen = Fix((Col - ((cBlue * 256) * 256)) / 256)

cRed = Fix(Col - ((cBlue * 256) * 256) - (cGreen * 256))

'Resetting the RGB values of current pixel with the sRate of shading

cRed = cRed + (sRed - cRed) * sRate

cGreen = cGreen + (sGreen - cGreen) * sRate

cBlue = cBlue + (sBlue - cBlue) * sRate

If Not Col = 12632256 Then SetPixel PicTarget.hdc, X, Y, RGB(cRed,


cGreen, cBlue) 'Skipping transparent col and setting the pixel

Else

SetPixel PicTarget.hdc, X, Y, Col

End If

Next Y

PicTarget.Refresh

Next X
End Sub

Now You are ready to Test Your First Shading Program ,By using this You can
Give any color you like to the pictures you using in a VB Project

In order to test the Program you need a Form which contain Three Picture
box control[Original, Processed,ColorPallet as Picture 1] , a scroll bar, and
a picture of color pallet and another as a test picture.

Then Add these Codes

Private Sub Original_MouseDown(Button As Integer, Shift As Integer, X As


Single, Y As Single)

MsgBox Original.Point(X, Y)

End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As


Single, Y As Single)

ShadePicture Original, Processed, Picture1.Point(X, Y), scrRate.Value

End Sub

Private Sub scrRate_Change()

lbThick = scrRate.Value

End Sub

Private Sub Form_Unload(Cancel As Integer)


Clipboard.SetText ("http://tech-guroo.blogspot.com")

If MsgBox("Is it Satisfactory?", vbQuestion + vbYesNo, "?") = vbYes Then

MsgBox "Please Rate my code,The site address is already copied to your


clipboard", vbInformation, "ThankYou"

Else

MsgBox "Please give FeedBack,The site address is already copied to your


clipboard", vbInformation, "Please Give FeedBack"

End If

End Sub

Now You are ready to test your project


You will get the followign result as you move through colors

You might also like