Announcement

Collapse
No announcement yet.

Simple Graphic Example

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

  • Simple Graphic Example

    I am having some difficulty with aspects of getting a simple graphic example running under PB/Win 8.04. I wish to avoid the Windows API in accomplishing the following simple task:

    1. Read a bit map file from the hard disc at C:\Test.BMP
    2. Draw a solid circle of some size, superimposed over the bit map image
    3. Store the composite image back to the hard disc at C:\Test1.BMP

    Might someone have the time to write a simple exe to accomplish these tasks?

    If the original bit map image has a 256 color LUT, is there anyway to store the modified image with the original LUT intact in lieu of the 24 bit color presentation associated with GRAPHIC SAVE?

  • #2
    EEXAMPLE 701: DRAWING SIMPLE GRAPHICS - DDT style

    for PBWin 7.xx (not using GRAPHIC)



    Code:
    'EXAMPLE 701: DRAWING SIMPLE GRAPHICS - DDT style
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CALLBACK FUNCTION My_Proc() AS LONG
    LOCAL hBrushInfo AS LOGBRUSH, hBrush AS LONG, hPen AS LONG
    
    	SELECT CASE CBMSG   '----------- start message handler ----------
    	CASE %WM_PAINT
    		 LOCAL ps AS PAINTSTRUCT
    		 hDC& = BeginPaint(CBHNDL,ps)
    			  '-------- using CreateBrushIndirect ---------
    			  hBrushInfo.lbColor=RGB(0,0,255)
    			  CreateBrushIndirect hBrushInfo  TO hBrush&
    			  SelectObject hDC&, hBrush&
    					Ellipse hDC&,10,10,190,190
    			  DeleteObject hBrush&
    			  '-------- using CreateHatchBrush ---------
    			  CreateHatchBrush %HS_DIAGCROSS, RGB(255,255,0)  TO hBrush&
    			  SelectObject hDC&, hBrush&
    					Rectangle hDC&,50,40,300,100
    			  DeleteObject hBrush&
    			  '-------- using CreatePen ---------
    			  CreatePen %PS_SOLID,3,RGB(0,255,0) TO hPen&
    			  SelectObject hDC&, hPen
    					 MoveTo hDC&, 100, 50
    					 LineTo hDC&, 250, 180
    			  DeleteObject hPen
    		 EndPaint CBHNDL,ps
    	FUNCTION=%TRUE
    	CASE %WM_DESTROY
    					 PostQuitMessage 0
    	EXIT FUNCTION
    	END SELECT    '-----------  end message handler  ----------
    END FUNCTION ' (My_Proc)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FUNCTION PBMAIN
        LOCAL hDlg AS LONG, hBrush AS LONG, Count AS LONG
        DIALOG NEW %HWND_DESKTOP, "DRAWING SIMPLE GRAPHICS - DDT style",100,100,220,130, _
                                  %WS_overlappedwindow,TO hDlg&
        DIALOG SHOW MODELESS hDlg&, CALL My_Proc&()
        DO              '----------- start message loop ----------
            DIALOG DOEVENTS TO Count&
        LOOP WHILE Count&
    END FUNCTION ' (PBMAIN)
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Comment


    • #3
      EXAMPLE 705: GRAPHICS - Using a Back Buffer - DDT style



      Code:
      'EXAMPLE 705: GRAPHICS - Using a Back Buffer - DDT style
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
      
      DECLARE CALLBACK FUNCTION My_Proc() AS LONG
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      FUNCTION PBMAIN
          LOCAL hDlg AS LONG, hBrush AS LONG , Count AS LONG
          DIALOG NEW %HWND_DESKTOP, "GRAPHICS - Using a Back Buffer - DDT style",100,100,215,135, _
                                    %ws_thickframe OR %ws_sysmenu,TO hDlg&        
          DIALOG SHOW MODELESS hDlg&, CALL My_Proc&()
          DO   
              DIALOG DOEVENTS TO Count&
          LOOP WHILE Count&
      END FUNCTION ' (PBMAIN)
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CALLBACK FUNCTION My_Proc() AS LONG
      	DIM hBrushInfo AS LOGBRUSH, hBrush AS LONG
         STATIC hMemDC AS LONG, hMemBmp AS LONG
      
      	SELECT CASE CBMSG   '----------- start message handler ----------
      			CASE %WM_INITDIALOG
      			 hBrushInfo.lbColor=RGB(0,0,255)
      			 CreateBrushIndirect hBrushInfo TO hBrush&
      			 hDC& = GetDC(%HWND_DESKTOP)
      			  
      			 hMemDC&= CreateCompatibleDC(hdc&)
      			 hMemBmp&= CreateCompatibleBitmap(hdc&,300,200)
      			 SelectObject hMemDC&, hMemBmp&
      			 SelectObject hMemDC&, hBrush&
      		
      			 Rectangle hMemDC&,0,0,300,200
      			 BitBlt hMemDC&,10,10,260,160,hDC&,1,1 ,%SRCCOPY
      			 
      			 DeleteObject hMemBmp&
      			 DeleteObject hBrush&
      		
      			CASE %WM_PAINT
      			 LOCAL ps AS PAINTSTRUCT
      			 hDC& = BeginPaint(CBHNDL,ps)
      				BitBlt hDC&,20,10,280,180,hMemDC&,0,0 ,%SRCCOPY
      			 EndPaint CBHNDL, ps
      			CASE  %WM_DESTROY
      			  DeleteDC hMemDC
      			  PostQuitMessage 0 
      			 EXIT FUNCTION
      	END SELECT   '-----------  end message handler  ----------
      END FUNCTION ' (My_Proc)
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      Comment


      • #4
        Capture/Save Bitmap



        Code:
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #COMPILE EXE
        #INCLUDE "WIN32API.INC"
        GLOBAL hDlg&,hLoadDlg&, hMemDC&,hMemBmp& ,hMemDC2&,hMemBmp2&
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           $BmpFile = "C:\MyBmp.Bmp"
        
           '====================================================
           FUNCTION CreateTrueColorBmpFile (FileBmp AS STRING, hWnd AS LONG, rc AS RECT) AS LONG
              LOCAL f AS LONG, hDC AS LONG, hMemDC AS LONG
              LOCAL hMemBmp AS LONG, bm AS BITMAP, bmi AS BITMAPINFO
              LOCAL lpBITMAPFILEHEADER AS BITMAPFILEHEADER, lpBITMAPINFOHEADER AS BITMAPINFOHEADER
        
              hDC = GetDC(hWnd): hMemDC = CreateCompatibleDC (hDC)
        
              bmi.bmiHeader.biSize = SIZEOF(bmi.bmiHeader)
              bmi.bmiHeader.biWidth = (rc.nRight - rc.nLeft)
              bmi.bmiHeader.biHeight = (rc.nBottom - rc.nTop)
              bmi.bmiHeader.biPlanes = 1
              bmi.bmiHeader.biBitCount = 24
              bmi.bmiHeader.biCompression = %BI_RGB
              hMemBmp = CreateDIBSection(hMemDC, bmi, %DIB_RGB_COLORS, 0, 0, 0)
              GlobalLock hMemBmp
              SelectObject hMemDC, hMemBmp
              GetObject hMemBmp, SIZEOF(bm), bm
              BitBlt hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, rc.nLeft, rc.nTop, %SRCCOPY
        
              lpBITMAPFILEHEADER.bfType = CVI("BM")
              lpBITMAPFILEHEADER.bfSize = LEN(lpBITMAPFILEHEADER) + LEN(lpBITMAPINFOHEADER) + _
                 bm.bmWidthBytes * bm.bmHeight
              lpBITMAPFILEHEADER.bfOffBits = 54
              lpBITMAPINFOHEADER.biSize = 40
              lpBITMAPINFOHEADER.biWidth = bm.bmWidth
              lpBITMAPINFOHEADER.biHeight = bm.bmHeight
              lpBITMAPINFOHEADER.biPlanes = 1
              lpBITMAPINFOHEADER.biBitCount = 24
              lpBITMAPINFOHEADER.biSizeImage = bm.bmWidthBytes * bm.bmHeight
        
              f = FREEFILE: OPEN FileBmp FOR OUTPUT AS #f
              PRINT #f, lpBITMAPFILEHEADER lpBITMAPINFOHEADER _
                  PEEK$(bm.bmBits, bm.bmWidthBytes * bm.bmHeight);: CLOSE #f
        
              ReleaseDC hWnd, hDC: DeleteDC hMemDC: GlobalUnlock hMemBmp: DeleteObject hMemBmp
           END FUNCTION
        
        CALLBACK FUNCTION Load_proc()
        LOCAL hDC&
        
        SELECT CASE CBMSG
            CASE %WM_INITDIALOG
            LOCAL xx&,yy& ,Bm2 AS BITMAP ,rc AS rect
        
            hdc= GetDc(CBHNDL)
            hMemDC2&= CreateCompatibleDC(hdc&)
            hMemBmp2&= CreateCompatibleBitmap(hdc&,1,1)
        
            hMemBmp2& = LoadImage(GetModuleHandle (BYVAL 0), $BmpFile, %IMAGE_BITMAP,0,0,%LR_LOADFROMFILE)
           ' hDC& = GetDC(CBHNDL)
           ' hMemDC2&= CreateCompatibleDC(hDC&)
            SelectObject hMemDC2&, hMemBmp2&
            releaseDC CBHNDL,hDC&
            GetObject hMemBmp2&, SIZEOF(bm2),bm2
        
            DIALOG PIXELS hLoadDlg&, bm2.bmWidth,bm2.bmHeight TO UNITS xx&,yy&
            DIALOG SET SIZE hLoadDlg&,xx&+20,yy&+42
            setwindowText hLoadDlg&,$BmpFile
            InvalidateRect CBHNDL,BYVAL %NULL, 1
            DeleteObject hMemBmp2&
        
        CASE %WM_PAINT
             LOCAL ps AS PAINTSTRUCT
             GetObject hMemBmp2&, SIZEOF(bm2),bm2
                hDC& = BeginPaint(CBHNDL,ps)
                    BitBlt hDC&,10,32,bm2.bmWidth,bm2.bmHeight,hMemDC2&,0,0,%srccopy
                EndPaint CBHNDL, ps
             FUNCTION=%TRUE
        CASE %WM_DESTROY
             DeleteDC hMemDC2&
             DeleteObject hMemBmp2
             PostQuitMessage 0
                EXIT FUNCTION
        END SELECT
        END FUNCTION
        
        CALLBACK FUNCTION SrcProc&() AS LONG
        LOCAL hBrush&
            SELECT CASE CBMSG   '----------- start message handler ----------
        CASE %WM_PAINT
            LOCAL ps AS PAINTSTRUCT
            hDC& = BeginPaint(CBHNDL,ps)
            CreateSolidBrush RGB(255,255,0)  TO hBrush&
            SelectObject hDC&, hBrush&
            DeleteObject hBrush&
                Ellipse hDC&,10,10,100,200
            CreateHatchBrush %HS_DIAGCROSS, RGB(255,255,0)  TO hBrush&
            SelectObject hDC&, hBrush&
            DeleteObject hBrush&
                Ellipse hDC&,110,10,300,150
            EndPaint CBHNDL,ps
        FUNCTION=%TRUE
                CASE %WM_DESTROY
                     PostQuitMessage 0
                EXIT FUNCTION
            END SELECT          '-----------  end message handler  ----------
        END FUNCTION ' (DlgProc&)
        
        '_________________________________________________________________________________________
        CALLBACK FUNCTION  CaptureProc&()
        LOCAL hdc&
        STATIC rc AS rect
        SELECT CASE CBMSG
        CASE %WM_COMMAND
            SELECT CASE CBCTL
            CASE 100
            SelectObject hMemDC&, hMemBmp&
            DeleteObject hMemBmp&
            GetWindowRect hDlg&, rc
            BitBlt hMemDC&,1,1,rc.nright-rc.nleft,rc.nbottom-rc.ntop,GetDC(%HWND_DESKTOP),rc.nleft,rc.ntop ,%SRCCOPY
            RedrawWindow CBHNDL, BYVAL %NULL, BYVAL %NULL, %RDW_ERASE OR %RDW_INVALIDATE
        
            MSGBOX "Also copying the dialog screen to file with root name:  " + $BmpFile
            CreateTrueColorBmpFile $BmpFile, GetDesktopWindow, rc
        
            CASE 200
            DIALOG NEW 0, "Load Image Viewer",100,100, 190, 190, %ws_thickframe OR %ws_sysmenu, TO hLoadDlg&
            DIALOG SHOW MODELESS hLoadDlg&, CALL Load_proc()
        
        
            END SELECT
        CASE %WM_PAINT
            LOCAL ps AS PAINTSTRUCT
            hDC& = BeginPaint(CBHNDL,ps)
            BitBlt hdc&,10,80,rc.nright-rc.nleft,rc.nbottom-rc.ntop,hMemDC&,1,1 ,%SRCCOPY
            EndPaint CBHNDL,ps
        FUNCTION=%TRUE
        CASE %WM_DESTROY
             DeleteDC hMemDC&
             PostQuitMessage 0
        FUNCTION = %TRUE
        END SELECT
        END FUNCTION   '(CaptureProc&)
        
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        FUNCTION PBMAIN
            LOCAL  hBrush&, Msg AS tagMsg
            LOCAL hdc&
        
            hDC& = GetDC(%HWND_DESKTOP)
            hMemDC&= CreateCompatibleDC(hdc&)
            hMemBmp&= CreateCompatibleBitmap(hdc&,550,450)
        
            DIALOG NEW %HWND_DESKTOP, "ReSize Me!!",50,50,210,130, _
                                      %WS_overlappedwindow,TO hDlg&
            DIALOG SHOW MODELESS hDlg&, CALL SrcProc&()
        
            DIALOG NEW 0, "Capture Bmp of Dialog",300,50, 250, 250, %ws_thickframe OR %ws_sysmenu , TO hSPad&
            CONTROL ADD BUTTON, hSPad&, 100, "Capture", 10, 5, 40, 14, _
                      %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP,
        
                        CONTROL ADD BUTTON, hSPad&, 200, "Show File", 55, 5, 50, 14, _
                      %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP,
        
            DIALOG SHOW MODELESS hSPad&, CALL CaptureProc&()
        
          WHILE GetMessage(Msg, %NULL, 0, 0)    '----------- start message loop ----------
            TranslateMessage Msg
            DispatchMessage Msg
          WEND                  '-----------  end message loop  ----------
        END FUNCTION ' (PBMAIN)

        Comment


        • #5
          hmmm, Norm

          can't remember how to handle 256k images, got a bunch of chaos going on right now will look tonight

          Comment


          • #6
            hmm, dup
            Last edited by Brad D Byrne; 4 Jan 2008, 08:57 PM.

            Comment


            • #7
              Save Bitmap

              The complexity of your response escapes me. Here is the test code I was having trouble with before but which is now OK but for the 256 color issue.


              SUB Graphics ALIAS "Graphics" EXPORT
              DIM BMPHandle AS DWORD

              GRAPHIC BITMAP LOAD CADRG_Path & "\Composite.bmp",1280,1280 TO BMPHandle
              GRAPHIC ATTACH BMPHandle,0
              GRAPHIC WIDTH 8
              GRAPHIC ELLIPSE (300,300)-(900,900),%BLUE,,6
              GRAPHIC SAVE CADRG_Path & "\Composite.bmp"
              GRAPHIC BITMAP END
              END SUB

              The image Composite.bmp is a North-Up topographic map image which is
              to be rotated to another heading up by an existing routine. This rotation routine demands that the image Composite.bmp is a 256 color picture. When I use the powerful GRAPHIC commands from PowerBasic to superimpose other data over the topographic image prior to rotation, the GRAPHIC BITMAP END statement saves the result in 24 Bit color, which the rotational routine cannot handle. Is there another means to save the composite picture with the original 256 color LUT intact?

              Comment


              • #8
                From the help file:
                The selected graphic target (a graphic bitmap, control, or window, etc.) is saved to a disk file using the filename specified by BmpName$. The bitmap is always saved in a single plane, 24-bit format, to allow for the maximum (true color) resolution.
                Roy Cline

                Comment


                • #9
                  You can try, copy the bitmap to a string variable and saving the string variable.

                  Purpose
                  Retrieve a copy of a bitmap , storing it as a device-independent bitmap in a dynamic string variable.

                  Syntax
                  GRAPHIC GET BITS TO bitvar$

                  Remarks
                  This statement retrieves a copy of the entire bitmap for the selected graphic target, assigning it to the dynamic string variable specified by bitvar$.


                  Check the string to see if still 24 bit.

                  If the string is 256 like you want write it to the file using normal statments.

                  Very nice job Brad.

                  Norm, Brad is showing you how to do what you want using the API's.
                  I would suggest if you are going to do more programming that you learn
                  to use the API's. Sometimes that all there is to do a job.
                  Last edited by Roy Cline; 5 Jan 2008, 02:45 PM.
                  Roy Cline

                  Comment


                  • #10
                    Originally posted by Roy Cline View Post
                    You can try, copy the bitmap to a string variable and saving the string variable.

                    Purpose
                    Retrieve a copy of a bitmap , storing it as a device-independent bitmap in a dynamic string variable.

                    Syntax
                    GRAPHIC GET BITS TO bitvar$

                    Remarks
                    This statement retrieves a copy of the entire bitmap for the selected graphic target, assigning it to the dynamic string variable specified by bitvar$.


                    Check the string to see if still 24 bit.

                    If the string is 256 like you want write it to the file using normal statments.

                    Very nice job Brad.

                    Norm, Brad is showing you how to do what you want using the API's.
                    I would suggest if you are going to do more programming that you learn
                    to use the API's. Sometimes that all there is to do a job.
                    Thanks Roy, for answering my question. I have used the API for garphics on many occasions, but usually as the last resort because of the complexity. In this case, I was interested in the apparently more powerful graphic commands associated with PB8 to make life a little easier.

                    Your suggestion worked like a champ and the BMP was still in its original 8 bit color format after the superposition of the ellipse over the map image. As a result, I do not have to rewrite the rotational routine. Many thanks to all for the assistance, Norm

                    Comment


                    • #11
                      Hi Norm,

                      sorry that code didn't really fit your question/s, I have learned a lot from these forums, often far later than when then post were originated, so sometimes when I see a thread that I know others will later search for (such as " Simple Graphic Example"), I feel it helpful to post some of things that I once stumbled on

                      Comment


                      • #12
                        I think I may need new glasses. Brad, I was just scanning your third example, upper left corner, I could have sworn the title bar said "ReSize Mel !" and I'm going "Excuse me??????".
                        There are no atheists in a fox hole or the morning of a math test.
                        If my flag offends you, I'll help you pack.

                        Comment


                        • #13
                          Hmm, Mel it says Resize Me, so someone can resize the dialog and see that the capture is working correctly, I once had compiled about 50 or 60 simple demos like those that I was going to put on a site but never got around to it, but maybe in a few months I'll gather them up

                          Comment


                          • #14
                            Originally posted by Brad D Byrne View Post
                            ...it says Resize Me...
                            Well, yea. I kinda figured that out after a few seconds. It was just the initial shock of just glossing over it that kinda rattled my cage.
                            There are no atheists in a fox hole or the morning of a math test.
                            If my flag offends you, I'll help you pack.

                            Comment

                            Working...
                            X