Deleting an Image from my desktop using Visual Basic - vb.net

I'm trying to do the following activity in visual basic:
Take a screenshot of my desktop and hold it in picture box.
Save it on my folder
Send a mail to someone with the image attached
Rename the image and Delete it from the folder
This is a recurring activity for every one hour. So far I have coded for all the steps above but when the script tries to rename the file on the folder I get an error message.
Error code
The code goes something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim theTime As DateTime
theTime = Now.ToLongTimeString
Dim regDate As Date = DateTime.Now()
strDate = regDate.ToString("ddMMMyyyy HH:mm:ss")
'MsgBox(theTime)
'currentfolder = "C:\Users\user\Desktop" + strDate
'My.Computer.FileSystem.CreateDirectory(currentfolder)
If theTime >= #8:00:00 AM# Then
Timer1.Interval = 100
Timer1.Start()
'Timer starts functioning
End If
End Sub
Function Takescreenshot()
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
Dim FileToDelete As String
FileToDelete = "C:\Users\user\Desktop\1.png"
If System.IO.File.Exists(FileToDelete) = True Then
PictureBox1.Image = Nothing
My.Computer.FileSystem.RenameFile("C:\Users\user\Desktop\1.png", "2.png")
System.IO.File.Delete("C:\Users\user\Desktop\2.png")
End If
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot
SaveImage("C:\Users\user\Desktop\1.png", PictureBox1.Image)
'Send Email
Second = 0
Timer1.Start()
End Function
Public Sub SaveImage(filename As String, image As Image)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".png")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub
I need to delete the current image and then save a fresh copy of it. Please help.

Ok I found out a solution for this problem. I actually found out what was the process that was really holding the script from deleting the image.
It was the mail function which I have not added for security reasons. I had to dispose the image that was being added to the email body after the work was done so
img1.Dispose()
solved the issue

Related

How do I take a screenshot of my project file, and then export/convert said file into a PDF, using vb.net?

I am trying to take the contents of my project file and turning/export it as a PDF. For context, here is the layout of my project file:
Don't mind the numbers, I was just testing that part of the code, my only issue is finding a way in taking that project file and turning it into a PDF. I tried breaking down this task into two buttons, the 2) Save File and 3) Create PDF buttons, though now I'm just getting more confused the more I try to do this.
Here is my code for the "2) Save File" button:
Private Sub Screenshot_Click(sender As Object, e As EventArgs) Handles Screenshot.Click
Dim bmp As New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height))
PictureBox1.Image = bmp
bmp.Save(My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & ".png", Imaging.ImageFormat.Png)
SaveFileDialog1.ShowDialog()
SaveFileDialog1.Title = "Save file"
SaveFileDialog1.InitialDirectory = "E:\French"
Dim location As String
location = SaveFileDialog1.FileName
bmp.Save(SaveFileDialog1.ShowDialog())
End Sub
The 3) Create PDF button doesn't currently have any special code, all I did was to try and create a bitmap and then taking that bitmap and turning it into a PDF:
Private Sub PDFbutton_Click(sender As Object, e As EventArgs) Handles PDFbutton.Click
Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
' Create a graphics object from the bitmap
Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
' Take a screenshot of the entire Form1
gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
' Save the screenshot
bmpScreenshot.Save("D:\Form1.jpg", ImageFormat.Jpeg)
End Sub
Just started working on vb.net, so I'm still new at this. I'll gladly take any help and/or advice!

Take a photo every 5 seconds

I am developing a panel, which from time to time runs a process and generates an image within it. Once the image is generated, you need to take a photo to save it for the changes that are made.
I already developed the part where the images change, but when I take the photos, they all come out blank. Add a delay on the screen thinking that it should take a while to take the photo of the panel later, but it still comes out blank and if there is something inside the panel, since I am resizing it according to the size of what is updated.
Can you help me to see where my error is? Or guide me to obtain an optimal result?, I also tried using a timer, but it gives me the same result, any ideas?
This is the code developed.
sub buldimages()
Panel1.Refresh()
System.Threading.Thread.Sleep(5000)
segondGa(varName)
'Timer1.Start()
End Sub
Function segondGa(nameLine As String)
Dim widthOldPuno = Panel1.Width
Dim heightOldPuno = Panel1.Height
Dim widthOldPdos = Panel2.Width
Dim heightOldPdos = Panel2.Height
Dim coordenada, i As Integer
For Each obj As Control In Panel1.Controls
coordenada = obj.Location.X
Next
Panel1.Width = widthOldPuno + (coordenada - Panel1.Width)
Panel2.Width = Panel1.Width + 113
Panel2.Height = heightOldPdos - 65
Dim nameLineB As String = nameLine
Using bmp = New Bitmap(Panel2.Width, Panel2.Height)
Panel2.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
Dim bmp2 As New Bitmap(bmp.Width * 3, bmp.Height * 3)
Dim gr As Graphics = Graphics.FromImage(bmp2)
gr.DrawImage(bmp, New Rectangle(0, 0, bmp2.Width, bmp2.Height))
bmp2.Save("C:\TEMP\" & nameLineB & ".png")
End Using
Panel2.Width = widthOldPdos
Panel2.Height = heightOldPdos
Panel1.Width = widthOldPuno
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim seconds As Integer
Label1.Text = seconds + 1
If Label1.Text = "5" Then
Timer1.Stop()
End If
End Sub

Saving the image of a picturebox

So I have this code:
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = PicOuterBorder.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
'this takes a screenshot
End Sub
PicOuterBorder is a picturebox on my form. PicFinal is another display picturebox. But this code gets me this: Which is basically a screenshot of a window in the size of PicOuterBorder starting from the origin of my screen. However, Me.Bounds instead of PicOuterBorder.Bounds works and gets a perefect screenshot of just my form. I want picFinal to have a screenshot of just PicOuterBorder
Try below code. You have to map the control coordinates to screen coordinates using PointToScreen. I have placed PicOuterBorder inside the panel PanelPicture. PanelPicture is without any border, while PicOuterBorder can have any type of border style. Below code takes the snapshot of the panel.
Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
Dim graph As Graphics = Nothing
Dim bounds As Rectangle = Nothing
Dim screenshot As System.Drawing.Bitmap
Dim location As Drawing.Point = PanelPicture.PointToScreen(Drawing.Point.Empty)
screenshot = New System.Drawing.Bitmap(PanelPicture.Width, PanelPicture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(location.X, location.Y, 0, 0, PanelPicture.Size, CopyPixelOperation.SourceCopy)
picFinal.Image = screenshot
graph.Dispose()
End Sub
Adapt your code for something like this:
Public Sub SaveImage(filename As String, image As Image, Encoder As ImageCodecInfo, EncParam As EncoderParameter)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub

Delete specific files from computer knowing the path

I'm using a timer to take screen captures after an amount of time and save the images to a specific path.
Private Sub tmrPS1_Tick(sender As Object, e As EventArgs) Handles tmrPS1.Tick
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot
PictureBox1.Image.Save("C:\ImagesFolder\1.jpg")
tmrPS1.Enabled = False
End Sub
And I want another timer to delete them after I sent them with mail because I will have to take new ones. My question is how do I delete the images knowing the path?
Delete/recreate the folder when you are done with it?
If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
Application.DoEvents()
IO.Directory.CreateDirectory(DestinationFolder)
This code cleans up files in "Temp" with the same extension as the file I'm saving.
With My.Computer.FileSystem
Dim s As String = Environ("temp")
For Each foundFile As String In .GetFiles(s, FileIO.SearchOption.SearchTopLevelOnly, "*.tmp.kml")
.DeleteFile(foundFile) ' clean up old output
Next
End With

Reading Image path from text file and add it to thumbnail control in vb.net

I have a CheckedListBox and a thumbnail control and I am trying to save and retrieve back the images along with their path.
I need to show their image path in the checked list box which I am able to do but I am unable to get the images.
I want to know how to read Images from the saved text file and display it in the thumbnail control.
Here is the code:
Private Sub LoadProject_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles LoadProject.Click
Using ofdlg As New Windows.Forms.OpenFileDialog
ofdlg.DefaultExt = "amk"
ofdlg.Filter = "AquaMark Project|*.amk"
ofdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim r As New IO.StreamReader("C:\Test\test.txt")
While (r.Peek() > -1)
CheckedListBox1.Items.Add((r.ReadLine), CheckState.Checked)
Dim Pname As String
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
Pname = CheckedListBox1.SelectedIndex.ToString
Next
End While
r.Close()
Dim SaveData As New gCanvasData
Using objStreamReader As New StreamReader(ofdlg.FileName)
Dim x As New XmlSerializer(GetType(gCanvasData))
SaveData = CType(x.Deserialize(objStreamReader), gCanvasData)
objStreamReader.Close()
End Using
With SaveData
frmDisplay.GCanvas1.ZoomFactor = 1
frmDisplay.GCanvas1.ImageXYReset()
frmDisplay.GCanvas1.Image = .Image
frmDisplay.GCanvas1.gAnnotates = .gAnnotates
frmDisplay.GCanvas1.RebuildAll()
frmDisplay.GCanvas1.AssembleBitmap()
End With
End If
End Using
End Sub
I have modified the code and placing the code here so that it might help others:
Dim sr1 As New IO.StreamReader("C:\Test\test.txt")
While (sr1.Peek() > -1)
CheckedListBox1.Items.Add((sr1.ReadLine), CheckState.Checked)
CheckedListBox1.SelectedIndex = 0
End While
sr1.Close()
Dim sr2 As New IO.StreamReader("C:\Test\test.txt")
While (sr2.Peek() > -1)
Thumbcontrol1.AddThumbnail(sr2.ReadLine)
End While
sr2.Close()