Results 1 to 15 of 15

Thread: Picture to Array, Array to Picture

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    107

    Picture to Array, Array to Picture

    OK I AM GETTING SO ANNOYED OVER THIS PROBLEM, ALL I WANT TO DO IS SEND A JPG OVER WINSOCK AND I CANT FIGURE OUT HOW TO DO IT

    I know I have to change the picture into a byte array to send it and then change it back, BUT WHAT IS THE CODE FOR THIS????

    I've been posting on these messageboards for 4 days with no Good response to this answer, Does anyone know the code or function to do this

    Thanks,
    Justin

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    To make it easy on yourself, I would simply send 'packets'. Send 1024 bytes at a time, by appending the characters to each other in a STRING (ex. String = Chr(0) & Chr(1) & Chr(2), but programatically). Next, use Winsock1.Send String, vbString (I've don't use winsock that often so it might not need the vbString). Then, on the other side, when you get the packets, start writing them to a file.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Huh and what would happen when you encontered a null character, hmm?

    You just get a byte array from the image and send it, there's a function for that. I already gave you the code... ok, here it is, just copy and paste:

    VB Code:
    1. Declarations:
    2.  
    3. Private Type SAFEARRAYBOUND
    4.     cElements As Long
    5.     lLbound As Long
    6. End Type
    7.  
    8. Private Type SAFEARRAY1D
    9.     cDims As Integer
    10.     fFeatures As Integer
    11.     cbElements As Long
    12.     cLocks As Long
    13.     pvData As Long
    14.     Bounds(0 To 0) As SAFEARRAYBOUND
    15. End Type
    16.  
    17. Private Type SAFEARRAY2D
    18.     cDims As Integer
    19.     fFeatures As Integer
    20.     cbElements As Long
    21.     cLocks As Long
    22.     pvData As Long
    23.     Bounds(0 To 1) As SAFEARRAYBOUND
    24. End Type
    25.  
    26. Private Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
    27. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    28. Private Type BITMAP
    29.     bmType As Long
    30.     bmWidth As Long
    31.     bmHeight As Long
    32.     bmWidthBytes As Long
    33.     bmPlanes As Integer
    34.     bmBitsPixel As Integer
    35.     bmBits As Long
    36. End Type
    37.  
    38. Private 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
    39. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    40.  
    41. Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    42.  
    43.  
    44.  
    45. 'To get an array from the image:
    46.  
    47. ' these are used to address the pixel using matrices
    48. Dim pict() As Byte
    49.  
    50. Dim sa As SAFEARRAY2D, bmp As BITMAP
    51. ' get bitmap info
    52. GetObjectAPI Pictbox.Picture, Len(bmp), bmp
    53. ' exit if not a supported bitmap
    54. If bmp.bmBitsPixel <> 24 Then
    55.     MsgBox " 24-bit bitmaps only", vbCritical
    56.     Exit Sub
    57. End If
    58.    
    59. ' have the local matrix point to bitmap pixels
    60. With sa
    61.     .cbElements = 1
    62.     .cDims = 2
    63.     .Bounds(0).lLbound = 0
    64.     .Bounds(0).cElements = bmp.bmHeight
    65.     .Bounds(1).lLbound = 0
    66.     .Bounds(1).cElements = bmp.bmWidthBytes
    67.     .pvData = bmp.bmBits
    68. End With
    69. CopyMemory ByVal VarPtrArray(pict), VarPtr(sa), 4
    70.  
    71.  
    72.  
    73. 'To put it back and clean up:
    74.  
    75. ' clear the temporary array descriptor
    76. ' without destroying the local temporary array
    77. CopyMemory ByVal VarPtrArray(pict), 0&, 4

    Hope that helps
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    So, you're telling him to send an uncompressed bitmap?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    you're right Sas

    Ok, why don't you forget all that code, and just load the file as binary to a byte array, then send it like that? The other program would just create a file with that array and load it as a JPG
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    107
    Thanks

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    No problem
    Thanks Sas
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Your way would've worked well for LAN situations.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  9. #9
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Yes, you're right, but usually Winsock is used for internet connections... (writing and reading the file would have taken much longer, right?)
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  10. #10
    Junior Member
    Join Date
    Nov 2002
    Posts
    18
    I have a question regarding GetObjectAPI. Can you use it on any StdPicture object to get the image data? Also, is there a way to do it in reverse - i.e. a SetObjectAPI function? That would be quite useful to me.

    Also, could these methods be used on a D3D texture object, or is there another method that one has to use?

  11. #11
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You can use SelectObject, I believe.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  12. #12
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    I think I should point this out that in the code given above by Jotaf98 he's missed off this line,as most example of this method do:
    VB Code:
    1. Private Declare Function VarPtr Lib "msvbvm50.dll" (Ptr As Any) As Long
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  13. #13
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    VarPtr is standard VB syntax. VarPtrArray isn't.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Using SAFEARRAY in VB, it's paradox...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Saxtraxi i'm not saying replace the API with the one I gave, I'm saying you need it aswel because in the following line its called twice, one passes an array and one passes a non-array
    VB Code:
    1. CopyMemory ByVal VarPtrArray(pict), VarPtr(sa), 4
    It thro's up errors without the "VarPtr" being declared.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

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