Results 1 to 32 of 32

Thread: Copy From one PictureBox to another

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Copy From one PictureBox to another

    Hello. How can i copy image from one picturebox to another?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Copy From one PictureBox to another

    PictureBox2.Image = PictureBox1.Image
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Thank you for your answer.

    Unfortunately i am still doing something wrong.
    I am developing this application which is capable of opening image.
    Code:
        Dim OpenFileDalog1 As New OpenFileDialog
        Dim Pic1 As PictureBox
        Dim Pic2 As PictureBox
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            OpenFileDialog1.ShowDialog()
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
    
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            PictureBox2.Image = PictureBox1.Image
        End Sub
    
    
        Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
    
        End Sub
    End Class
    Maybe there's something wrong with the code, or i simply put it in the wrong place;/?
    Last edited by Pain33; Dec 8th, 2010 at 09:39 AM.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    28

    Re: Copy From one PictureBox to another

    Hi.

    Well since you are declaring your Picture boxes as Pic1 & Pic2 you need to use those names, also its better to just use the Picturebox.ImageLocation identifier.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         OpenFileDialog1.ShowDialog()
    4.         Pic1.ImageLocation = OpenFileDialog1.FileName
    5.     End Sub
    6.  
    7. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    8.         Pic1.Image = Pic2.Image
    9.     End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    when i change the button2 code to Pic1.Image = Pic2.Image, then it says: Object reference not set to an instance of an object.

  6. #6
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    What are you calling your pictureboxes?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Pic1 and Pic2

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    I tried Me.Pic2.Image = New System.Drawing.Bitmap(Pic1.Image) but it isn't working

  9. #9
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    What line does the error occur on?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    this one: Pic1.Image = Pic2.Image
    if i declare
    Dim Pic1 As new PictureBox
    Dim Pic2 As new PictureBox
    then the error does not appear, but the second image is not appearing as well. nothing happens.

  11. #11
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    You have this line:
    Code:
    PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    Which sets an image to PictureBox1

    Then you do this:
    Code:
    Pic1.Image = Pic2.Image
    You are setting the image equal to an image that has never been set to anything, which is one reason why you do not see anything. Another is you have never set the parent of your new pictureboxes.

    When you do this:
    Code:
    Dim Pic1 As PictureBox
    You are declaring Pic1 as a type of picturebox, but are not setting it to a value, that's why you get:
    Object reference not set to an instance of an object.
    When you do this:
    Code:
    Dim Pic1 As new PictureBox
    It is the same as doing this:
    Code:
    Dim Pic1 As PictureBox
    Pic1 = new PictureBox
    You don't get the error because you are setting Pic1 to a reference to a new picturebox. But nowhere are you setting the image.

    You must decide where you want the image to appear, in PictureBox1 or Pic1.
    You may not even need Pic1 or Pic2. You won't if you have two pictureboxes on the form. If you do then use those and get rid of Pic1 and Pic2.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Thank you for your answers. Respect.

  13. #13

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Oh one more thing. Let's say i want to manipulate pixels within the picturebox.

    I have created variable InspectBitMap
    Code:
    Dim InspectBitMap As New Bitmap(PictureBox2.Image)
    Then PixelColour
    Code:
    Dim PixelColour As Color
    and used getPixel
    Code:
    PixelColour = InspectBitMap.GetPixel(30, 60)
    now then, how can i convert it to numeric values?
    i tried this:
    Code:
    txtResult.BackColor = PixelColour   
    txtResult.Text = PixelColour.ToString
    but it did not work. Maybe there was a problem with declaration of txtResult? I declared it as String

    Just in case heres my whole code:
    Code:
    Public Class Form1
    
        Dim OpenFileDalog1 As New OpenFileDialog
        Dim InspectBitMap As New Bitmap(PictureBox2.Image)
        Dim PixelColour As Color
        Dim txtResult As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            OpenFileDialog1.ShowDialog()
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PixelColour = InspectBitMap.GetPixel(30, 60)
        End Sub
    
        Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
    
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            PictureBox2.Image = PictureBox1.Image
        End Sub
    
    
        Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
    
        End Sub
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            txtResult.BackColor = PixelColour
            txtResult.Text = PixelColour.ToString
        End Sub
    End Class
    any help would be appreciated very much.

  14. #14
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    When you say "it did not work" that doesn't help us.

    You need to say what it DID do AND what you wanted it to do.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  15. #15

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    There's an error in this line

    txtResult.BackColor = PixelColour
    txtResult.Text = PixelColour.ToString


    Text is not member of string
    Backcolour is not member of string.
    That's it.

    I wanted the numeric values to be displayed in the textbox
    Last edited by Pain33; Dec 8th, 2010 at 12:42 PM.

  16. #16
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    That is correct, neither one of those properties belong to a string type.

    I have no idea what you are trying to do. It makes no sense, and you didn't give me any information like I asked you to.

    The prefix txt is normally used to indicate a textbox, which those properties belong to.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  17. #17

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Seems i screwed up my explanation.


    Firstly you must create a variable that will hold the bitmap image found in the Picture Box.

    Dim InspectBitMap As New Bitmap(PictureBox1.Image)


    To find the colour of any pixel in the bitmap i use the GetPixel(x,y) function. This function returns a color argument which means the variable used to store the colour must be declared as type Color.

    Dim PixelColour As Color
    PixelColour = InspectBitMap.GetPixel(30, 60)


    Variable PixelColour can be used to change the colour property of another control like a text box, or converted to a string to show the numeric values

    txtResult.BackColor = PixelColour
    txtResult.Text = PixelColour.ToString

    That is what im trying to do. Get the numeric values.
    Last edited by Pain33; Dec 8th, 2010 at 12:57 PM.

  18. #18
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    That explanation was good, however what is this supposed to do:
    Code:
    txtResult.BackColor = PixelColour
    txtResult.Text = PixelColour.ToString
    That's what I need to know. If you want a numeric value that what are you doing with Backcolor and Text?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  19. #19

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Im trying to convert Backcolor and Text to string. So that they can be displayed in the textbox. I think thats not how it works though

  20. #20
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    The Backcolor and Text of what? You have txtResult declared as a string. A string does not have a backcolor. And the Text property when used properly IS a string, so there would be no conversion there.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  21. #21

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Damn. Im completely lost then. Is there any decent way of displaying the numerical values?

  22. #22
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    You can put a textbox on the form, if you call it txtResult that may be what you want. But you never told me you wanted to change the background of a textbox to a specific color.

    If you just want to display the numerical value you can use a msgbox:
    Code:
    MessageBox.Show(PixelColour.ToString)
    There are also different ways of displaying something, A messagebox, a textbox, a label, text on a form.

    You really should figure out what you wish to do, and if you know, they you need more detailed explanations.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  23. #23

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    I tried to get the numeric value, but again failed
    Code:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Dim InspectBitMap As New Bitmap(PictureBox1.Image)
            PixelColour = InspectBitMap.GetPixel(30, 60)
            TextBox1.Text = PixelColour.ToString
        End Sub

  24. #24
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    what went wrong on what line?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  25. #25

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    there are no prompted errors. the text box is simply empty.

  26. #26
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    what is in PixelColour?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  27. #27

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Dim InspectBitMap As New Bitmap(PictureBox1.Image)
    PixelColour = InspectBitMap.GetPixel(30, 60)

  28. #28
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Copy From one PictureBox to another

    put a breakpoint on the line then print the value in the debug window then post it here.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  29. #29
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Copy From one PictureBox to another

    Quote Originally Posted by Pain33 View Post
    there are no prompted errors. the text box is simply empty.
    Probably because the code to change the textbox is inside the textbox change event, which normally doesn't make any sense.

    Code:
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim PixelColour As Color
            Dim OpenFileDialog1 As New OpenFileDialog
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
                Dim InspectBitMap As New Bitmap(PictureBox1.Image)
                PixelColour = InspectBitMap.GetPixel(30, 60)
                TextBox1.Text = PixelColour.ToString
                TextBox1.BackColor = PixelColour
            End If
    
        End Sub

  30. #30

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    Yes! Thank you very much)

  31. #31

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    38

    Re: Copy From one PictureBox to another

    I will not create another topic, i will ask this right here. It's about flipping the image.
    As far as i know the code for flipping images is
    Code:
    PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipY)
    So do i have to put this code in the BUTTON section? Or somewhere else?
    I tried this
    Code:
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipY)
        End Sub
    End Class
    , but no luck so far

  32. #32
    New Member
    Join Date
    May 2024
    Posts
    1

    Re: Copy From one PictureBox to another

    Quote Originally Posted by Pain33 View Post
    Hello. How can i copy image from one PictureBox to another?


    I have similar problems with it not showing on screen when I don't finish the copy/transfer with:

    pic1.refresh()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width