Draw portion of image in cgraphctx

Started by docroger, August 14, 2025, 10:08:24 AM

Previous topic - Next topic

docroger

Hello José...
I need help... confusing in image, bitmap... GDI, GDI+....

I need to draw a portion of image loaded in resource.

I use a cgraphctx with :
DIM pGraphCtx AS CGraphCtx = CGraphCtx(hDlg, IDC_GRCTX, "", 10, 10, 200, 100)

then :
pgraphctx.loadimagefromres(hinstance,"#102",1,false,0)

All is OK : full image is draw.

But i want a portion of this image (a rect)...

Thanx for any help.

José Roca

This does it using GDI+ and loading an image from file. To use a resource is more complicated, having to find and load the resource, create an stream and a bitmap from the stream. Also depends of the kind of image that you're using (code not shown).


docroger

Hello José,
Thanx for reply... Alas not what i need...
I work with images in resource (bitmap).

Drawing on afx is too much for me.
On powerbasic there are many simple fonctions.
I need an DDT equivalent for graph...

I think i return on purebasic for this project.

The code in powerbasic is :

#RESOURCE BITMAP 100, "Digits.bmp"
...
 GRAPHIC BITMAP LOAD "#100", 210, 20 TO hBmp 

....
IF CB.WPARAM = %ID_TIMER1 THEN                ' Make sure it's corrent timer id
          GRAPHIC ATTACH CB.HNDL, %IDC_GRAPHIC1, REDRAW
        '  GRAPHIC CLEAR %BLACK
          sTime = TIME$                              ' grab time string (00:00:00)
          FOR c = 1 TO LEN(sTime)                    ' parse through it
              ac = ASC(sTime, c)                    ' look at ASCII code for each character
              SELECT CASE AS LONG ac                ' copy bitmap parts via lookup "table"
              CASE 48 TO 57                          ' digits 0 to 9
                  ac = ac - 48                      ' make the value zero-based and copy bmp part
                  GRAPHIC COPY hBmp, 0, (ac * 15, 0)-(ac * 15 + 15, 20) TO ((c - 1) * 15, 0)
              CASE 58                                ' colon is the 13th image in the Bmp strip
                  GRAPHIC COPY hBmp, 0, (12 * 15, 0)-(12 * 15 + 15, 20) TO ((c - 1) * 15, 0)
              END SELECT
          NEXT
          GRAPHIC REDRAW                            ' Now we can redraw it all
          '
          DrawClock CB.HNDL, %IDC_GRAPHIC2, TIME$

          '
      END IF   


José Roca

Well, at least it has inspired me to add a new procedure that allows to load an image from a resource file and return it as a stream that can be used easily with a new constructor. The images can be any of the formats supported by GDI+, including alpha-blending for transparency, not just old bitmaps.

' ========================================================================================
' The following example draws part of an image.
' ========================================================================================
SUB Example_DrawImage (BYVAL hdc AS HDC)

   ' // Create a graphics object from the window device context
   DIM graphics AS CGpGraphics = hdc
   ' // Get the DPI scaling ratios
   DIM rxRatio AS SINGLE = graphics.GetDpiX / 96
   DIM ryRatio AS SINGLE = graphics.GetDpiY / 96
   ' // Set scaling
   graphics.SetPageUnit(UnitPixel)
   graphics.SetPageScale(rxRatio)

   ' // Create an Image object.
'   DIM pImage AS CGpImage = "climber.jpg"   ' // load from file
   DIM pImage AS CGpImage = CGpImage(GetModuleHandle(NULL), "#998")   ' // load from resource by ordinal
'   DIM pImage AS CGpImage = CGpImage(GetModuleHandle(NULL), "IDI_CLIMBER")   ' // load from resource by name

   ' // Draw the original source image.
   graphics.DrawImage(@pImage, 10, 10)

   ' // Part of the source image to draw
   DIM rcsrc AS GpRectF = TYPE<GpRectF>(80, 30, 80, 80)
   ' // Destination recangle
   DIM rcdest AS GpRectF = TYPE<GpRectF>(200, 10, 80, 80)

   ' // Draw the scaled image.
   graphics.DrawImage(@pImage, @rcdest, @rcsrc, UnitPixel)

END SUB
' ========================================================================================

docroger

Hi José,

Nice !

Very cool, i can continue my translation of my powerbasic app...

Thank you !