Copy graphics of picturebox-control

juhah

Active member
Joined
Apr 16, 2007
Messages
28
Programming Experience
3-5
I'm changing a software made with VB5 to VB.NET 2003, and i have a problem with picturebox:

When the user enters and moves the mouse above a picturebox, a vertical line must be shown at the mouse arrow point. The line must follow the mouse. Also, some graphics (strings or lines) must be shown in the picturebox at the same.

How can I do this? I managed to draw the line and make it like "follow" the mouse, but it erases all other graphics away.

I came up with one solution:

1. Copy the graphic data into memory at the point where the line must be drawn
2. Draw the line
3. When the mouse moves to a new point, paste the graphic data from memory to the previous point

This would make sure nothing's erased, but I just don't know how to do it. Can someone help me?
 
The way I've handled this is very simple...

I use the BackgroundImage property of the picturebox to store the background image, then I use the Image property as a transparent layer for drawing my lines.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#008000]   'set the background image[/COLOR][/SIZE]
[SIZE=2]   PictureBox1.BackgroundImage = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap([/SIZE][SIZE=2][COLOR=#800000]"C:\pics\MyPic.bmp"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PictureBox1_MouseMove([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PictureBox1.MouseMove[/SIZE]
[SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] MyBitmap [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Bitmap[/SIZE]
[SIZE=2]   MyBitmap = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap(PictureBox1.Width, PictureBox1.Height)[/SIZE]
 
[SIZE=2][COLOR=#008000]   'If it were me, I'd probably change the cursor to a cross[/COLOR][/SIZE]
[SIZE=2]   PictureBox1.Cursor = Cursors.Cross[/SIZE]
 
[SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = Graphics.FromImage(MyBitmap)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] Vline [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Drawing2D.GraphicsPath[/SIZE]
[SIZE=2]   Vline = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Drawing2D.GraphicsPath[/SIZE]
[SIZE=2]   Vline.AddLine([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] PointF(e.X, 0), [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] PointF(e.X, PictureBox1.Height))[/SIZE]
[SIZE=2]   g.DrawPath([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(Color:=Color.Red, Width:=1), Vline)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]   Dim[/COLOR][/SIZE][SIZE=2] Hline [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Drawing2D.GraphicsPath[/SIZE]
[SIZE=2]   Hline = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Drawing2D.GraphicsPath[/SIZE]
[SIZE=2]   Hline.AddLine([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] PointF(0, e.Y), [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] PointF(PictureBox1.Width, e.Y))[/SIZE]
[SIZE=2]   g.DrawPath([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(Color:=Color.Red, Width:=1), Hline)[/SIZE]
 
[SIZE=2]   PictureBox1.Image = MyBitmap[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Thanks! That works ok but there is another problem:

I have to draw strings and different graphics to the control from code, not from a file. I tried to make those graphics (lines and strings and stuff) as bitmaps and then set them as backgroundimage of the control. This works, but the BackGroundImage-property doesn't support transparent values! So it colors the transparent values as black, so actually the whole control is filled with black color.

Any other solutions?
 
I don't get black when drawing text to image and using it as background image, transparent and fine here:
VB.NET:
Dim bmp As New Bitmap(200, 200)
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawString("hello Graphics", Me.Font, Brushes.Blue, 10, 10)
g.Dispose()
PictureBox1.BackgroundImage = bmp
 
It doesn't work for me... "Hello graphics" seems blue but everything else turns to black. I have Visual Studio. NET 2003-version.
 
ok, I only tested it with VS2005 earlier, I see now that there is black background by default for VS2003, but setting the Picturebox BackColor to Transparent solves it for VS2003.
 
Back
Top