Today I will show you how to correctly dispose of an image, freeing it from your program and "unlocking" it to be deleted/renamed/edited/copied.
Add this above "Public Class formname"
Add this into a classCode:Imports System Imports System**** Imports System. Runtime.InteropServices Imports System.Drawing Imports System.Drawing.Imaging
Then just use this as your image capturing. It will capture the entire screen and save to "C:\image.png".Code:Imports System Imports System. Runtime.InteropServices Imports System.Drawing Imports System.Drawing.Imaging Namespace ScreenShot Public Class ScreenCapture Public Function CaptureScreen() As Image ' Capture The Screen! 'This next will return an image 'using your desktop screen capture Return CaptureIt(User32.GetDesktopWindow()) End Function Public Function CaptureIt(ByVal handle As IntPtr) As Image Dim SRCCOPY As Integer = &HCC0020 Dim hdcSrc As IntPtr = User32.GetWindowDC(handle) Dim windowRect As New User32.RECT User32.GetWindowRect(handle, windowRect) Dim width As Integer = windowRect.right - windowRect.left Dim height As Integer = windowRect.bottom - windowRect.top Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc) Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height) Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap) GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY) GDI32.SelectObject(hdcDest, hOld) GDI32.DeleteDC(hdcDest) User32.ReleaseDC(handle, hdcSrc) Dim img As Image = Image.FromHbitmap(hBitmap) GDI32.DeleteObject(hBitmap) Return img End Function Public Sub CaptureScreenToFile(ByVal pathPNG As String) Dim image As Image = CaptureScreen() Dim imagecopy As New Bitmap(image) image.Dispose() imagecopy.Save(pathPNG, Imaging.ImageFormat.Png) End Sub Private Class GDI32 Public SRCCOPY As Integer = &HCC0020 Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32 Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal nWidth As Int32, ByVal nHeight As Int32) As IntPtr Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As IntPtr Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Int32 Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Int32 Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr End Class Public Class User32 <StructLayout(LayoutKind.Sequential)> Public Structure RECT Public left As Integer Public top As Integer Public right As Integer Public bottom As Integer End Structure Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr Declare Function GetWindowDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32 Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Int32 End Class End Class End Namespace
Then, if you want to delete the file later:Code:Dim SCRCapture as new ScreenCapture SCRCapture.CaptureScreenToFile("C:\image.png")
If you wanted to delete the file but usedCode:File.Delete("C:\image.png")
it would throw an exception eventually whether after one click or two clicks of the "Delete" button (or whatever sub you have the File.Delete under).Code:Dim originalimage As Bitmap = CaptureScreen() originalimage.Save("C:\image.png", Imaging.ImageFormat.Png)
Press thanks if this helped you at all.
@³²³
Credits for GDI32:
http://www.codeproject.com/KB/GDI-plus/BitBlt.aspx
Credits for original C# code that I cleaned up:
http://www.developerfusion.co.uk/show/4630/







endeavor Game
Epic War 4 Game
Crystal Story Game
Haunt the House Game
Colour My Fate Game
LARRY: Pup Run Game
Demolition City Game
Sushi Cat Game
Cursed Treasure Game
Manhattan Project Game
Glad you like it. 




