-
Dec 8th, 2010, 07:13 AM
#1
Thread Starter
Member
Copy From one PictureBox to another
Hello. How can i copy image from one picturebox to another?
-
Dec 8th, 2010, 08:57 AM
#2
Re: Copy From one PictureBox to another
PictureBox2.Image = PictureBox1.Image
-
Dec 8th, 2010, 09:17 AM
#3
Thread Starter
Member
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.
-
Dec 8th, 2010, 10:48 AM
#4
Junior Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
Pic1.ImageLocation = OpenFileDialog1.FileName
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Pic1.Image = Pic2.Image
End Sub
-
Dec 8th, 2010, 11:00 AM
#5
Thread Starter
Member
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.
-
Dec 8th, 2010, 11:11 AM
#6
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
-
Dec 8th, 2010, 11:18 AM
#7
Thread Starter
Member
Re: Copy From one PictureBox to another
-
Dec 8th, 2010, 11:20 AM
#8
Thread Starter
Member
Re: Copy From one PictureBox to another
I tried Me.Pic2.Image = New System.Drawing.Bitmap(Pic1.Image) but it isn't working
-
Dec 8th, 2010, 11:21 AM
#9
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
-
Dec 8th, 2010, 11:26 AM
#10
Thread Starter
Member
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.
-
Dec 8th, 2010, 11:40 AM
#11
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
-
Dec 8th, 2010, 11:46 AM
#12
Thread Starter
Member
Re: Copy From one PictureBox to another
Thank you for your answers. Respect.
-
Dec 8th, 2010, 12:26 PM
#13
Thread Starter
Member
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.
-
Dec 8th, 2010, 12:32 PM
#14
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
-
Dec 8th, 2010, 12:38 PM
#15
Thread Starter
Member
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.
-
Dec 8th, 2010, 12:42 PM
#16
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
-
Dec 8th, 2010, 12:52 PM
#17
Thread Starter
Member
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.
-
Dec 8th, 2010, 01:10 PM
#18
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
-
Dec 8th, 2010, 01:17 PM
#19
Thread Starter
Member
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
-
Dec 8th, 2010, 01:20 PM
#20
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
-
Dec 8th, 2010, 01:26 PM
#21
Thread Starter
Member
Re: Copy From one PictureBox to another
Damn. Im completely lost then. Is there any decent way of displaying the numerical values?
-
Dec 8th, 2010, 01:31 PM
#22
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
-
Dec 8th, 2010, 02:01 PM
#23
Thread Starter
Member
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
-
Dec 8th, 2010, 02:31 PM
#24
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
-
Dec 8th, 2010, 02:41 PM
#25
Thread Starter
Member
Re: Copy From one PictureBox to another
there are no prompted errors. the text box is simply empty.
-
Dec 8th, 2010, 02:51 PM
#26
Re: Copy From one PictureBox to another
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
-
Dec 8th, 2010, 03:33 PM
#27
Thread Starter
Member
Re: Copy From one PictureBox to another
Dim InspectBitMap As New Bitmap(PictureBox1.Image)
PixelColour = InspectBitMap.GetPixel(30, 60)
-
Dec 8th, 2010, 03:46 PM
#28
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
-
Dec 8th, 2010, 04:17 PM
#29
Re: Copy From one PictureBox to another
 Originally Posted by Pain33
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
-
Dec 8th, 2010, 04:32 PM
#30
Thread Starter
Member
Re: Copy From one PictureBox to another
Yes! Thank you very much )
-
Dec 8th, 2010, 04:57 PM
#31
Thread Starter
Member
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
-
May 24th, 2024, 01:07 AM
#32
New Member
Re: Copy From one PictureBox to another
 Originally Posted by Pain33
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|