Visual Basic 2008: A parameter passed ByVal is being changed!?
I'm writing a simple image editing program.
Important bits of code:
In class "Document":
Public Delegate Function Effect(ByVal Image As Bitmap, ByVal Params() As Object) As Bitmap
Private Image As Bitmap
Private CImage As Bitmap
Public Sub ApplyEffect(ByVal Effect As Effect, ByVal Params() As Object)
CImage = Effect(Image, Params)
ReZoom()
End Sub
In a form:
Private Function Contrast(ByVal Image As Bitmap, ByVal Params() As Object) As Bitmap
' Some code which edits Image, and returns it
End Function
I hope this is enough code to make sense of my problem. I have a class called Document, which contains a photo, information about it, some functions etc. It contains 2 Bitmaps, Image and CImage. Image is the original photo, and CImage is the edited photo. The Delegate Sub defines the form of an effect function, which takes *ByVal* Image and produces a new Bitmap which is put in CImage and displayed. However, Image is being changed, which shouldn't happen.
Goodness this is hard to explain!
In my Contrast function, there is code which uses LockBits on Image, edits the image in unmanaged memory, UnLockBits, and returns Image. As Image is passed ByVal, the original Image shouldn't be altered, but it is. Any ideas why?
|