MapWinGIS:Image SetImageBitsDC

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

SetImageBitsDC

This function uses a bitmap selected in the specified device context handle as the data to copy into the image for which the function is called. This function requires the image to be the same width and height as the bitmap selected in the device context. Note: Use of this function requires advanced knowledge in windows graphics concepts and is intended for advanced users only.


VB.NET Usage

Function SetImageBitsDC(hDC As Integer) As Boolean

            Parameters

hDC

The device context handle of the device context for which the selected bitmap is to be used to copy the bits into the image used to call the function.
ReturnValue A boolean value representing success or failure of setting the image's bits using the bitmap selected in the specified device context.

Sample Code

    'These are API functions needed to get a device context handle and select a bitmap for the device context
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Integer) As Integer
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
    '...
    Private Sub SetImageBitsDC()
        Dim hBmpPtr As IntPtr
        Dim hBitmap As Integer, hndl As Integer, hDC As Integer
        Dim image As New MapWinGIS.Image()
        Dim success As Boolean
        Dim i As Integer, j As Integer, r As Integer, g As Integer, b As Integer
        Dim bitmap As System.Drawing.Bitmap
        Dim newbitmap As System.Drawing.Bitmap
        Dim color As System.Drawing.Color, newcolor As System.Drawing.Color
        Dim result As Long
        'Create a new Bitmap for the original image
        bitmap = New System.Drawing.Bitmap("C:\Test.bmp")
        'Create a new Bitmap for the modified image
        newbitmap = New System.Drawing.Bitmap(bitmap.Width, bitmap.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
        'For each of the bits in the bitmap, invert the colors for the bit
        For i = 1 To bitmap.Height - 1
            For j = 1 To bitmap.Width - 1
                color = bitmap.GetPixel(j, i)
                r = 255 - color.R
                g = 255 - color.G
                b = 255 - color.B
                newcolor = newcolor.FromArgb(r, g, b)
                newbitmap.SetPixel(j, i, newcolor)
            Next
        Next
        'Get the hBitmap handle to the modified image
        hBmpPtr = newbitmap.GetHbitmap()
        'Get an integer from the intPtr hBitmap
        hBitmap = hBmpPtr.ToInt32
        'Create a compatible device context handle
        hDC = CreateCompatibleDC(0)
        'Select the bitmap for the specified device context
        result = SelectObject(hDC, hBitmap)
        'Create a new MapWinGIS.Image using the width and height of the bitmap
        success = image.CreateNew(newbitmap.Width, newbitmap.Height)
        'Set the image's data to be the bitmap selected in the specified device context
        success = image.SetImageBitsDC(hDC)
        'Add the image to the map
        hndl = Map1.AddLayer(image, True)
    End Sub
  

VB 6 Usage
Function SetImageBitsDC(hDC As Long) As Boolean
            Parameters

hDC

The device context handle of the device context for which the selected bitmap is to be used to copy the bits into the image used to call the function.
ReturnValue A boolean value representing success or failure of setting the image's bits using the bitmap selected in the specified device context.

Sample Code

    'These are the API functions needed to manipulate a bitmap
    Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
    Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    'This is a user declared type used to store info about the bitmap
    Private Type BITMAP
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
    End Type
    '...
    Private Sub SetImageBitsDC()
        Dim hBitmap As Long, hndl As Long, hDC As Long
        Dim image As New MapWinGIS.image
        Dim success As Boolean
        Dim i As Long, j As Long
        Dim bits() As Byte
        Dim bm As BITMAP
        'Constants used to get an hBitmap
        Const IMAGE_BITMAP As Long = 0
        Const LR_LOADFROMFILE As Long = &H10
        Const LR_CREATEDIBSECTION As Long = &H2000
        'Load a bitmap from file storing a handle to the bitmap in hBitmap
        hBitmap = LoadImage(0, "C:\Test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
        'Create a compatible device context handle
        hDC = CreateCompatibleDC(0)
        'Select the bitmap for the specified device context
        SelectObject hDC, hBitmap
        'Get the bitmap info from the bitmap
        GetObjectAPI hBitmap, Len(bm), bm
        'Resize the bits array to hold all of the bits for the bitmap
        ReDim bits(1 To bm.bmWidthBytes, 1 To bm.bmHeight)
        'Get the bits from the bitmap
        GetBitmapBits hBitmap, bm.bmWidthBytes * bm.bmHeight, bits(1, 1)
        'For each of the bits in the bitmap, invert the colors for the bit
        For i = 1 To bm.bmWidthBytes - 1 Step 3
            For j = 1 To bm.bmHeight
                bits(i, j) = 255 - bits(i, j)
                bits(i + 1, j) = 255 - bits(i + 1, j)
                bits(i + 2, j) = 255 - bits(i + 2, j)
            Next j
        Next i
        'Update the bitmap with the inverted color bits
        SetBitmapBits hBitmap, bm.bmWidthBytes * bm.bmHeight, bits(1, 1)
        'Create a new MapWinGIS.Image using the width and height of the bitmap
        success = image.CreateNew(bm.bmWidth, bm.bmHeight)
        'Set the image's data to be the bitmap selected in the specified device context
        success = image.SetImageBitsDC(hDC)
        'Add the image to the map
        hndl = Map1.AddLayer(image, True)
    End Sub

Retrieved from "http://mapwindow.org/wiki/index.php/MapWinGIS:Image_SetImageBitsDC"

This page has been accessed 2,531 times. This page was last modified on 9 November 2005, at 16:45.