Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / February 2006

Tip: Looking for answers? Try searching our database.

Cannot delete file It is in use by another person or program

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
steve - 24 Feb 2006 05:29 GMT
Hi All

I have a VB2005 program which stores photos of members as bitmap files

On a form a picture box (picCapture) loads the picture from file

When a new photo is required the user clicks on a button and my code
captures a new photo and saves it

50% of the time the code works fine, the other 50% I get a GDI+ error....

'Cannot delete file It is in use by another person or program'

I need a more reliable means of taking and saving the bitmap file

Any ideas please

Regards
Steve

Code..................................
Dim grabpicture As Bitmap, path As String = ""

Clipboard.Clear()
' Copy image to clipboard
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
grabpicture = New Bitmap(My.Computer.Clipboard.GetImage())
If submembername = "" Then
   path = photopath & Me.Tag & ".bmp"
Else
   path = photopath & Me.Tag & submembername & ".bmp"
End If

If Not IsNothing(picCapture.Image) Then
   picCapture.Image.Dispose()
   picCapture.Image = Nothing
   My.Application.DoEvents()
End If

grabpicture.Save(path)
picCapture.Image = grabpicture
ClosePreviewWindow()
"Jeffrey Tan[MSFT]" - 24 Feb 2006 08:37 GMT
Hi steve,

Thanks for your post.

Which method do you get this exception? Can you show me the full call stack
regarding this exception? Then we can identify the exception in a more
detailed granularity.

I look forward to hearing from you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

steve - 24 Feb 2006 11:58 GMT
Hi Jeff

Here is the code segment where the error often occurs (but not
always).........................

Clipboard.Clear()
' Copy image to clipboard
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
' Get image from clipboard and convert it to a bitmap
If My.Computer.Clipboard.ContainsImage() Then
If Not IsNothing(picCapture.Image) Then
picCapture.Image.Dispose()
picCapture.Image = Nothing
My.Application.DoEvents()
End If
If flgsubmember Then
If Not IsNothing(frmgridedit.image1.Image) Then
frmgridedit.image1.Image.Dispose()
frmgridedit.image1.Image = Nothing
My.Application.DoEvents()
End If
path = photopath & Me.Tag & submembername & ".bmp"
If My.Computer.FileSystem.FileExists(path) Then
Try
My.Computer.FileSystem.DeleteFile(path) ******** Exception here
Catch ex As IOException
MsgBox(ex.Message)
End Try
End If

Call stack...............................

 mscorlib.dll!System.IO.__Error.WinIOError(int errorCode, string
maybeFullPath = "C:\\vbnet 2005 progs\\Gym
Master\\bin\\Photos\\4352Luke.bmp") + 0x47e bytes
 mscorlib.dll!System.IO.File.Delete(string path) + 0x9e bytes
 Microsoft.VisualBasic.dll!Microsoft.VisualBasic.FileIO.FileSystem.DeleteFileInternal(string
file, Microsoft.VisualBasic.FileIO.FileSystem.UIOptionInternal showUI,
Microsoft.VisualBasic.FileIO.RecycleOption recycle,
Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel) + 0xb2 bytes
 Microsoft.VisualBasic.dll!Microsoft.VisualBasic.MyServices.FileSystemProxy.DeleteFile(string
file) + 0x10 bytes
> Gym Master.exe!Gym_Master.frmphoto.btntakephoto_Click(Object sender =
> {Text = "Take Photo"}, System.EventArgs e =
> {System.Windows.Forms.MouseEventArgs}) Line 260 + 0x1e bytes Basic
 System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs
e) + 0x57 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs
e) + 0x49 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs
mevent = {X = 38 Y = 12 Button = Left}) + 0xc3 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref
System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button,
int clicks) + 0xf2 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x544 bytes
 System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref
System.Windows.Forms.Message m) + 0xce bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref
System.Windows.Forms.Message m) + 0x2b bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref
System.Windows.Forms.Message m) + 0xd bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref
System.Windows.Forms.Message m) + 0xd6 bytes
 System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr
hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x75
bytes
 [Native to Managed Transition]
 [Managed to Native Transition]
 System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int
dwComponentID, int reason = 4, int pvLoopData = 0) + 0x2ea bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int
reason = 4, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.Application.ModalApplicationContext}) + 0x17d bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x53 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window
owner) + 0x4ee bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x7
bytes
 Gym Master.exe!Gym_Master.frmgridedit.Button7_Click(Object sender = {Text
= "Take Photo"}, System.EventArgs e = {System.Windows.Forms.MouseEventArgs})
Line 606 + 0xa bytes Basic
 System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs
e) + 0x57 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs
e) + 0x49 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs
mevent = {X = 26 Y = 12 Button = Left}) + 0xc3 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref
System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button,
int clicks) + 0xf2 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x544 bytes
 System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref
System.Windows.Forms.Message m) + 0xce bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref
System.Windows.Forms.Message m) + 0x2b bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref
System.Windows.Forms.Message m) + 0xd bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref
System.Windows.Forms.Message m) + 0xd6 bytes
 System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr
hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x75
bytes
 [Native to Managed Transition]
 [Managed to Native Transition]
 System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int
dwComponentID, int reason = 4, int pvLoopData = 0) + 0x2ea bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int
reason = 4, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.Application.ModalApplicationContext}) + 0x17d bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x53 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window
owner) + 0x4ee bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x7
bytes
 Gym Master.exe!Gym_Master.frmmembers.Button22_Click(Object sender = {Text
= "Edit"}, System.EventArgs e = {System.Windows.Forms.MouseEventArgs}) Line
7457 + 0x9 bytes Basic
 System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs
e) + 0x57 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs
e) + 0x49 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs
mevent = {X = 18 Y = 5 Button = Left}) + 0xc3 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref
System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button,
int clicks) + 0xf2 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x544 bytes
 System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref
System.Windows.Forms.Message m) + 0xce bytes
 System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref
System.Windows.Forms.Message m) + 0x2b bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref
System.Windows.Forms.Message m) + 0xd bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref
System.Windows.Forms.Message m) + 0xd6 bytes
 System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr
hWnd, int msg = 514, System.IntPtr wparam, System.IntPtr lparam) + 0x75
bytes
 [Native to Managed Transition]
 [Managed to Native Transition]
 System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int
dwComponentID, int reason = -1, int pvLoopData = 0) + 0x2ea bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int
reason = -1, System.Windows.Forms.ApplicationContext context =
{Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.WinFormsAppContext})
+ 0x17d bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x53 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext
context) + 0x15 bytes
 Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
+ 0xc0 bytes
 Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
+ 0xe4 bytes
 Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[]
commandLine) + 0x62 bytes
 [Native to Managed Transition]
 [Managed to Native Transition]
 mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,
System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x32
bytes
 Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
+ 0x2b bytes
 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
state) + 0x3b bytes
 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
executionContext, System.Threading.ContextCallback callback, object state) +
0x81 bytes
 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x40 bytes

Regards
Steve

> Hi steve,
>
[quoted text clipped - 12 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
Jim Hughes - 24 Feb 2006 14:09 GMT
I found that using the Image.FromFile method left a file lock in place that
prevented manipulating the file.

What I did was load the image into a FileStream and then set the Image from
the stream.

Public Function ImageFromPath(ByVal ImagePath As String) as Image
 Dim fs As System.IO.FileStream
 Dim img as Image = Nothing
 Try
  fs = System.IO.File.OpenRead(ImagePath)
  fs.Position = 0
  img = System.Drawing.Image.FromStream(fs)
 Catch ex As Exception
  DisplayError(ex)
 Finally
  If Not fs Is Nothing Then
   fs.Close()
  End If
 End Try
 Return img
End Function

> Hi Jeff
>
[quoted text clipped - 228 lines]
>> This posting is provided "as is" with no warranties and confers no
>> rights.
steve - 24 Feb 2006 21:40 GMT
Jim

Thanks for the info

Worked a treat

Regards
Steve

>I found that using the Image.FromFile method left a file lock in place that
>prevented manipulating the file.
[quoted text clipped - 251 lines]
>>> This posting is provided "as is" with no warranties and confers no
>>> rights.
steve - 24 Feb 2006 21:42 GMT
Hi Jeff

Jim Hughes has supplied a solution which works

Thanks for your efforts so far

Regards
Steve

> Hi steve,
>
[quoted text clipped - 12 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 28 Feb 2006 02:27 GMT
Hi steve,

I was out of office yesterday. I am glad "Jim Hughes"'s reply resolves your
problem and thank "Jim Hughes" for sharing with the community.

If you need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.