Results 1 to 6 of 6

Thread: GCHandle to Convert Bitmap to IntPtr

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    11

    GCHandle to Convert Bitmap to IntPtr

    I need help doing this. I have some source code to do so but I'm lost on 1 part of it. Look below..."Whatever" would be the bitmap but I'm lost on the "Some API Call" part. What API call would I need there?

    Code:
    Dim gch As GCHandle 
    Try gch = GCHandle.Alloc(whatever, GCHandleType.Pinned) 
    SomeAPICall(gch.AddressOfPinnedObject()) 
    Finally 
    If GCHandle.IsAllocated() 
    Then GCH.Free() 
    End Try

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: GCHandle to Convert Bitmap to IntPtr

    "SomeAPICall" would be where you pass in the address of your now pinned bitmap to whatever API you wanted to use it with. It is not crucial to the pinning process, it's merely showing that you can now use that memory address.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    11

    Re: GCHandle to Convert Bitmap to IntPtr

    My goal with this is as follows, to write it to native resources as a bitmap and not as a byte array. Here's my code, maybe you can help...
    Code:
    Imports System.Runtime.InteropServices
    Module WR    
    Private Function ToPtr(ByVal data As Object) As IntPtr        
    Dim h As GCHandle = GCHandle.Alloc(data, GCHandleType.Pinned)        
    Dim ptr As IntPtr        
    Try            
    ptr = h.AddrOfPinnedObject()        
    Finally            
    h.Free()        
    End Try        
    Return ptr    
    End Function    
    
    <DllImport("kernel32.dll", SetLastError:=True)> _    
    Private Function UpdateResource(ByVal hUpdate AsIntPtr, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As UShort, ByVal lpData As IntPtr, ByVal cbData As UInteger) As Boolean    
    End Function    
    <DllImport("kernel32.dll", SetLastError:=True)> _    
    Private Function BeginUpdateResource(ByVal pFileName As String, <MarshalAs(UnmanagedType.Bool)> ByVal bDeleteExistingResources As Boolean) As IntPtr    
    End Function    
    <DllImport("kernel32.dll", SetLastError:=True)> _    
    Private Function EndUpdateResource(ByVal hUpdate As IntPtr, ByVal fDiscard As Boolean) As Boolean    
    End Function    
    
    Public Function WriteRes(ByVal filename As String, ByVal bytes As Byte()) As Boolean        
    Try            
    Dim handle As IntPtr = BeginUpdateResource(filename, False)            
    Dim file1 As Byte() = bytes            
    Dim fileptr As IntPtr = ToPtr(file1)            
    Dim res As Boolean = UpdateResource(handle,"RT_BITMAP", "0", 0, fileptr, Convert.ToUInt32(file1.Length))            
    EndUpdateResource(handle, False)        
    Catch ex As Exception            
    Return False        
    End Try        
    Return True    
    End Function
    End Module

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: GCHandle to Convert Bitmap to IntPtr

    And you don't want to use a VB resource file because ... ?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    11

    Re: GCHandle to Convert Bitmap to IntPtr

    Because it's already a precompiled exe and I'm not using codedom...

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: GCHandle to Convert Bitmap to IntPtr

    I don't think that would work. What you're trying to would actually pass the address of the manged Bitmap object. I really don't expect the Windows kernel to know what to do with that since among other reasons, the kernel predates the .Net framework by eons. It is probably expecting either an hBitmap or DIBSection for image resources.

    That being said. This type of thing could get pretty complicated and have some nasty bugs if you don't get it right in some places even when you know what you're doing. Why do you want to do such a thing anyway ? What problem is this meant to solve ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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