Results 1 to 11 of 11

Thread: PIctureBox "Device Context" problems

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    PIctureBox "Device Context" problems

    I am using an "old" third-party DLL which has several graphics-based procedures that need to be called with the Device Context of a PictureBox. The sample VB6 code - which works perfectly - uses simply pb1.hdc (where PB1 is of course a PictureBox). Now hdc is not available (directly) in VB.NET so I did some Googling and came up with two methods for getting the hdc of a PictureBox. Assume pb1 is my PictureBox:

    Code:
    ' Function
    Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As IntPtr) As IntPtr
    
    ' Usage
    Dim hdc As IntPtr = GetDC(pb1.Handle)
    
    
    ' or, without declaring any functions
    Dim hdc As IntPtr = pb1.CreateGraphics.GetHdc.ToInt32
    Neither of these work; they both produce an AccessViolationException when I call the functions in the DLL ("Attempted to read or write protected memory. This is often an indication that other memory is corrupt."). On closer inspection I have found that these two methods not only produce different results, but the results change on every call. Surely that can't be right?

    I hate to word it this way, but is there a way to get the hdc of PictureBox in VB.NET that is exactly equivalent to what the old pb1.hdc would have given in VB6? (Yes, I know they are different languages ...)

    Thanks.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: PIctureBox "Device Context" problems

    If all you want is to get the handle of the property, then don't use the GetDc function; just use <control>.Handle:
    Code:
    Dim hDc As IntPtr = MyPictureBox.Handle
    By the way, I'm not a VB6 programmer. I'm just referring to this: http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: PIctureBox "Device Context" problems

    I didn't think a "handle" was quite the same thing as a "handle to a device context". Unfortunately using the handle of the picturebox doesn't work.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: PIctureBox "Device Context" problems

    Does an exception get thrown whenever you use the Handle property or does it just not work?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: PIctureBox "Device Context" problems

    An exception is thrown. I'm quite prepared to accept there could be more problems than just getting the device context, but an error there would certainly stop the process in its tracks.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: PIctureBox "Device Context" problems

    What is the exception? Is it the same as the exception in the first post? What exactly is it that you're wanting to do with the picturebox, because it may be much simpler just to start from scratch than to rely on the older DLL.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: PIctureBox "Device Context" problems

    Yes, the same AccessViolationException. Writing from scratch is not an option. The dll provides some sophisticated audio DSP and FFT functions which are not available elsewhere.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: PIctureBox "Device Context" problems

    Well then... If you're unable to start from scratch then I two other alternatives and then I'm out:
    Code:
    Dim hDc As Runtime.InteropServices.HandleRef = New Runtime.InteropServices.HandleRef(MyPictureBox.CreateGraphics, MyPictureBox.CreateGraphics.GetHdc)
    Or change the target cpu to any.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: PIctureBox "Device Context" problems

    I have a program that switches between drawing with GDI (BitBlt and StretchBlt need an hDC) or normal VB.Net drawing and the drawing is done in the Paint Event.
    The two methods are mutually exclusive in that if you get an hDC for the graphics, you can't use the graphics object to do any drawing until you release the hDC.
    So, if you want to draw in the picturebox, you should first do an Invalidate to trigger the Paint event.
    Then in the paint event you want to get an hDC, and then use that in your GDI (not GDI+) drawing methods.
    Then release the hDC.
    Code:
      Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
          Dim dstHDC As IntPtr = e.Graphics.GetHdc
    
        '... Drawing stuff using hdC, like bitblt, StretchBlt etc...
    
          e.Graphics.ReleaseHdc(dstHDC)  'release the hDC (allows using e.Graphics to draw now, if you like)
      End Sub
    If you want non-drawing persistence, then instead of drawing in the paint event of the picturebox, you could use a bitmap or image object, create a graphics object from that, and then get an hDC for the bitmap, and draw in that.
    You could then assign that bitmap to the picturebox so you have a persistent image along the same lines as if you had AutoRedraw set to True for a picturebox in VB6.
    Last edited by passel; Oct 15th, 2014 at 03:33 PM.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: PIctureBox "Device Context" problems

    Thanks to you both. I'm going to try those ideas within the next hour.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: PIctureBox "Device Context" problems

    passel - your method seems to have fixed the exception, although the function that is called with the hdc isn't actually working yet. I'll work on it more tomorrow. Thanks to dday9 for your input too.

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