Results 1 to 4 of 4

Thread: [RESOLVED] Problem with saving an image

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    16

    Resolved [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:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.         Dim maxHeight As Integer = 0
    3.         Dim maxWidth As Integer = 0
    4.         For Each scr As Screen In Screen.AllScreens
    5.             maxWidth += scr.Bounds.Width
    6.             If scr.Bounds.Height > maxHeight Then maxHeight = scr.Bounds.Height
    7.         Next
    8.         Dim AllScreensCapture As New Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    9.         Dim screenGrab As Bitmap
    10.         Dim screenSize As Size
    11.         Dim g As Graphics
    12.         Dim g2 As Graphics = Graphics.FromImage(AllScreensCapture)
    13.         Dim a As New Point(0, 0)
    14.         For Each scr As Screen In Screen.AllScreens
    15.             screenSize = New Size(scr.Bounds.Width, scr.Bounds.Height)
    16.             screenGrab = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
    17.             g = Graphics.FromImage(screenGrab)
    18.             g.CopyFromScreen(a, New Point(0, 0), screenSize)
    19.             g2.DrawImage(screenGrab, a)
    20.             a.X += scr.Bounds.Width
    21.         Next
    22.         Dim PicName = Date.Now + ".jpg"
    23.         Dim Screenshot = "C:\Users\Public\Downloads\" + PicName
    24.         AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)
    25.     End Sub

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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"
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. ...
    2.         Dim PicName = Date.Now.ToString("MM-dd-yyyy hh-mm-ss") + ".jpg"
    3.         Dim Screenshot = "C:\Users\Public\Downloads\" + PicName
    4.         AllScreensCapture.Save(Screenshot, System.Drawing.Imaging.ImageFormat.Jpeg)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    16

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width