Announcement

Collapse
No announcement yet.

Byte Array an Image

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Byte Array an Image

    Dear Sirs,

    Please help! Is it possible to somehow put the image (GIF format) represented by a Byte Array into the Clipboard? If so, I appreciate if you could post the code of how to do it - thanks.

    Cheers!
    Last edited by Gary Domantay; 12 Oct 2009, 09:11 PM.

  • #2
    I don't know how do you plan to save animated GIF image to clipboard, but I think you can stop animation and put every frame to cliboard and then read every frame as byte array (here - string)

    Code:
    '
    ' Use PBWIN 9.02
    '
    
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
        LOCAL hBmp AS DWORD, lRes AS LONG, sBuff AS STRING
        
        ?"Press OK to read clipboard"
        '
        ' get bitmap from clipboard 
        CLIPBOARD GET BITMAP TO hBmp, lRes
        '
        ' select bitmap
        GRAPHIC ATTACH hBmp, 0
        '
        ' load bitmap content into a string...
        GRAPHIC GET BITS TO sBuff
        '
        ' analyze content...
        OPEN "c:\1.dat" FOR BINARY AS 1
        PUT$ 1, sBuff
        CLOSE 1
        '
        ?"Buff size:" & STR$(LEN(sBuff))
    END FUNCTION

    Comment


    • #3
      I have an active-x dll with a routine that creates a Byte array of an image (gif format that will never be animated - it is a barcode to be precise).

      I'm passing a string data into the routine which then return a barcode image in byte array.

      What I need to do is to put the image from the byte array into the clipboard where I can then paste the image into say a Microsoft Word document.

      I'm actually using VB6 to do this but I can use powerbasic also not a problem.

      Comment


      • #4
        You can do it by simple converting the byte array into numeric text
        representation:
        i.e binary data into a sequence of ascii codes in hex.

        havent' tested the code below, but it should works..

        Code:
        '
        ' convert binary data into a sequence of hex values without delimeters
        ' each byte will be a 2 hex digits
        ' 
        FUNCTION d2hex_to_clipboard (byval sData as string) AS LONG
            LOCAL sRz AS STRIG, i AS LONG
            FOR i=1 TO LEN(sData)
                 sRz = sRz & HEX$(ASC(sData,i),2) ' <-- only two hex digits
            NEXT
            CLIPBOARD RESET
            CLIPBOARD SET OEMTEXT sRz
            FUNCTION = LEN(sRz)
        END FUNCTION
        
        '
        ' now, read clipboard and decode hex values into a binary string
        '
        FUNCTION Clipboard_to_Image () AS STRING
            LOCAL sRz AS STRIG, i, cRez AS LONG
            sRz = "" 
            CLIPBOARD GET OEMTEXT TO sRz, cRes
            IF cRes = 0 THEN 
                ?"Problem occured when reading clipboard"
                FUNCTION = ""
                EXIT FUNCTION
            END IF 
            FOR i=1 TO LEN(sRz) STEP 2
                  sRz = sRz & CHR$( VAL("&H"+MID$(sRz,1,2)) ) 
            NEXT
            FUNCTION = sRz
        END FUNCTION

        Comment


        • #5
          Not sure how to pass a VB byte array to PB, but shouldn't be a problem.
          Here is complete test without the byte array.

          If this method could be used with a private clipboard would be great
          in sharing data between Visual Basic and PowerBASIC.

          Looks like the conversion method is above.


          1. Reads .gif from disk
          2. Save .gif to clipboard
          3. Read .gif from clipboard
          4. Compares in memory
          5. Write .gif from memory to disk
          6. ShellExecute to see it works

          Code:
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "win32api.inc"
          $Gif_In   = "c:\keep\happy.gif"
          $Gif_Out  = "c:\keep\junk999.gif"
          FUNCTION PBMAIN AS LONG
            LOCAL ClipResult     AS LONG
            LOCAL h              AS LONG
           
            LOCAL sReadFromDisk  AS STRING
            LOCAL sReadFromClip  AS STRING
           
            h = FREEFILE
            OPEN $Gif_In FOR BINARY AS #h
            IF ERR THEN ? "Error opening file" + STR$(ERRCLEAR):GOTO Exit1
            GET$ #h, LOF(h), sReadFromDisk
            IF ERR THEN ? "Disk read error" + STR$(ERRCLEAR):CLOSE h:GOTO Exit1
            CLOSE #h
            CLIPBOARD RESET, ClipResult
            IF ClipResult <> -1 THEN ? "Clipboard reset error":GOTO Exit1
           
            CLIPBOARD SET OEMTEXT sReadFromDisk, ClipResult
            IF ClipResult <> -1 THEN ? "Clipboard Set error":GOTO Exit1
           
           
            CLIPBOARD GET OEMTEXT TO sReadFromClip , ClipResult
            IF ClipResult <> -1 THEN ? "Clipboard GET error":GOTO Exit1
           
            IF sReadFromDisk = sReadFromClip THEN
              'another check that it actually works
              h = FREEFILE
              OPEN $Gif_Out FOR BINARY AS #h
              IF ERR THEN ? "Open for output error" + STR$(ERRCLEAR)
              PUT$ #h, sReadFromClip
              IF ERR THEN ? "Disk write error" + STR$(ERRCLEAR)
              CLOSE #h
              ShellExecute(0,"open",$Gif_out,"","",1)
            ELSE
              ? "Disk file does not match what was read from clipboard"
            END IF
          Exit1:
            SLEEP 1000
          END FUNCTION
          Last edited by Mike Doty; 13 Oct 2009, 05:34 AM.
          How long is an idea? Write it down.

          Comment


          • #6
            Writing for export in a DLL
            Note: XP SP3 maxed out at 117,108,723 bytes on my 2 gigabyte machine.
            Vista had no problem with 500, 000,000. Not sure what limit is? Vista box has 3 GB.
            I need to pass information between VB and PB and this will help.
            Still need a private clipboard since OEM wipes out the text stuff in my tests.

            Code:
            #COMPILE EXE
            #DIM ALL
            REM #BLOAT 117108723  '117,108,723 bytes maximum bloat on this machine
            #INCLUDE "win32api.inc"
            FUNCTION PBMAIN AS LONG
             
              LOCAL ErrorReturned&, sFileName AS STRING
             
            DO
              sFileName = INPUTBOX$("Prompt","1001 uses of bloat",EXE.FULL$)
              ErrorReturned = FileToClipBoardOEM (sFileName)
              SELECT CASE ErrorReturned
                    CASE 0: ? "No error"
                   CASE -1: ? "Nothing passed"
                   CASE -2: ? "Zero length file"
                   CASE -3: ? "Clipboard set error"
                   CASE -4: ? "Clipboard set oem failed"
                 CASE ELSE: ? "Error" + STR$(ErrorReturned)
              END SELECT
              ? "Length of clipboard " + FORMAT$(LEN(GetClipBoardOEM),"#,")
            LOOP WHILE LEN(sFileName)
            END FUNCTION
            FUNCTION GetClipBoardOEM EXPORT AS STRING
              LOCAL sReadFromClip$, ClipResult& 'return nothing if error
              CLIPBOARD GET OEMTEXT TO sReadFromClip , ClipResult
              IF ClipResult = -1 THEN FUNCTION = sReadFromClip
            END FUNCTION
            FUNCTION FileToClipBoardOEM(sFileName AS STRING) EXPORT AS LONG
             
              LOCAL ClipResult     AS LONG          ' return 0 on success
              LOCAL h              AS LONG
              LOCAL sReadFromDisk  AS STRING
             
             
              'Don't reset the clipboard if invalid file passed
              IF LEN(sFileName) = 0 THEN
                 FUNCTION = -1
                 EXIT FUNCTION                     'nothing passed
              END IF
             
              IF NOT ISFILE(sFileName) THEN
                 FUNCTION = 53
                 EXIT FUNCTION                     'use DOS value for file not found
              END IF
             
              IF FileSize(sFileName) < 1 THEN      'Large filesize routine used
                 FUNCTION = -2
                 EXIT FUNCTION                     'don't use 0 length files
              END IF
             
              h = FREEFILE
              OPEN sFileName FOR BINARY AS #h
              IF ERR THEN FUNCTION = ERRCLEAR:EXIT FUNCTION
             
              GET$ #h, LOF(h), sReadFromDisk
              IF ERR THEN FUNCTION = ERRCLEAR:CLOSE h:EXIT FUNCTION
             
              CLOSE #h
              CLIPBOARD RESET, ClipResult
              IF ClipResult <> -1 THEN FUNCTION = -3:EXIT FUNCTION
             
              CLIPBOARD SET OEMTEXT sReadFromDisk, ClipResult
              IF ClipResult <> -1 THEN FUNCTION = -4:EXIT FUNCTION
             
              FUNCTION = ERRCLEAR  'did anything get missed?
            END FUNCTION
            FUNCTION FileSize(BYVAL sFileName AS STRING) AS QUAD
              LOCAL FindData AS WIN32_FIND_DATA
              LOCAL hDir AS LONG
              hDir = FindFirstFile(BYVAL STRPTR(sFileName), FindData)
              IF hDir = -1 THEN  'if not found return -53
                FUNCTION = -53
              ELSE               'return number of bytes
                FindClose hDir
                FUNCTION = MAK(QUAD, FindData.nFileSizeLow , FindData.nFileSizeHigh)
              END IF
             
            END FUNCTION
            Last edited by Mike Doty; 13 Oct 2009, 09:00 AM.
            How long is an idea? Write it down.

            Comment


            • #7
              To ask the unasked.... why does it have to go on the clipboard?

              You have all the bytes, you can store it on disk, in a memory object, yadda, yadda......

              (Guess: you want to 'paste' into a non-cooperating application?)
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                I have an active-x dll with a routine that creates a Byte array of an image (gif format that will never be animated - it is a barcode to be precise).

                I'm passing a string data into the routine which then return a barcode image in byte array.

                What I need to do is to put the image from the byte array into the clipboard where I can then paste the image into say a Microsoft Word document.

                I'm actually using VB6 to do this but I can use powerbasic also not a problem.
                __________________





                I can see the need if he is using .gif.
                I take that back, I haven't been able to paste gif into anything.
                There is probably another way. Is there a way to copy an image from the clipboard into word?
                Last edited by Mike Doty; 13 Oct 2009, 09:10 AM.
                How long is an idea? Write it down.

                Comment


                • #9
                  I don't think you can directly paste a GIF into the clipboard as a GIF and expect that to be recognized by other programs as a GIF, there is no "GIF" format in the standard list of clipboard formats available, you'll probably need something to convert it to a packed dib and put that onto the clipboard.
                  Furcadia, an interesting online MMORPG in which you can create and program your own content.

                  Comment


                  • #10
                    Another solution is to use GDI+ and/or TB_PICCTX control
                    TB_PICCTX control

                    Comment

                    Working...
                    X