Results 1 to 4 of 4

Thread: hDC to stdPicture

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    Ha noi
    Posts
    2

    Angry hDC to stdPicture

    How can I do to create stdPicture object from a specified device context. For example I have a picture box, then I draw something on it. please help me to create a stdole.stdPicture object from this picturebox that I can use it to load on other picture box by method picture2.picture = mvStdPicture. Thanks a lot.
    quadratic

  2. #2
    Zaei
    Guest
    You can do:
    Code:
    picture2.picture = myPicture.image
    ... unless you have some other specific reason for creating an stdPicture object.

    Z.

  3. #3
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    Try out Benjamin Marty's CapturePicture function, it creates an StdPicture from a specified area of any hDC.

    It might be included in a module of this PSC post:
    http://www.planet-source-code.com/vb...29762&lngWId=1
    Or in the source of his open source game toolkit:
    http://gamedev.sourceforge.net/

    When I get home I'll try and remember to find the function and post it here.
    "1 4m 4 1337 #4xz0r!'
    Janus

  4. #4
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    '
    '
    ' GDI Image Tools (v1.0)
    ' Copyright 2001, Kevin Gadd & Benjamin Marty
    ' You are free to use this code in your own applications, and reproduce it on web pages,
    ' Provided you leave this header intact.
    '
    '
    Option Explicit

    Public Type GITRect
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    Public Type iid
    x As Long
    s1 As Integer
    s2 As Integer
    c(0 To 7) As Byte
    End Type

    Public Type PICTDESC
    cbSizeOfStruct As Long
    picType As Long
    hBitmap As Long
    hPal As Long
    End Type

    Public Type BITMAPINFOHEADER '40 bytes
    biSize As Long
    biWidth As Long
    biHeight As Long
    biPlanes As Integer
    biBitCount As Integer
    biCompression As Long
    biSizeImage As Long
    biXPelsPerMeter As Long
    biYPelsPerMeter As Long
    biClrUsed As Long
    biClrImportant As Long
    End Type

    Public Type RGBQUAD
    RGBBlue As Byte
    RGBGreen As Byte
    RGBRed As Byte
    rgbReserved As Byte
    End Type

    Public Type RGBTRIPLE
    rgbtBlue As Byte
    rgbtGreen As Byte
    rgbtRed As Byte
    End Type

    Public Type BITMAPINFO
    bmiHeader As BITMAPINFOHEADER
    bmiColors As RGBQUAD
    End Type

    Public Const DIB_RGB_COLORS = 0
    Public Const BI_RGB = 0&
    Public Const OBJ_PAL = 5
    Public Const BLACK_BRUSH = 4
    Public Const S_OK = &H0

    Public Enum picType ' IPictureDisp.Type
    PICTYPE_UNINITIALIZED = -1
    PICTYPE_NONE = 0
    PICTYPE_BITMAP = 1
    PICTYPE_METAFILE = 2
    PICTYPE_ICON = 3
    PICTYPE_ENHMETAFILE = 4
    End Enum

    Public Declare Function GetCurrentObject Lib "gdi32" (ByVal hDC As Long, ByVal uObjectType As Long) As Long
    Public Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
    Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function FillRect Lib "user32" (ByVal hDC As Long, lpRect As GITRect, ByVal hBrush As Long) As Long
    Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    Public Declare Function OleCreatePictureIndirect Lib "olepro32" (ByRef pPictDesc As PICTDESC, ByRef riid As iid, ByVal fOwn As Boolean, ByRef ppvObj As StdPicture) As Long
    Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
    Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
    Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long

    ' Written by Benjamin Marty
    Public Function CapturePicture(ByVal hDC As Long, ByVal Left As Long, ByVal Top As Long, ByVal Width As Long, ByVal Height As Long) As IPictureDisp
    Dim picGuid As iid
    Dim picDesc As PICTDESC
    Dim hdcMem As Long
    Dim hBmp As Long
    Dim hOldBmp As Long
    Dim Pic As IPictureDisp
    Dim rcBitmap As GITRect

    ' IID_IPictureDisp
    picGuid.x = &H7BF80981
    picGuid.s1 = &HBF32
    picGuid.s2 = &H101A
    picGuid.c(0) = &H8B
    picGuid.c(1) = &HBB
    picGuid.c(2) = &H0
    picGuid.c(3) = &HAA
    picGuid.c(4) = &H0
    picGuid.c(5) = &H30
    picGuid.c(6) = &HC
    picGuid.c(7) = &HAB

    picDesc.cbSizeOfStruct = Len(picDesc)
    hdcMem = CreateCompatibleDC(hDC)
    If hdcMem = 0 Then
    Exit Function
    End If
    hBmp = CreateCompatibleBitmap(hDC, Width, Height)
    If hBmp = 0 Then
    DeleteDC hdcMem
    Exit Function
    End If
    hOldBmp = SelectObject(hdcMem, hBmp)
    If Left >= 0 And Top >= 0 Then
    If BitBlt(hdcMem, 0, 0, Width, Height, hDC, Left, Top, vbSrcCopy) = 0 Then
    SelectObject hdcMem, hOldBmp
    DeleteDC hdcMem
    DeleteObject hBmp
    Exit Function
    End If
    Else
    With rcBitmap
    .Left = 0
    .Top = 0
    .Right = Width
    .Bottom = Height
    End With
    FillRect hdcMem, rcBitmap, GetStockObject(BLACK_BRUSH)
    End If
    SelectObject hdcMem, hOldBmp
    picDesc.hBitmap = hBmp
    picDesc.hPal = GetCurrentObject(hDC, OBJ_PAL)
    picDesc.picType = PICTYPE_BITMAP
    If OleCreatePictureIndirect(picDesc, picGuid, True, Pic) <> S_OK Then
    DeleteDC hdcMem
    DeleteObject hBmp
    Exit Function
    End If
    Set CapturePicture = Pic
    Set Pic = Nothing
    DeleteDC hdcMem
    End Function
    "1 4m 4 1337 #4xz0r!'
    Janus

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