filedel after use

derez

Expert
Licensed User
Longtime User
Hi
How can I delete a file after using it by drawimage ?
The following example does not run because test1.jpg is in use, even though the form is already drawn with test2.jpg :confused:

B4X:
Sub App_Start
   Form1.Show
   form1.DrawImage("test1.jpg",0,0)
   form1.DrawImage("test2.jpg",0,0)
   FileDel("test1.jpg")
End Sub
I'm sure it must be a simple answer but I can't find it...
Thanks
 

Cableguy

Expert
Licensed User
Longtime User
Hi

I'm sure it must be a simple answer but I can't find it...
Thanks

Yes...simple...you can't...
Any image file used by a b4p app stays locked by b4p, even after fileclose...
This behaviour only happens with image files...
ie:
set an image control file in designtime and run your app...
from that firstrun, the image file will no longer be available to be overwritten, unless you stop the b4p ide...
This is one of the most awaited features to be yet acomplished...the image files unlocking...
 

derez

Expert
Licensed User
Longtime User
Thank you Paulo.

Lets see if you can help me with a workaround to this.
The application in mind is sending the form image to another device using network, in a continuous way.

It works using saveimage from imagelibEx.dll and then sending the file, and in the recieving device by form.drawimage. But to save the file that is recieved I need to delete the file that was recievd before, and it is in use !!!
alternatively I can save the recieved file in a different name each time but then the memory will explode from all these files that stay locked :(

If there a way to recieve the stream and create an image directly, not by using a file in the middle, then the problem is solved.

Agraham
In ImagelibEx there is SaveImageToStream but there is no SaveStreamToImage ! can you add it ?

Any suggestion ?
 
Last edited:

agraham

Expert
Licensed User
Longtime User
there is no SaveStreamToImage ! can you add it ?
I could but I think you will run into the same locking problem with a stream as with a file. This locking of the file or stream is a .NET Bitmap object "feature" not a Basic4ppc bug. The lock lasts for the life of the Bitmap. If you Dispose the Bitmap and have no other references to it, that is it is not assigned to an Image or Form, then the file should become free again. You can then recreate the Bitmap with AddObject.

There is a bug in Basic4ppc that means you should not call New more than once once on any library object that uses native resources, like a Bitmap or BitmapEx. See this thread http://www.b4x.com/forum/questions-help-needed/3655-image-zoom-memory-problem.html
 

derez

Expert
Licensed User
Longtime User
Agraham
your suggestion will do if you tell me how to dispose the bitmap which is displayed on the form, since I want to display a new one and even get rid of the image file.
 

derez

Expert
Licensed User
Longtime User
[If you look at the first post you can see that even if I draw another bitmap I can't delete the first image file, it is locked until the program ends.
And again - how do you dispose a bitmap (other than drawing over it) ?
The following code has the same problem:

B4X:
Sub App_Start
   Form1.Show
   imagelist1.Add("test1.jpg")
   form1.image = imagelist1.Item(0)
   imagelist1.Add("test2.jpg")
   form1.image = imagelist1.Item(1)
             imagelist1.RemoveAt(0)
   FileDel("test1.jpg")
End Sub
 
Last edited:

derez

Expert
Licensed User
Longtime User
Thank you Erel, I think that this is the solution :)
with the code below, if an imagefile named test[n].jpg is created by another process, the sub will display it on the form and delete the file.

B4X:
Sub Globals
n = 0
End Sub

Sub App_Start
form1.Show
End Sub

Sub Button1_Click
n = n+1
AddObject("bitmap1","Bitmap")
bitmap1.New1(AppPath & "\test" & n & ".jpg")
image1.Image = bitmap1.Value
form1.Image = image1.Image
bitmap1.Dispose
FileDel("test" & n & ".jpg")
End Sub
 
Last edited:
Top