Picturebox - create a frame(rectangle) at centre position&move around - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Picturebox - create a frame(rectangle) at centre position&move around

prasanna_orprasanna_or Posts: 1Member
Hi Experts,

I have a picturebox with loaded image. Inside picturebox, I want to create a rectangle at centre position of picturebox and also i have to move the rectangle window across the picturebox.

Please see the attached image file.

Kindly guide me

Regards,
Prasanna



Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    There was no attached image...

    Regardless, here is an example to get you started on what you want to do:

    [code]
    'This form has a PictureBox named PictureBox1 on it, that is 600x600 size
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    If IsNothing(PictureBox1.BackgroundImage) Then
    'I am not loading an image into the picture box
    'So I check if this image is nothing, if so, make one
    'Otherwise, I would get a nullReferenceError when I try
    'To use PictureBox1.BackgroundImage
    PictureBox1.BackgroundImage = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    End If
    'Associate Graphics object G to PictureBox1.BackgroundImage
    Dim G As Graphics = Graphics.FromImage(PictureBox1.BackgroundImage)
    'Unconditional Fill with the color white
    G.Clear(Color.White)
    'Get the Mouse Coordinates into a Point object
    Dim B As Point = e.Location
    'Draw a rectangle on PictureBox1.BackgroundImage using the
    'Graphics object G
    G.DrawRectangle(Pens.Black, B.X - 20, B.Y - 20, 40, 40)

    'It is important to call the Refresh command for objects
    'that an image was drawn on to. This way the changes will
    'be visible
    PictureBox1.Refresh()
    End Sub
    [/code]

    This code should make a Rectangle center on the cursor. The reason I do B.X - 20, B.Y - 20 is because the 40x40 rectangle needed to center (hence, starting x - halfwidth, starting y - halfheight)

    Hope this helps!
Sign In or Register to comment.