Solved

VB 2005 - image/graphics on user control

Posted on 2008-06-11
3
584 Views
Last Modified: 2012-05-05


I have a user control on which a bitmap image is painted using a graphics object (i.e. not a background image). I need to be able to capture a section of the bitmap in the mouse move event so as mouse moves, rectangle section (rct.x = e.x, rct.y = e.y, rct.width = 100, rct.height = 100) of the existing graphic is captured to an image object variable, and then transferred as the texture of a new brush object which is used to paint a copy of the defined section on a different area of the user control.

Any code examples of how to do this much appreciated...
0
Comment
Question by:therealmongoose
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Author Comment

by:therealmongoose
ID: 21758528
Here's the code I need help with....
Imports System.Drawing
 
Public Class UserControl1
 
    Dim rectCopy As Rectangle
    Dim imgCopy As Image
    Dim bBrushCopy As TextureBrush
    Dim rctPaintTo As New Rectangle(5, 70, 50, 50)
 
    Sub drawrect()
 
        Dim rect As New Rectangle(5, 5, 300, 50)
        Dim bBrush As New TextureBrush(My.Resources.Image1)
 
        Using g As Graphics = Me.CreateGraphics
 
            g.FillRectangle(bBrush, rect)
 
        End Using
 
    End Sub
 
    Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
 
        rectCopy.X = e.X
        rectCopy.Y = e.Y
        rectCopy.Width = 50
        rectCopy.Width = 50
 
        'capture screen section to imgCopy - size and location of rectCopy
        '=======================
        ' How do I copy a section of the screen to the image control?
        '
        imgCopy = ????
        '=======================
 
        bBrushCopy = New TextureBrush(imgCopy)
 
        Using g As Graphics = Me.CreateGraphics
 
            g.FillRectangle(bBrushCopy, rctPaintTo)
 
        End Using
 
 
    End Sub
 
 
    Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
 
        drawrect()
 
    End Sub
 
End Class

Open in new window

0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 500 total points
ID: 21758797
Hi,
I'm a C# guy so you might have to translate the code...
Instead of directly drawing the image on the screen I used a PictureBox. To draw an image you have to use the Graphics.DrawImage() method instead of a filled rectangle.

Regards,
Bruce
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    // may have a global bitmap  and graphics
    // to improve performance
    Bitmap map = new Bitmap(50, 50);
    Graphics g = Graphics.FromImage(map);
 
    // get actual mouse point as screen location
    Point pt = this.PointToScreen(new Point(e.X, e.Y));
    // capure specific area from screen
    g.CopyFromScreen(pt.X, pt.Y, 0, 0, map.Size);
    // populate bitmap in picturebox
    pictureBox1.Image = map;
}

Open in new window

0
 
LVL 10

Author Closing Comment

by:therealmongoose
ID: 31466102
Cool - thanks Bruce - just enough to let me solve it...

Dim pt As Point = Me.PointToScreen(New Point(e.X, e.Y))
(Point pt = this.PointToScreen(new Point(e.X, e.Y));)
 Was the bit I was missing - thanks for your time...          
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question