Greetings...
hope all are fine..
i am trying to making a image effect.
Let say:
there is 3 picture box
when i click on a button picture1 drawn on picture3
now to do the effect again i set pic3=pic2
and click the button again. it's drawn the pic on on pic3
but the issue is it also change pic2 to pic1 (or may be to pic3) why
so, please download the attached file and help me out. I am stuck..
i had experience that many one don't want to download attachment for them: (but better if you download the vb.net source files)
Steps:
1. Create a new vb.net project (winform)
2. Place 3 Picturebox (size Mode Property to stretch image)
3. Now Add 3 Different Picture On The Picture boxes
3. Add A Button
Now Use Bellow Source Code:
Code:
Public Class Form1
Dim bmp1 As Bitmap
Dim bmp2 As Bitmap
Dim grpDst As Graphics
Dim FromRect As Rectangle
Dim ToRect As Rectangle
Private Sub cmd1_Click(sender As System.Object, e As System.EventArgs) Handles cmd1.Click
bmp1 = New Bitmap(PictureBox1.Image)
bmp2 = New Bitmap(PictureBox2.Image)
grpDst = Graphics.FromImage(PictureBox3.Image)
SDrawTest()
PictureBox3.Image = PictureBox2.Image REM to load another image on pic3 to set pic 1 again
PictureBox2.Refresh()
PictureBox3.Refresh()
End Sub
Sub SDrawTest()
FromRect = New Rectangle(0, 0, bmp1.Width, bmp1.Height)
ToRect = New Rectangle(0, 0, bmp1.Width, bmp1.Height)
grpDst.DrawImage(bmp1, ToRect, FromRect, GraphicsUnit.Pixel)
PictureBox3.Refresh()
System.Threading.Thread.Sleep(1000) REM to show what the effect does
End Sub
End Class
Now just run and press the button 2 time (better if you gap few sec)
on 2nd run picture 2 changes to picture 1 (or may be 3) i don't know
but why?
i just want to show/draw the source image on target image
(May main gold is manipulating vb6 PaintPicture method)
...
Let say:
there is 3 picture box
when i click on a button picture1 drawn on picture3
now to do the effect again i set pic3=pic2
and click the button again. it's drawn the pic on on pic3
but the issue is it also change pic2 to pic1 (or may be to pic3) why
....
pic3 is a reference to an object.
pic2 is a reference to an object.
pic3 = pic2
Now pic3 and pic2 reference the same object, the one pic2 was referencing.
Since they both "point" to the same thing, you can use pic3 or pic2 to change the underlying object.
Perhaps you want to make a copy of the image using the Clone method, something like
PictureBox3.Image = CType(PictureBox2.Image.Clone,Image) REM to load another image on pic3 to set pic 1 again
Or perhaps, redraw the image
Code:
Using g as Graphics = Graphics.FromImage(PictureBox3.Image)
g.DrawImage(PictureBox2.Image,0,0)
End Using
If an object has a Dispose method, I always like to call it before the object goes out of scope, to cleanup unmanaged resources sooner, so would code (if I understand your intent) the sub as follows. The Using block will auto Dispose the used object, and the bitmaps I just manually dispose of.
Code:
Private Sub cmd1_Click(sender As System.Object, e As System.EventArgs) Handles cmd1.Click
bmp1 = New Bitmap(PictureBox1.Image)
bmp2 = New Bitmap(PictureBox2.Image)
Using grpDst = Graphics.FromImage(PictureBox3.Image)
SDrawTest()
grpDst.DrawImage(PictureBox2.Image, 0, 0)
PictureBox2.Refresh()
PictureBox3.Refresh()
End Using
bmp1.Dispose()
bmp2.Dispose()
End Sub
If an object has a Dispose method, I always like to call it before the object goes out of scope, to cleanup unmanaged resources sooner, so would code (if I understand your intent) the sub as follows. The Using block will auto Dispose the used object, and the bitmaps I just manually dispose of.
[/code]
pic3 is a reference to an object.
pic2 is a reference to an object.
pic3 = pic2
Now pic3 and pic2 reference the same object, the one pic2 was referencing.
Since they both "point" to the same thing, you can use pic3 or pic2 to change the underlying object.
but my understanding is: (may be very weak)
X = 5
Y = 10
Z=15
So, if i call
Z=Y (so, the value of the z only should be changed didn't it)? Y Not? right?
So, if i call
Z=Y (so, the value of the z only should be changed didn't it)? Y Not? right?
It depends on whether the variable is a Value Type or a Reference Type.
If X,Y and Z are a Value Type, like an Integer, Double, or a Structure, then when you do Z = Y then the Z value is a copy of the Y value, but they are separate values.
But if Y and Z were Reference Types, then the value is in memory somewhere, and Y and Z are references, like C pointers to where the value is in memory.
A Class is a reference type, and so controls, images, bitmaps, etc. are allocated a fair amount of memory to hold the information, an Instance of that class.
When you pass a large object to a method, you aren't passing the large amount of data that makes up the object, but just the four or eight byte reference (i.e. pointer) to that object.
If you say Z = Y, you are not copying all the data that makes up Y to another place and setting Z to point to that new place. You are just copying the 4 or 8 byte reference so now Z and Y are two separate reference variables, the reference are the same so point to the same object in memory.