Results 1 to 7 of 7

Thread: SetPixel Problem

  1. #1

    Thread Starter
    Junior Member mortolee's Avatar
    Join Date
    Nov 2002
    Location
    england, essex
    Posts
    20

    Unhappy SetPixel Problem



    Hi,

    I have used the SetPixel API to fill the screen with lines, i know i could use the MoveToEx, and LineTo api's but i need to set each pixel.

    When it gets to about the 7th line the program crashes.

    Here is the code i put in a module...





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

    Sub Main()


    For a = 1 To 1024 Step 10
    For b = 1 To 768
    SetPixel GetWindowDC(0), a, b, RGB(255, 0, 200)
    DoEvents
    Next b
    SetPixel GetWindowDC(0), a, b, RGB(255, 0, 200)
    DoEvents
    Next a

    End Sub






    It also goes extremely slow!!!


  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    SetPixel is extremely slow by nature. Do a search, there are routines that are (a bit) faster.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    Junior Member mortolee's Avatar
    Join Date
    Nov 2002
    Location
    england, essex
    Posts
    20
    Could you name some please.
    I believe in Death after Life!

  4. #4
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    First can I point out that in the code you gave you used:
    VB Code:
    1. GetWindowDC(0)
    2. 'Shouldnt you use this:
    3. Public Declare Function GetDesktopWindow Lib "user32" () As Long
    4. Public Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
    5.  
    6. GetWindowDC(GetDesktopWindow)
    Anyway theres a neat way of getting a pointer that points to the place in memory the picture is stored and editing it that way, see these two threads for more about it:
    http://www.vbforums.com/showthread.p...hreadid=210508
    http://www.vbforums.com/showthread.p...hreadid=210500
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  5. #5

    Thread Starter
    Junior Member mortolee's Avatar
    Join Date
    Nov 2002
    Location
    england, essex
    Posts
    20
    I didnt really understand those.

    i dont know about the GetWindowDC thing but it seems to work fine.
    I believe in Death after Life!

  6. #6
    Junior Member ISDP's Avatar
    Join Date
    Mar 2002
    Posts
    28
    If you want alternatives to SetPixel look at this thread:
    http://www.vbforums.com/showthread.p...hreadid=209455

    ISDP
    "Mmm, so I hear they have the internet on computers these days..."

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Your variables might be being declared oddly by VB. Try declaring them explicitly before using them.

    Also, NEVER EVER EVER use GetWindowDC(0) in EVERY call to SetPixel or GetPixel. It will slow your program down so very much.

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width