[RESOLVED] Problem with saving an image
Hi, im trying to save the pictures with the current date and time using Date.Now, but the error (The given path's format is not supported.) occurs on line 24.
It saves perfectly when i remove Date.Now, i would really want to save the pictures with the current time so that all the pics have different names.
How can this problem be solved?
vbnet Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim maxHeight As Integer = 0
Dim maxWidth As Integer = 0
For Each scr As Screen In Screen.AllScreens
maxWidth += scr.Bounds.Width
If scr.Bounds.Height > maxHeight Then maxHeight = scr.Bounds.Height
Next
Dim AllScreensCapture As New Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim screenGrab As Bitmap
Dim screenSize As Size
Dim g As Graphics
Dim g2 As Graphics = Graphics.FromImage(AllScreensCapture)
Dim a As New Point(0, 0)
For Each scr As Screen In Screen.AllScreens
screenSize = New Size(scr.Bounds.Width, scr.Bounds.Height)
screenGrab = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
g = Graphics.FromImage(screenGrab)
g.CopyFromScreen(a, New Point(0, 0), screenSize)
g2.DrawImage(screenGrab, a)
a.X += scr.Bounds.Width
Next
Dim PicName = Date.Now + ".jpg"
Dim Screenshot = "C:\Users\Public\Downloads\" + PicName
AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Re: Problem with saving an image
Did you ever bother to check what the filename you'd get from that? Would you type the following as a filename in any circumstances?
16/05/2013 20:09:37.jpg
If you want a filename then you need to translate the date into some kind of code. The simplest method is ...
Dim PicName = Date.Now.ToString("yyyyMMdd") & ".jpg"
Re: Problem with saving an image
This is because your PicName value will be something like this "5/17/2013 12:45:57 AM.jpg", which has characters which is not supported in filenames. e.g. the "/" and ":" characters are not supported in filenames.
Try this instead:
vb.net Code:
...
Dim PicName = Date.Now.ToString("MM-dd-yyyy hh-mm-ss") + ".jpg"
Dim Screenshot = "C:\Users\Public\Downloads\" + PicName
AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)
Re: Problem with saving an image
Aw man... i knew it was something as simple as that... that never came to my mind.
Well thanks anyways guys!