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
Related
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!
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
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
I'm using a picture box as a 'preview' screen for a webbrowser control on a another winform. It works well apart from that in order to update the picturebox control I'm finding that the code needs to run twice (double click the button) linked to the below code. Any ideas why the picturebox isn't updating in the first pass to show the newly navigated page?
Private Sub mOutput_Click(sender As Object, e As EventArgs) Handles mOutput.Click
If Not mFiles.SelectedItem Is Nothing Then
Formloading.fDisplay.WebBrowser1.Navigate(folderloc.ToString & "\" & mFiles.SelectedItem.ToString)
End If
Dim Bounds As System.Drawing.Rectangle
Dim outputscreen As System.Drawing.Bitmap
Dim graph As System.Drawing.Graphics
Dim LO As Integer = 0
With Bounds
.Height = fDisplay.WebBrowser1.Height
.Width = fDisplay.WebBrowser1.Width
.X = Formloading.fDisplay.Location.X
.Y = Formloading.fDisplay.Location.Y
.Size = Formloading.fDisplay.Size
End With
outputscreen = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = System.Drawing.Graphics.FromImage(outputscreen)
graph.CopyFromScreen(Bounds.X, Bounds.Y, 0, 0, Bounds.Size, Drawing.CopyPixelOperation.SourceCopy)
PictureBox1.Image = outputscreen
End Sub
I'm trying to add a feature to my program to take a full screenshot of the users screen when they click a button. I got the program to take the screenshot and open a file dialog box to save it, the saving works. The issue is that no matter how I save the screenshot, the saved image has significant quality loss and pixelates around text and stuff. This is a massive issue because I need the image to save exactly as it is seen on the users screen, I cannot have ANY quality loss at all. I tried to save the image as a jpg and a png and both gave me quality loss. I was wondering if anyone could point me towards some code or a method that would allow me to save the screenshots at the same quality as the users screen. I would like to save the image as a JPG or a PNG if possible. Any help would greatly be appreciated!
Get the image in Bitmap format and save it as bmp.
Private Function TakeScreenShot() As Bitmap
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
Return screenGrab
End Function
For starters, JPEG images use a lossy compression algorithm so you tend to lose quality when you save in that format. It is preferable to save as Bitmap (BMP), which is uncompressed, or PNG, which uses a lossless compression.
Here is code to copy the working area of the screen to a PNG Image.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'the working area excludes all docked toolbars like taskbar, etc.
Dim currentScreen = Screen.FromHandle(Me.Handle).WorkingArea
'create a bitmap of the working area
Using bmp As New Bitmap(currentScreen.Width, currentScreen.Height)
'copy the screen to the image
Using g = Graphics.FromImage(bmp)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), currentScreen.Size)
End Using
'save the image
Using sfd As New SaveFileDialog() With {.Filter = "PNG Image|*.png",
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop}
If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Png)
End If
End Using
End Using
End Sub
.Net usually saves the file in 96dpi, so using following code you can save the file in higher resolution with Jpeg or other format.
'Create a new bitmap
Using Bmp As New Bitmap(800, 1000, Imaging.PixelFormat.Format32bppPArgb)
'Set the resolution to 300 DPI
Bmp.SetResolution(300, 300)
'Create a graphics object from the bitmap
Using G = Graphics.FromImage(Bmp)
'Paint the canvas white
G.Clear(Color.White)
'Set various modes to higher quality
G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
'Create a font
Using F As New Font("Arial", 12)
'Create a brush
Using B As New SolidBrush(Color.Black)
'Draw some text
G.DrawString("Hello world", F, B, 20, 20)
End Using
End Using
End Using
'Save the file as a TIFF
Bmp.Save("c:\\test.Jpeg", Imaging.ImageFormat.Jpeg)
End Using
I've found that adding 3 lines to the above code significantly improves the quality of the image
var graphics = Graphics.FromImage(theRequestedAllocatedImage);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
// Then call
graphics.CopyFromScreen(..)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
PictureBox1.Image = screenGrab
PictureBox1.Image.Save("c:\picture.bmp")
End Sub