Results 1 to 23 of 23

Thread: vb.net align imported coordinates to an image

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    vb.net align imported coordinates to an image

    Hi All,

    I am working on a simple tool that will import coordinates with some other info, which all works great. What I am having an issue with is, I am actually creating buttons that are placed at the center of particular points on an image in my form. The coordinates match the physical dimensions of a circuit board. I am using a very flat high quality image of the board as the image background, but of course there is a certain amount of stretch and skew of the image. Is there a way to adjust an image so that I can align it to the button coordinates perfectly? Or even better, is there a way to click on several points to perfectly align the buttons to the image? I hope I am making sense explaining what I am trying to do. Thanks

  2. #2
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    264

    Re: vb.net align imported coordinates to an image

    Better to show us your code...
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    Instead of using the image of your circuit board as your backgroundimage, use you form.paint event, and draw the circuit board image at actual size on your form…

    Code:
    Private Sub Form1_Paint(sender as Object, e as PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawImage(yourImage, Point.Empty)
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    Hi paul... so what I am doing right now with the image is trying to get it to be a 1:1 copy of the physical circuit board dimensions, but no matter what it is always off just a bit. The dimensions on the circuit board are quite accurate and basically a .CAD file. So if I use the CAD file to import into my project, I know the placement of the buttons are perfect, but the image of course never is quite perfect as I cannot get just the right alignment. I was hoping for a way to use some reference point on the imported file and then use the same on the image to get a perfect alignment between the two. It seems like the larger the image, the more it starts to skew off in both X & Y.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    If it really is a case that all of your coordinates are off by a uniform amount, it would be easy to use a couple of NumericUpDown controls to align one button manually, then when you’re happy with it, load the entire set of buttons using your file of coordinates, with each coordinate offset by your previously determined number of pixels for the left and top positions…

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    Thanks paul. Do you mean change the file of coordinates with the offset of that first button?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    I meant loading your full set of buttons with the offset, but if changing your file works for you, do that.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    Thanks paul, I will give this a try

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    Hi paul, ok I have tried a couple of small things to get proper adjustment but have had no luck. There seems to be enough stretch in the image where I cannot get alignment from corner to corner set properly. With the file coordinates being exact. I convert the board image from inch to pixels and adjust the size to that. I have even gone as far as placing the board over my screen image and see that the alignment is almost exact. I make the form sizeable so I can try and tweak the size to match the buttons on the screen only to see that the offset is not uniform across the image. I'm not really sure how I will get around this.... my search continues.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    You realise that size, ie height and width are the external dimensions of the form?
    Use (in form_load) Me.SetClientSizeCore(exact width in pixels, exact height in pixels)

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    BTW. I’d make the form fixedsingle or fixeddialog.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    Also, stretching or compressing the image will never have any good results with exact coordinates

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    That is what I am finding. The only way I have found to do this is in Excel, where it lets you resize the image in increments from top, bottom, left, right. For vb.net, I have checked every dimension possible and everything matches until the data loads on the screen. The variation is generally at the top and bottom of the image, maybe 1mm of offset. The center of the image is pretty well aligned. My image is 715px, 672px

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    https://docs.microsoft.com/en-us/dot...owsdesktop-6.0

    Set your Form BackgroundImageLayout property to None

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    Ok, that at least seems to have placed buttons in a uniform offset. I have approx 125 buttons and they are off roughly 1mm to the right and 1mm down.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    you had mentioned before about form1_paint event? what would that do different?

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    Quote Originally Posted by mikeg71 View Post
    you had mentioned before about form1_paint event? what would that do different?
    Compared to what you have now, it probably wouldn't do anything, as i posted, but try this...

    Code:
    Private Sub Form1_Paint(sender as Object, e as PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawImage(yourImage, New Point(-1, -1))
    End Sub
    I can't guarantee this'll work with other images

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    That code will draw the unstretched image full size, at the point (top-left) specified

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    It gives me a bunch of overloads with 'DrawImage' underlined

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    That is the correct overload. A bitmap, and a point

    E.graphics.DrawImage(New BitMap(“path”), New Point(-1,-1))

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    It attempts to load but I get an invalid parameter error with no details.

    Code:
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawImage(New Bitmap("4-946020.bmp"), New Point(-1, -1))
        End Sub
    * update.... I need coffee, thats not a path

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    140

    Re: vb.net align imported coordinates to an image

    However, you are correct, it does do strange things to the image size. So for now, I am using:

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.SetClientSizeCore(715, 672)
            Call CreateBtns()
        End Sub
    And my image is sized to 715 x 672 pixels.

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,753

    Re: vb.net align imported coordinates to an image

    The form paint code will draw your image at 100% no stretching, no compressing.
    If your stored image isn’t 715px*672px, then if that is the size at which all of your coordinates were calculated, you need to resize the image to that size before drawing the image in form paint. The New Point(x,y) argument can be used to offset the position the image is drawn at. A negative -1px x value will move the button positions 1px to the right. A positive x value will move your on-form button positions to the left… The most important part is to use the correct sized image. Offsets can be tested by a process of trial and error.

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