VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Picturebox - create a frame(rectangle) at centre position&move around Posted by prasanna_or on 19 Jul 2009 at 5:37 AM
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



Report
Re: Picturebox - create a frame(rectangle) at centre position&move Posted by seancampbell on 28 Jul 2009 at 6:00 AM
There was no attached image...

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

'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


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!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.