GDI+ DrawImage not working for "redraw"

@Mike_Duglas Thanks so much for making your GDI+ classes public. Do you have a “donate” button anywhere? I’d contribute.

I’m playing with the UsingImages example. When I resize the window smaller than my image and then resize it back larger, the image is not re-drawing. WM_Paint is firing and ScaleCropImage is being called. There are no errors reported by the img.FromFile method call or the g.DrawImage method call, but the image is not displayed.
Any pointer you can give me?
Thanks.

Hi Rick,

No idea, I am unable to reproduce the issue on my machine. My UsingImages.exe was built in C11.1.
Did you build the exe “as is”? What is Clarion version you’re using?

Regarding donates: there are 2 items on Clarionshop: “Easy Donation 50” and “Easy Donation 100”.

Hi Mike,

You are correct the original is working fine.
I was trying to modify the example to put the image in the center of the window.
Of course, that means the image position changes when the window is resized.
The first paint is fine, but it isn’t redrawing the image in the new position. I’ve checked the coordinates on the subsequent paint calls and they look OK.

Here’s the changed code. There is also code to new/dispose the new ClientRect property.

TSimpleWindow.OnPaint         PROCEDURE()
dc                              TPaintDC
  CODE
  dc.GetDC(SELF.hwnd)
  self.GetClientRect(self.ClientRect)

  ScaleCropImage(dc.GetHandle(), self)
!  MappedToParallelogram(dc.GetHandle())
!  ColorProfile(dc.GetHandle())
!  CachedBitmap(dc.GetHandle())
  
ScaleCropImage                PROCEDURE(HDC phdc, *TSimpleWindow this)
g                               TGdiPlusGraphics
img                             TGdiPlusImage
w                               UNSIGNED, AUTO
h                               UNSIGNED, AUTO
srcRect                         LIKE(GpRectF)
destRect                        LIKE(GpRectF)
curX                            UNSIGNED, auto
curY                            UNSIGNED, auto

  CODE
  !The following example constructs an Image object from the file apple-dreaming.jpg. 
  !The code draws the entire apple image in its original size. The code then calls the DrawImage method of a Graphics object to draw a portion of the apple image 
  !in a destination rectangle that is larger than the original apple image.
  g.FromHDC(phdc)
  img.FromFile('apple-dreaming.jpg')
  w = img.GetWidth()
  h = img.GetHeight()
  curX = (this.ClientRect.Width()-w)/2
  curY = (this.ClientRect.Height()-h)/2
!  GNT:Debuger.Message('ScaleCropImage ClientHeight' & curH & ', Width: ' & curW & ', curx: ' & curX & ', cury: ' & curY & ', ImageWidth: ' & w & ', ImageHeight: ' & h)
  
  !Draw the image unaltered with its upper-left corner at (0, 0).
!  if curX > 0 and curY > 0
    GNT:Debuger.Message('Drawing')
  g.DrawImage(img, curX, curY)
  
!  end

!  g.DrawImage(img, 50, 50)
  
    
  return

Donation sent! :slight_smile:

Rick,

In this case you should process WM_WINDOWPOSCHANGED message.
I modified UsingImages a bit to show the idea.
CenterImage.clw (3,1 КБ)

Thanks, Mike.

Can you explain to me why the DrawImage call in my example didn’t paint anything?

For paint device context (TPaintDC), Windows itself decides whether to repaint the window or not. You can use a regular device context (TDC) instead, in this case the window will be redrawn unconditionally.

!dc                              TPaintDC
dc                              TDC

Thanks, Mike. Great info.