Dynamic label click event crash, dodgy coding?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1614 Views - Last Post: 31 January 2013 - 10:36 AM Rate Topic: -----

#16 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Dynamic label click event crash, dodgy coding?

Posted 29 January 2013 - 12:43 PM

View PostGinister, on 29 January 2013 - 11:47 AM, said:

I've moved onto the print code but I'm getting the error "Out of memory" when I try and assign a file location to a print screen. I think it's because I have the wrong file type, but I'm not entirely sure.
Here's the code from the print form.

                PrintForm1.PrintFileName = CurDir() & "\Printout.png"
                PrintForm1.Print()


You're right. It's actually encapsulated PostScript. You need to first make a bitmap of the client area of your form, then save it to a file in PNG format. Not as difficult as it seems...
    Sub takescreenshot()
        Try
            Dim Img As New Bitmap(Me.Width, Me.Height)
            Dim g As Graphics = Graphics.FromImage(Img)
            g.CopyFromScreen(Me.Left, Me.Top, 0, 0, Img.Size)
            g.Dispose()
            Dim path As String = "j:\Printout.png"
            Img.Save(path, Imaging.ImageFormat.Png)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



So, you need to do the following in order to print the form...
                takescreenshot() ' saves the file
                PrintForm1.PrintFileName = CurDir() & "\Printout.png" 'sets the filename to print from
                PrintForm1.Print() ' prints the file


Edit: I haven't tried it, but your code might work if you save to a file with an extension of .eps

This post has been edited by lar3ry: 29 January 2013 - 12:46 PM

Was This Post Helpful? 2
  • +
  • -

#17 Ginister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 19-December 12

Re: Dynamic label click event crash, dodgy coding?

Posted 29 January 2013 - 03:15 PM

View Postlar3ry, on 29 January 2013 - 12:43 PM, said:

View PostGinister, on 29 January 2013 - 11:47 AM, said:

I've moved onto the print code but I'm getting the error "Out of memory" when I try and assign a file location to a print screen. I think it's because I have the wrong file type, but I'm not entirely sure.
Here's the code from the print form.

                PrintForm1.PrintFileName = CurDir() & "\Printout.png"
                PrintForm1.Print()


You're right. It's actually encapsulated PostScript. You need to first make a bitmap of the client area of your form, then save it to a file in PNG format. Not as difficult as it seems...
    Sub takescreenshot()
        Try
            Dim Img As New Bitmap(Me.Width, Me.Height)
            Dim g As Graphics = Graphics.FromImage(Img)
            g.CopyFromScreen(Me.Left, Me.Top, 0, 0, Img.Size)
            g.Dispose()
            Dim path As String = "j:\Printout.png"
            Img.Save(path, Imaging.ImageFormat.Png)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub



So, you need to do the following in order to print the form...
                takescreenshot() ' saves the file
                PrintForm1.PrintFileName = CurDir() & "\Printout.png" 'sets the filename to print from
                PrintForm1.Print() ' prints the file


Edit: I haven't tried it, but your code might work if you save to a file with an extension of .eps

Hi, thanks. I tried implementing it with my own variables but it didn't work, so I copied in your exact coding, and it pops up saying "a generic error occured in GDI+." and crashes when it tries to print the image, still saying that it is "out of memory". Also, if I open the png, no preview shows. Please help!
Was This Post Helpful? 0
  • +
  • -

#18 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Dynamic label click event crash, dodgy coding?

Posted 30 January 2013 - 10:18 AM

Quote

Hi, thanks. I tried implementing it with my own variables but it didn't work, so I copied in your exact coding, and it pops up saying "a generic error occured in GDI+." and crashes when it tries to print the image, still saying that it is "out of memory". Also, if I open the png, no preview shows.

When you say "my own variables", do you mean the path of the file? I see that I left my path as "j:\Printout.png", but didn't change yours. They do, of course, need to be the same. Actually, it's better to have parameters for the subroutine that generates the file, like so...
    Sub takescreenshot(ByVal path As String, fmt As Imaging.ImageFormat)
        Try
            Dim Img As New Bitmap(Me.Width, Me.Height)
            Dim g As Graphics = Graphics.FromImage(Img)
            g.CopyFromScreen(Me.Left, Me.Top, 0, 0, Img.Size)
            g.Dispose()
            Img.Save(path, fmt)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


Then you can call it with any filename and format, with something like
takescreenshot(CurDir() & "\foo.jpg", Imaging.ImageFormat.Jpeg)Imaging.ImageFormat.Jpeg)

As for your error, I'm at a loss to explain it, except to say that it's a GDI problem of some sort. Can you tell me what line causes the error? If you set a breakpoint and step through the code, you should be able to tell. If you aren't familiar with them, see the Debugging Skills Tutorial
Was This Post Helpful? 0
  • +
  • -

#19 Ginister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 19-December 12

Re: Dynamic label click event crash, dodgy coding?

Posted 30 January 2013 - 11:43 AM

View Postlar3ry, on 30 January 2013 - 10:18 AM, said:

Quote

Hi, thanks. I tried implementing it with my own variables but it didn't work, so I copied in your exact coding, and it pops up saying "a generic error occured in GDI+." and crashes when it tries to print the image, still saying that it is "out of memory". Also, if I open the png, no preview shows.

When you say "my own variables", do you mean the path of the file? I see that I left my path as "j:\Printout.png", but didn't change yours. They do, of course, need to be the same. Actually, it's better to have parameters for the subroutine that generates the file, like so...
    Sub takescreenshot(ByVal path As String, fmt As Imaging.ImageFormat)
        Try
            Dim Img As New Bitmap(Me.Width, Me.Height)
            Dim g As Graphics = Graphics.FromImage(Img)
            g.CopyFromScreen(Me.Left, Me.Top, 0, 0, Img.Size)
            g.Dispose()
            Img.Save(path, fmt)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


Then you can call it with any filename and format, with something like
takescreenshot(CurDir() & "\foo.jpg", Imaging.ImageFormat.Jpeg)Imaging.ImageFormat.Jpeg)

As for your error, I'm at a loss to explain it, except to say that it's a GDI problem of some sort. Can you tell me what line causes the error? If you set a breakpoint and step through the code, you should be able to tell. If you aren't familiar with them, see the Debugging Skills Tutorial

Thank you, I think the ByVals didn't match the img.save bit, which is evidently vital :P Many thanks for the help, again!
Was This Post Helpful? 0
  • +
  • -

#20 Ginister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 19-December 12

Re: Dynamic label click event crash, dodgy coding?

Posted 31 January 2013 - 09:54 AM

Heh, yet another thing! I'm taking a screenshot, as seen above, but when I call it in the click sub, it takes a screenshot of the form but the label that is being booked is not red, nor does it say "booked". I believe that it is because it is in the same sub, but if I have a message box, it takes a screenshot of the form with it red, but with a message box floating too.
How do I make it print screen when clicked, but still showing it being booked?
Here's the relevant code for reference.
  If Response = MsgBoxResult.Yes Then
                Dim WriteToFile As String = WriteXCoord & "," & WriteYCoord & "," & SelectedTime & "," & SelectedDay & "," & SelectedWeek & "," & SelectedRoom & "," & Account.ClassCode & vbNewLine
                My.Computer.FileSystem.WriteAllText(PCFile, WriteToFile, True)




                myLabel.BackColor = Color.Red
                myLabel.Text = "Booked"
                TakeScreenshot(CurDir() & "\ScreenShot" & FoundClassCode & ".jpg", Imaging.ImageFormat.Jpeg)

                Confirmation_Form.Show()
                Me.Hide()

            End If


Was This Post Helpful? 0
  • +
  • -

#21 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Dynamic label click event crash, dodgy coding?

Posted 31 January 2013 - 10:36 AM

View PostGinister, on 31 January 2013 - 10:54 AM, said:

Heh, yet another thing! I'm taking a screenshot, as seen above, but when I call it in the click sub, it takes a screenshot of the form but the label that is being booked is not red, nor does it say "booked".

It's because the program is not handling the control while it is busy with another event. You can solve it by inserting a line between setting the Label properties and taking the screenshot, like this...
    myLabel.BackColor = Color.Red
    myLabel.Text = "Booked"
    MyLabel.Refresh()
    TakeScreenshot(CurDir() & "\ScreenShot" & FoundClassCode & ".jpg", Imaging.ImageFormat.Jpeg)


This allows the control to show its changes before going on with the event you are currently handling.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2