B4J Question Resetting a bitmap

dieterp

Active Member
Licensed User
Longtime User
I'm struggling to reset a bitmap linked to a canvas once I've drawn on it. I open a form from another form and set the bitmap using the following code in the Show part of the form:
B4X:
bmpImage = xui.LoadBitmapResize(File.DirAssets, "myImage.png", pnllWidth, pnlHeight, False)
I then draw a line on the image and close the form. When I reopen that form later the image is still there with the line drawn on it. When I check the IsInitialized boolean I see the bitmap is still Initialized. How do I reset that bitmap so that it will always load the screen with a fresh image?
 

dieterp

Active Member
Licensed User
Longtime User
I am uploading a small test project to demo the issue I am having. Here are the steps to recreate:

1) Click on the "Open Form" button
2) Drag the mouse along the green canvas area. It will draw a white line that will follow your mouse pointer
3) Close the form
4) Click on the "Open Form" button again

You will now see that the original white line that was drawn in step 2 above is still visible. How do I sort it so that the screen will open with only the green canvas displayed, so that I can redraw the white line?
 

Attachments

  • TestCanvas.zip
    31.1 KB · Views: 206
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You should only create the canvas once. This means that you need to call Create only when frm.IsInitialized if false.

2. Clear the canvas each time:
B4X:
Public Sub Show
   
   If frm.IsInitialized = False Then
     frm.Initialize("frm", 400, 600)
     frm.RootPane.LoadLayout("1")
     Create
   End If
   Clear
   cnvImage.Invalidate
   frm.ShowAndWait
End Sub

Sub Create
   bmpImage = xui.LoadBitmapResize(File.DirAssets, "greenImage.png", 400, 600, False)
   DestRect.Initialize(0, 0, 400, 600)
   cnvImage.Initialize(Pane1)
   cnvImage.DrawBitmapRotated(bmpImage, DestRect, 0)
End Sub

Sub Clear
   cnvImage.ClearRect(cnvImage.TargetRect)
   cnvImage.DrawBitmapRotated(bmpImage, DestRect, 0)
End Sub

Sub Pane1_MouseDragged (EventData As MouseEvent)
   Clear
   cnvImage.DrawLine(0, 0, EventData.X, EventData.Y, xui.Color_White, 2)
   cnvImage.Invalidate
End Sub
 
Upvote 0
Top