Image Locking

Cableguy

Expert
Licensed User
Longtime User
Hi, is there any workaround image locking after being setted to an image control?

I'm developing a database based app, with embeded images as blobs, but to assure the correct image is being selected, i first show it in an image control, and after the user clicks a button I add the image to the database...
If i do not preview the image in the image control all works fine, but if I do preview it, the I cannot add the image to the database, as it is locked...
 

digitaldon37

Active Member
Licensed User
Longtime User
work around

Maybe you could copy the image to a temp file before loading it in preview, thus always having the original available
 

Cableguy

Expert
Licensed User
Longtime User
I cannot copy it from within the code as that would, again, lock the file...

@Andrew: I've been searching for a .net solution, and I found some mentions to FileStream.closestream...
Since you are a far better dll maker than me, and have a much greater understnading of how things work in b4ppc and .net, would it be possible to create a "unlock" dll?
 

klaus

Expert
Licensed User
Longtime User

francisco cobos

Member
Licensed User
Longtime User
Hi, with this code I get the error: "A generic error ocurred in GDI+ continue?"
when I try to save a image that is used in the form control. If I try to continue, not always appears the same error (sometimes yes, sometimes no...)

I think this problem has something to do whith the image locked, is there any solution?

The code:

Sub Globals
Dim Devices(0)
End Sub

Sub App_Start
Form1.Show
AddButton("form1","bSaveimage",2,230,90,20,"Save Image")
AddButton("form1","Button3",100,230,90,20,"To Form")
AddImage("form1","image1",5,5,100,100)

WebCam.New1
Devices() = WebCam.GetAllDevices
WebCam.NewCaptureWindow(0, "image1",1,1,image1.Width,image1.Height)
Webcam.PreviewOn(10)

End Sub


Sub bSaveImage_Click
WebCam.SaveImage(AppPath & "\capture.jpg","J")
End Sub


Sub Button3_Click
bitmap1.New1 (AppPath & "\capture.jpg")
Form1.Image = bitmap1.Value
End Sub

Sub Form1_MouseDown (x,y)
form1.circle(x,y,5,cRed,f)
End Sub
 

agraham

Expert
Licensed User
Longtime User
From the .NET docs for Image - "Saving the image to the same file it was constructed from is not allowed and throws an exception." Certainly a Bitmap seems to hold open the file it was created from as you can't delete or rename it in Explorer while the Bitmap exists. I have no idea why this limitation that a Bitmap needs to hold the file open exists but it does seem known/intended as the problem/limitation is documented :confused:

I have tried using the Door library to clone the underlying Bitmap and assign the clone to Form1.Image and dispose of Bitmap1 but the original file is still held open! The link to the file seems to follow even a copy of Bitmap1.

This is the only way that I have found to free that link.
B4X:
Sub Button3_Click
  bitmap1.New1(AppPath & "\capture.jpg")
  Rect1.New1(0, 0, Bitmap1.Width, Bitmap1.Height)
  Draw.New1("Form1", False)
  Draw.DrawImage(Bitmap1.Value, Rect1.Value, Rect1.Value, False)
  Form1.Refresh
  Bitmap1.Dispose
End Sub
Note the Bitmap1.Dispose. Even New1ing Bitmap1 to another file or New2ing it to a new size or New3ing it to an image from a different bitmap from a different file or assigning Bitmap1.Value with a different bitmap doesn't break the link between Bitmap1 and the original file. I don't understand this, it is counter-intuitive and I can't think of a reason why this behaviour seems to be designed in to the Bitmap object at a very low level. I'm probably missing something ! :confused:
 

francisco cobos

Member
Licensed User
Longtime User
Thanks Agraham, I have modified the code in order to be able of use more than once the button3, and works fine! (If not, you get an error when you try to use the bitmap1 object after the dispose sentence)

Sub Globals
Dim Devices(0)
in1=2
End Sub

Sub Button3_Click
AddObject("Bitmap" & in1,"Bitmap")
Control("bitmap" & in1 ,"Bitmap").New1(AppPath & "\capture.jpg")
Rect1.New1(0, 0, Control("bitmap" & in1 ,"Bitmap").Width, Control("bitmap" & in1 ,"Bitmap").Height)
drawer.New1("Form1", False)
Drawer.DrawImage1(Control("bitmap" & in1 ,"Bitmap").Value, Rect1.Value, Rect1.Value,False)
Form1.Refresh
Control("bitmap" & in1 ,"Bitmap").Dispose
End Sub
 
Top