Results 1 to 4 of 4

Thread: Code of Calling Sub VB.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Code of Calling Sub VB.NET

    Do not know how to call a Function with this code from a button_Click Sub. I use DrawImageRectRect() in the Click event but what will be the arguments ?. Thank you.

    Code:
    Private Sub DrawImageRectRect(ByVal e As PaintEventArgs)
    
        ' Create image.
        Dim newImage As Image = Image.FromFile("SampImag.jpg")
    
        ' Create rectangle for displaying image.
        Dim destRect As New Rectangle(100, 100, 450, 150)
    
        ' Create rectangle for source image.
        Dim srcRect As New Rectangle(50, 50, 150, 150)
        Dim units As GraphicsUnit = GraphicsUnit.Pixel
    
        ' Draw image to screen.
        e.Graphics.DrawImage(newImage, destRect, srcRect, units)
    End Sub
    Like this :

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DrawImageRectRect(?, ?)
        End Sub
    Last edited by ebellounisoft; Aug 14th, 2017 at 05:46 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Code of Calling Sub VB.NET

    Um, yes you do know, because I already told you in your other thread on the same subject. Do you think that it's cool to ask a question, get an answer, ignore it and then ask the same question in another thread?

    As I have already told you, you DO NOT call that method from the Click event handler of a Button. You can't because you have no PaintEventArgs object to pass it. You call that method from the Paint event handler of the form or control you want to draw on. If you want to force a Paint event to be raised when you click that Button then you call the Refresh method of that same form or control in that Click event handler.

    That said, you absolutely must NOT call Image.FromFile there like that. Firstly, Image.FromFile locks the file until the Image object created is disposed, which you never do. Secondly, you're going to create a new Image object every time that code gets executed and, if it's called from the Paint event handler, it's going to get executed multiple times. I get the feeling that you're trying to do this to learn something but what you're doing is not the sort of thing that should be done in any real application. Keep that in mind.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Code of Calling Sub VB.NET

    Again your are correct. Yes with Paint works fine and other examples. What I am trying is to find a way to let the user select from a Form interface select from multiple images. I know there is a lot to change in the code in order to achieve this. I will keep working on it. Thank you again .

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Code of Calling Sub VB.NET

    There isn't a lot to change. How you execute the code to do the drawing will not change. If you want the user to be able to select an image then this is really the only part that needs to change:
    Code:
    Private Sub DrawImageRectRect(ByVal e As PaintEventArgs)
    
        ' Create image.
        Dim newImage As Image = Image.FromFile("SampImag.jpg")
    
        ' Create rectangle for displaying image.
        Dim destRect As New Rectangle(100, 100, 450, 150)
    
        ' Create rectangle for source image.
        Dim srcRect As New Rectangle(50, 50, 150, 150)
        Dim units As GraphicsUnit = GraphicsUnit.Pixel
    
        ' Draw image to screen.
        e.Graphics.DrawImage(newImage, destRect, srcRect, units)
    End Sub
    All you need to do is provide a way for the user to select a file, e.g. an OpenFileDialog, and store the selected file path in a variable, then use that variable to load your image from instead of your hard-coded file name. That part has got absolutely nothing to do with drawing on the form. If you know how to draw an image on the form then you know how to draw ANY image on the form.

Tags for this Thread

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