borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (USB Port)Next Topic (How do you simulate a right-click?) New Topic New Poll Post Reply
AndreaVB Forum : API : Paths
Poster Message
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 54

icon Paths

Hi there,
I'm creating a Path by using the following formula.A far as pasth construction is concerned, its formed well.But when I want to convert it in a region, everything just doesn't work.I hope somebody shall guide me how to correct my code.

Private Sub Process_Click()
TranCol = RGB(255, 0, 255)
Call BeginPath(Picture1.hdc)
For X = 0 To Picture1.ScaleWidth - 1
For Y = 0 To Picture1.ScaleHeight - 1
    If Not GetPixel(Picture1.hdc, X, Y) = TranCol Then
      If Not bInTransparent Then
        bInTransparent = True
        yStartTransparent = Y
      End If
    Else
      If bInTransparent Then
        Call Rectangle(Picture1.hdc, X, yStartTransparent, X + 1, Y)
        bInTransparent = False
      End If
    End If
  Next Y
  ' If last transparent section extends to the width of the Bitmap, then remove it:
  If bInTransparent Then
   Call Rectangle(Picture1.hdc, X, yStartTransparent, X + 1, Y)
   bInTransparent = False
  End If
Next X
Call EndPath(Picture1.hdc)
rgn = PathToRegion(Picture1.hdc)
Debug.Print rgn
Call SetWindowRgn(Form1.hwnd, rgn, True)
Call DeleteObject(rgn)
End Sub

This procedure will create a path consisting of the part of a bitmap that is not black.Its actually for creating the Region From Bitmap Procedure... Please correct me in this code.

Regards,
Asim Siddiqui...

06-08-2005 at 06:11 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Paths

I am not quite sure what are you doing in this loops, so it is hard to debug, but it shoud be something like this:

1) create bitmap
2) check if pixel is different than the defined transparent color
3) if yes, use setpixel on the bitmap to draw the current pixel

the rest of the code should be fine.

____________________________
If you find the answer helpful, please mark this topic as solved.

09-08-2005 at 08:19 PM
View Profile Send Email to User Show All Posts | Quote Reply
misterxed
Level: VB Lord


Registered: 12-06-2005
Posts: 151
icon Re: Paths

Hi,
Goran, I'm really surprised to see something like that from u... I think u need to read about PATHs and Regions first, and then review what u've written here... I bet u'd laugh too   
here r a few links:
Understanding Paths (MSDN)

Regions

Anyways, I don't mean any disrespect or anything of the sort, it was just to correct u...

____________________________
lOsT...

09-08-2005 at 10:44 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Paths

Not really sure why....

This did do something:

Private Sub Test()
    Dim TranColor As Long
    Dim PixelColor As Long
    Dim X As Long
    Dim Y As Long
    Dim yStartTransparent As Long
    
    Picture1.BackColor = RGB(255, 255, 255)
    Picture1.FillColor = RGB(0, 0, 0)
    Picture1.Cls
    Picture1.AutoRedraw = True
    Picture1.Circle (150, 150), 100
  
    Picture1.ForeColor = RGB(255, 0, 0)
    Picture1.FillColor = RGB(255, 0, 0)
    Picture1.PSet (150, 150)
  
    Picture1.ForeColor = RGB(128, 128, 128)
    Picture1.FillColor = RGB(128, 128, 128)
    
    Me.ScaleMode = vbPixels
    Me.Show


    trancol = RGB(255, 255, 255)
    BeginPath Picture1.hdc
    
'    Rectangle Picture1.hdc, 0, 0, 20, 20
' ALtering the following from Step 5 to nothing (step 1) can cause VB to lock
    For X = Picture1.ScaleLeft To Picture1.ScaleWidth - 1 Step 5
        For Y = Picture1.ScaleTop To Picture1.ScaleHeight - 1 Step 5
            PixelColor = Picture1.Point(X, Y)
            
            If PixelColor <> -1 Then
                If PixelColor <> trancol Then
                    DoEvents
                    Rectangle Picture1.hdc, X, Y, X + 1, Y + 1
' If the below line is +1 not +5, it won't work... similar if line is omitted.
                    Picture1.Line (X, Y)-(X + 5, Y + 5), , BF
                End If
'                If Not PixelColor = trancol Then
'                    If Not bInTransparent Then
'                        bInTransparent = True
'                        yStartTransparent = Y
'                    End If
'                Else
'                    If bInTransparent Then
'                        Rectangle Picture1.hdc, X, yStartTransparent, X + 1, Y
'                        Picture1.PSet (X, Y)
'                        DoEvents
'                        bInTransparent = False
'                    End If
'                End If
            End If
        Next Y
    ' If last transparent section extends to the width of the Bitmap, then remove it:
        If bInTransparent Then
            Rectangle Picture1.hdc, X, yStartTransparent, X + 1, Y
            Picture1.PSet (X, Y)
            DoEvents
            bInTransparent = False
        End If
    Next X

'    Rectangle Picture1.hdc, 20, 20, 40, 40

    EndPath Picture1.hdc
    
    rgn = PathToRegion(Picture1.hdc)
    
    SetWindowRgn Command1.hWnd, rgn, True
    DeleteObject rgn


    Debug.Print "Done"
End Sub


____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

10-08-2005 at 08:19 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 54
icon Re: Paths

Hello,
Very nice answer indeed.I've two questions related to it.How can I do the work done by "Picture1.Line (X, Y)-(X + 5, Y + 5), , BF" using API.And the other thing is what do youthink why vb is crashing when the step is set to 1.

I truned your code into following one.Check this out.It's faster.

Private Sub Test()
    Dim TranColor As Long
    Dim X As Long
    Dim Y As Long
    Dim DC As Long
    Dim PIC As Long
    
DC = CreateCompatibleDC(Picture1.hdc)
PIC = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
Call SelectObject(DC, PIC)
Call RoundRect(DC, 0, 0, 300, 300, 50, 50)
    trancol = RGB(0, 0, 0)
    Call BeginPath(Picture1.hdc)
    For X = Picture1.ScaleLeft To Picture1.ScaleWidth - 1 Step 3
        For Y = Picture1.ScaleTop To Picture1.ScaleHeight - 1 Step 3
            PixelColor = GetPixel(DC, X, Y)
                If PixelColor <> trancol Then
                   DoEvents
                    Call Rectangle(Picture1.hdc, X, Y, X + 1, Y + 1)
                    Picture1.Line (X, Y)-(X + 3, Y + 3), , BF
                End If
        Next Y
        If bInTransparent Then
            Call Rectangle(Picture1.hdc, X, yStartTransparent, X + 1, Y)
        End If
    Next X
    Call EndPath(Picture1.hdc)
    rgn = PathToRegion(Picture1.hdc)
    Call SetWindowRgn(Form1.hwnd, rgn, True)
    Call DeleteObject(rgn)
    Call DeleteObject(DC)
    Call DeleteObject(PIC)
End Sub

What do you think looping over the bits of the Bitmap will make this procedue more faster?

Regards,
Asim Siddiqui.

10-08-2005 at 01:40 PM
View Profile Send Email to User Show All Posts | Quote Reply
Juliemac
Level: Trainee

Registered: 01-08-2005
Posts: 3
icon Re: Paths

DC = CreateCompatibleDC(Picture1.hdc)
PIC = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
Call SelectObject(DC, PIC)

This code found as sub/function not defined.

Please explain?

10-08-2005 at 03:58 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Paths

Their api's

Public Declare Function CreateCompatibleDC Lib "gdi32" Alias "CreateCompatibleDC" (ByVal hdc As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" Alias "CreateCompatibleBitmap" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As Long, ByVal hObject As Long) As Long



Steve  

10-08-2005 at 04:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 54
icon Re: Paths

For Juliemac,
these are the subroutines defined by windows itself for the programmers like us to use.they are thousands in number available for us and coded in different dll files that reside in the system32 folder.They must be defined in your project before you use them in the way steve showed.Now if you want to know what does these three API do.Then ask again.We're here to define it to you...

regards,
asim siddiqui.

10-08-2005 at 08:16 PM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Paths

Not really sure about the crashing bit... all I know was VB was using 100% of the processor (well, 99%). And had to restart the computer to get it to shut down - killing the process, and just terminating the app didn't work.

The Step 1 could've just been too much for vb to process or something (or that + me closing the program [form] mid-loop without realizing it) - but it didn't always happen either, only like 1/10 times, only like 1/50 did I have to restart the computer. However, it did take a long time to do an picture area that's about the standard "form1" size when you add a new form. On my computer, it took like 3 minutes to run at a "Step 1"


Actually this was my first attempt to do any editing of GDI stuff. So I really don't know about the other things.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

10-08-2005 at 09:18 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 54
icon Re: Paths

Hey Rodger, I tried to debug the code to get to the point where the whole time is taken.And I'm surprised to see that the PathToRegion API is taking those 3 minutes.Otherwise the whole path was created in just few milliseconds but after that the PathToRegion API took all the time.
I think I can make it ever.Please you try to correct it too..
And tell how to convert the following line into raw API:
Picture1.Line (X, Y)-(X + 5, Y + 5), , BF

Regards,
Asim Siddiqui.

11-08-2005 at 09:42 AM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Paths

Converting it to a region might just take so much time given all the little "paths" that it has to deal with.

Don't know how to convert the other item. Never tried.

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

11-08-2005 at 08:15 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 54
icon Re: Paths

Hey guys, I found the ultimate solution to this problem.And just see how well the code works.


Public Function fRegionFromBitmap(picSource As PictureBox, Optional lBackColor As Long) As Long
'
' The optional last parameter allows you to specify
' the image's background color.  If left blank, the
' color of the image's top left pixel is used.
'
Dim lReturn   As Long
Dim lRgnTmp   As Long
Dim lSkinRgn  As Long
Dim lStart    As Long
Dim lRow      As Long
Dim lCol      As Long
'
' Create a rectangular region.
' A region is a rectangle, polygon, or ellipse (or a combination
' of two or more of these shapes) that can be filled, painted,
' inverted, framed, and used to perform hit testing (testing
' for the cursor location).
'
lSkinRgn = CreateRectRgn(0, 0, 0, 0)

With picSource
    '
    ' Get the dimensions of the bitmap.
    '
    glHeight = .Height / Screen.TwipsPerPixelY
    glWidth = .Width / Screen.TwipsPerPixelX
    '
    ' If no background color is passed in, get the
    ' red, green, blue (RGB) color value of the top
    ' left pixel in the picturebox's device context (DC).
    '
    If lBackColor < 1 Then lBackColor = GetPixel(.hDC, 0, 0)
    '
    ' Loop through the bitmap, row by row, examining each pixel.
    ' In each row, work from left to right comparing each pixel
    ' to the background color.
    '
    For lRow = 0 To glHeight - 1
        lCol = 0
        Do While lCol < glWidth
            '
            ' Skip all pixels in a row with the same
            ' color as the background color.
            '
            Do While lCol < glWidth And GetPixel(.hDC, lCol, lRow) = lBackColor
                lCol = lCol + 1
            Loop

            If lCol < glWidth Then
                '
                ' Get the start and end of the block of pixels in the
                ' row that are not the same color as the background.
                '
                lStart = lCol
                Do While lCol < glWidth And GetPixel(.hDC, lCol, lRow) <> lBackColor
                    lCol = lCol + 1
                Loop
                If lCol > glWidth Then lCol = glWidth
                '
                ' Create a region equal in size to the line of pixels
                ' that don't match the background color. Combine this
                ' region with our final region.
                '
                lRgnTmp = CreateRectRgn(lStart, lRow, lCol, lRow + 1)
                lReturn = CombineRgn(lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR)
                Call DeleteObject(lRgnTmp)
            End If
        Loop
    Next
End With

fRegionFromBitmap = lSkinRgn
End Function



This is obviously not my code.This code is a written by the founder of Scarms Code Library.The Scarms


Regards,
Asim Siddiqui...

18-08-2005 at 03:16 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : API : Paths
Previous Topic (USB Port)Next Topic (How do you simulate a right-click?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder