Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Picture Box 2

Status
Not open for further replies.

sacsac

Programmer
Dec 10, 2000
182
GB
I have a PictureBox control containing a set of graphics which I have generated. I had thought that I could copy the contents to another PictureBox by using code like:

PictureBox2.Image = PictureBox1.Image

but that does not seem to do anything. Any ideas out there?
 
Try this:

Picturebox2.Imagelocation=Picturebox1.Imagelocation
 
No go! It seems that the problem is because the PictureBox is loaded with graphics. If I load a pre-existing image from file then I can copy that just fine. So are the graphics on the PictureBox stored on a different property to .Image ?
 
You must have something else interfering with your picture copying...

I create a new project and put two picture boxes and one button on a form. I set both picture boxes SizeMode property to Zoom for ease of viewing. I then inserted the following code in the page:

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.PictureBox1.Image = Image.FromFile("C:\Documents and Settings\rjohnson9\Desktop\Downloads\473059main1_globaldisruption-670.jpg")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.PictureBox2.Image = Me.PictureBox1.Image

    End Sub

When I open the form, the picture appears in picturebox1 as I expect it to. When I click the button, the picture appears in picturebox2, slightly larger due to that picture box being a bit bigger and the zoom setting. Again this is the expected result.

Something is causing that not to work for you. Maybe some other method is overriding your image copying???

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Thanks for this mstrmage. Indeed, I can do the same with complete success i.e. when the PictureBox contains an image loaded from file all is OK. The problem arises however when I "create" the image on the PictureBox by using graphics (e.g. shapes, lines, rectangles, text etc). It then resolutely refuses to copy. ???
 
Ahhh....Try the below code. It converts the image in Picturebox1 to a stream in memory and then loads Picturebox2 with an image created from this memory stream.

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ms As New IO.MemoryStream
        PictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg)

        Me.PictureBox2.Image = Image.FromStream(ms)

    End Sub

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
That all looked good, but when I run this code I get error message #91 "Object reference not set to an instance of an object", at the first line of code:
(PictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg)
Can't see why this should be the case.
 
Well, that error would make me think the image in Picturebox1 does not truly exist.

You say you are creating the image using methods from the Graphics namespace. Can you give a sample of some of that code??

I feel that we are missing a part of the puzzle that assigns the graphics you are drawing into the image in the picturebox. Without that piece...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Well..I see what your're driving at, but the created graphics in PicBox1 definitely 'exist', inasmuch that I can see them!
Here's some sample code:


Private Sub DrawGraphics()

Dim objG As Graphics = PictureBox1.CreateGraphics


Dim rectB As New Rectangle
rectB.X = PictureBox1.Width * 0.05
rectB.Y = PictureBox1.Height * 0.05
rectB.Width = PictureBox1.Width * 0.9
rectB.Height = PictureBox1.Height * 0.75


Dim objPen As Pen
objPen = New Pen(Drawing.Color.DarkOrange, 3)
objPen.DashStyle = Drawing.Drawing2D.DashStyle.Solid


objG.DrawRectangle(objPen, rectB)
objG = Nothing



End Sub
 
Just create an image in memory and use that image for both PictureBoxes
Code:
Public Class Form2
    Private Img As Bitmap

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Img = New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(Me.Img)
        g.FillEllipse(Brushes.Red, 10, 10, 50, 50)

        Dim PB1 As New PictureBox
        Me.Controls.Add(PB1)
        PB1.Left = 10
        PB1.Top = 10
        PB1.Image = Me.Img

        Dim PB2 As New PictureBox
        Me.Controls.Add(PB2)
        PB2.Left = 10
        PB2.Top = 50
        PB2.Image = Me.Img

        g = Nothing
     End Sub
End Class
 
Thanks RiverGuy - that works fine. But it does not explain why my original method of working fails!
 
Riverguy is on to something. Have your drawgrapics method create the image in memory. Then set the image property of picturebox1 from this inmemory graphic. Then one of the copies from above should work when you need to copy it to another pocturebox

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Thanks to you all. I have a successful way of working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top