-
Apr 3rd, 2013, 10:52 AM
#1
Thread Starter
New Member
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
-
Apr 3rd, 2013, 11:00 AM
#2
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.
-
Apr 3rd, 2013, 11:51 AM
#3
Thread Starter
New Member
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
-
Apr 3rd, 2013, 11:57 AM
#4
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!
-
Apr 3rd, 2013, 12:31 PM
#5
Thread Starter
New Member
Re: GCHandle to Convert Bitmap to IntPtr
Because it's already a precompiled exe and I'm not using codedom...
-
Apr 3rd, 2013, 03:52 PM
#6
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 ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|