Results 1 to 7 of 7

Thread: Facing Issues With Draw Image Method

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Exclamation Facing Issues With Draw Image Method

    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)

    any help would be highly appreciated

    best regards
    Attached Files Attached Files

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Facing Issues With Draw Image Method

    Quote Originally Posted by Shohag_ifas View Post
    ...
    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

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Facing Issues With Draw Image Method

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Facing Issues With Draw Image Method

    Quote Originally Posted by passel View Post
    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]
    i tried dispose too, but same effect :

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Facing Issues With Draw Image Method

    Quote Originally Posted by passel View Post
    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?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    340

    Re: Facing Issues With Draw Image Method

    i am not good enough for giving thanks. But thanks a lot as you always support/help me my on and each topic.

    thanks again...

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Facing Issues With Draw Image Method

    Quote Originally Posted by Shohag_ifas View Post
    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?
    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.

Tags for this Thread

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