Results 1 to 21 of 21

Thread: Image Transition Effect (Using Gradient/Alpha Mask) Image Change One Image To Anothe!

Threaded View

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

    Re: Image Transition Effect (Using Gradient/Alpha Mask) Image Change One Image To Ano

    Quote Originally Posted by Shohag_ifas View Post
    ..., can you tell me what is the purpose of that class module, can i skip that and use regular bitmap object instead of that class module?
    i am trying to handle your code now and got stuck.. so, your earlier help will be highly appreciated
    ...
    The purpose of the class module is to take the place of the API calls you had in the VB6 code to set up a SafeArray structure and point it at the bitmap data which allowed you to access the bits of the bitmap directly through that array.
    A difference being that your VB6 code created the SafeArray structure each time it was going to access the bitmaps. This was fairly quick to do because creating the SafeArray structure to point at the existing bitmap only had to create the "pointer" structure to the bitmap.

    This class sort of does the same thing, but in reverse. The class will allocate an array in memory of the size needed to hold the bitmap. It will get a handle to that array memory, designating it as pinned so it will stay in place for as long as we need it.
    It will then create a bitmap, passing the handle to the constructor, so the bitmap data will be stored using the same memory as the array.
    Now you can access the bitmap as a bitmap, so you can use it for drawing with or drawing on using the normal .Net Graphics object methods, and you can access the pixels directly by changing values in the array.
    This allows quick access to the pixels through the array. As boobs boops mentioned, the other quick way to get to the pixels involves mover logic to essential copy the bits into an array for access, making changes, then copying them back.

    What does your code look like that is trying to make use of the class? What is hanging you up?

    Quote Originally Posted by Shohag_ifas View Post
    ...also, sir, your class module didn't use dispose function/sub? isn't that needed/necessary? or you forgot that?...
    No, not forgotten. You see the Sub Finalize. Whenever no references to an instance of the class exists, the object is available for Garbage Collection, and the GC will call that Finalize Sub to clean up the object. Setting the bmp reference to nothing will allow the GC to also clean up the associated memory and call the Dispose methods on those objects that implement the IDisposeable Interface. You can call the Dispose method yourself to even out timing, rather than have the GC do it later, especially if you are creating a lot of IDisposable objects for short periods of use. In this particular example we are not creating a lot of disposable objects rapidly. The bitmap create remains for as long as the instantiated object exists.
    The only place, in the class, that an object should be disposed is in the finalize sub. That only occurs regularly in this example each time you click the button, and call the Init routine. It creates a new instance of the class, so the old instance is no longer referenced. Since this doesn't happen all that rapidly, the GC is allowed to clean up the object rather than add an IDisposable interface to the class.
    If you put a counter in the Init sub, and a counter in the Finalize sub, and print out the count in each each time they are called, if the object was disposed of by the GC with every New call in the Init sub, then the Finalize count should always be one less than the Init count. But, the GC will finalize the object when it wants, so if you hit the button a lot, you may see the count differ by several for some period of time. But you will see it clean up several at a time sometimes, and generally remain close to the Init count, eventually event getting back to within 1, which means all old instances have been cleaned up.
    It probably wouldn't hurt to dispose of the bitmap object ourselves in the Finalize sub if you want, but technically I don't think it is necessary in the long run.
    Code:
      Protected Overrides Sub Finalize()
        Bmp.Dispose()
        Bmp = Nothing
        Handle.Free()
        Handle = Nothing
        Pixels = Nothing
        MyBase.Finalize()
      End Sub
    p.s. Another way of speeding up the transition is to make the looping through the mask more efficient.
    Right now, the looping though the bitmap to modify the bits takes slightly longer than the time it takes to refresh the screen, so that is the current bottleneck.
    You can make that process about 200 times faster by making one pass through the bitmask whenever you load it, to create 256 linked lists. Each list represents the pixels of a given grayscale value (0 to 255).
    That way, when you want to copy all the pixels from one image to the other where the grayscale value is a particular value, you just traverse the list for that level, and copy the pixels specified.
    In the example, there are a little over 101 thousand pixels in the image. Rather than visit almost 26 million pixels for the complete transition (each pixel 256 times (256 * 101,376 = 25,952,256), you just copy the pixels designated in each of the list, so only "visit" each pixels once.

    The linked lists are very simple, as you don't need a link and a value, you just link the pixels to each other so the link is the value. That makes the storage the same size as the original bitmap (one link per pixel), plus an array of 256 head pointers for the lists, to find the first pixel in each linked list.
    If you're interested in that, I can update the example so show that. It isn't a lot of code, and not too much modification.
    Last edited by passel; Feb 2nd, 2015 at 12:31 AM.

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