Results 1 to 32 of 32

Thread: Image Transparency (URGENT PLEASE HELP)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Image Transparency (URGENT PLEASE HELP)

    Hey,
    I am working on building this chess program. I just started and I had a little problem. Well I have images of pieces that will be moved around the board. The problem is that pieces can be moved from a dark square to a light square or vice versa. So the background colour of the image cannot remain same.

    The background colour is going to be white... So is there any way that the background colour of the image can be made transparent. Also is it possible that we can set the white colour to be transparent or the colour that most occures in the image (i.e. white) to be transparent.

    Please help me out or else I will have to do twice the work by making pieces with two types of background colours and also have to check which square the piece is on.

    Any help would be appreciated.

    Khanjan
    Last edited by khanjan_a2k; Aug 16th, 2006 at 06:22 PM.
    Hey... If you found this post helpful please rate it.

  2. #2
    New Member
    Join Date
    Aug 2006
    Posts
    6

    Re: Image Transparency

    edit your chess peice in Abode Photoshop. Click file and new then check the set transparent box. Then open your image and select the part of the image you want to retain using the magic wand tool. then click edit and copy. click on the 1st one you made(the one with a chessboard like background, gray and white checkered background) and click edit and paste. Then save it as gif. On vb, create an image box and put your gif image there.

  3. #3
    Member
    Join Date
    Apr 2006
    Location
    Serbia
    Posts
    32

    Re: Image Transparency

    the easiest way is to make pieces as transparent gifs and load them in image controls, but thats is not good solution (flickering may and probably will appear)

    i suggest you to use sprites, you can find usefull code at: http://www.vbaccelerator.com/home/VB...ry/article.asp

  4. #4
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency

    Search for sprite examples/tutorials.. Transparency, even full blown alphablending in any image can be achieved directly with vb code examples

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency

    Well first of all... carlandrewg the problem is that I don't have Adobe Photoshop and I am not good at it. And is it possible that Painst Shop Photo Album 4 or Dell Pictur Studio will help??

    And I do not undertsand your process quite clearly. I am sorry but could you explain clearly. In the meantime I will try to get some help from sprites

    Khanjan
    Hey... If you found this post helpful please rate it.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Wink Re: Image Transparency

    VB Code:
    1. Public Sub pDrawTransparentBitmap(lHDCDest As Long, lBmSource As Long, _
    2.         lMaskColor As Long, Optional lDestStartX As Long, _
    3.         Optional lDestStartY As Long, Optional lDestWidth As Long, _
    4.         Optional lDestHeight As Long, Optional lSrcStartX As Long, _
    5.         Optional lSrcStartY As Long, Optional BkGrndHdc As Long)
    6.  
    7. ' Draw a sprite onto a picture.  The background of
    8. ' the sprite is made transparent so the pictures
    9. ' shows through.
    10.  
    11. Dim lColorRef    As Long
    12. Dim lBmAndBack   As Long
    13. Dim lBmAndObject As Long
    14. Dim lBmAndMem    As Long
    15. Dim lBmSave      As Long
    16. Dim lBmBackOld   As Long
    17. Dim lBmObjectOld As Long
    18. Dim lBmMemOld    As Long
    19. Dim lBmSaveOld   As Long
    20. Dim lHDCMem      As Long
    21. Dim lHDCBack     As Long
    22. Dim lHDCObject   As Long
    23. Dim lHDCTemp     As Long
    24. Dim lHDCSave     As Long
    25. Dim X            As Long
    26. Dim Y            As Long
    27. Dim udtBitMap    As BITMAP
    28. Dim udtSize      As POINTAPI
    29.  
    30. '
    31. ' Create a temporary Device Context compatible with
    32. ' the destination DC (picturebox's DC).
    33.  
    34. lHDCTemp = CreateCompatibleDC(lHDCDest)
    35.  
    36. ' Select the sprite's bitmap into the temporary DC.
    37.  
    38. Call SelectObject(lHDCTemp, lBmSource)
    39.  
    40. ' Store the sprite bitmap's characteristics in
    41. ' the udtBitMap.
    42.  
    43. Call GetObject(lBmSource, Len(udtBitMap), udtBitMap)
    44.  
    45. ' Set the size of the temporary bitmap.
    46.  
    47. With udtSize
    48.     .X = udtBitMap.bmWidth
    49.     .Y = udtBitMap.bmHeight
    50.    
    51.     ' Use the optionally passed in width and height values.
    52.    
    53.     If lDestWidth <> 0 Then .X = lDestWidth
    54.     If lDestHeight <> 0 Then .Y = lDestHeight
    55.     X = .X
    56.     Y = .Y
    57. End With
    58.  
    59. ' Create some DCs compatible with
    60. ' the picture to hold temporary data.
    61.  
    62. ' This creates one pixel by one pixel
    63.  
    64. lHDCBack = CreateCompatibleDC(lHDCDest)
    65. lHDCObject = CreateCompatibleDC(lHDCDest)
    66. lHDCMem = CreateCompatibleDC(lHDCDest)
    67. lHDCSave = CreateCompatibleDC(lHDCDest)
    68.  
    69. ' Create a bitmap for each DC.  DCs are required
    70. ' for a number of GDI functions.
    71.  
    72. ' Monochrome bitmaps.
    73.  
    74. lBmAndBack = CreateBitmap(X, Y, 1&, 1&, 0&)
    75. lBmAndObject = CreateBitmap(X, Y, 1&, 1&, 0&)
    76.  
    77. ' Color Compatible bitmaps.
    78.  
    79. lBmAndMem = CreateCompatibleBitmap(lHDCDest, X, Y)
    80. lBmSave = CreateCompatibleBitmap(lHDCDest, X, Y)
    81.  
    82. ' Each DC must select a bitmap object to store pixel data.
    83.  
    84. ' Monochrome
    85.  
    86. lBmBackOld = SelectObject(lHDCBack, lBmAndBack)
    87. lBmObjectOld = SelectObject(lHDCObject, lBmAndObject)
    88.  
    89. ' Color
    90.  
    91. lBmMemOld = SelectObject(lHDCMem, lBmAndMem)
    92. lBmSaveOld = SelectObject(lHDCSave, lBmSave)
    93.  
    94. ' Set the mapping mode of the temporary (sprite)
    95. ' DC to that of the picture's DC. The mapping mode
    96. ' defines the unit of measure used to transform
    97. ' page-space units into device-space units, and
    98. ' also defines the orientation of the device's
    99. ' x and y axes.
    100.  
    101. Call SetMapMode(lHDCTemp, GetMapMode(lHDCDest))
    102.  
    103. ' Save the sprite bitmap that was passed in
    104. ' because it will be overwritten.
    105.  
    106. Call BitBlt(lHDCSave, 0&, 0&, X, Y, lHDCSave, lSrcStartX, lSrcStartY, vbSrcCopy)
    107.  
    108. ' Set the background color of the sprite's DC to
    109. ' the color in the sprite that should be transparent.
    110.  
    111. ' Background color of the DC to MaskColor
    112.  
    113. lColorRef = SetBkColor(lHDCTemp, lMaskColor)
    114.  
    115. ' Create a mask for the sprite by performing a BitBlt from
    116. ' the sprite's bitmap to a monochrome bitmap.  The result is
    117. ' a matrix of 1's and 0's where 0 represents the foreground
    118. ' color and 1 represents the the background color.
    119.  
    120. Call BitBlt(lHDCObject, 0&, 0&, X, Y, lHDCTemp, lSrcStartX, lSrcStartY, vbSrcCopy)
    121.  
    122. ' Set the background color of the sprite's
    123. ' DC back to its original color.
    124.  
    125. Call SetBkColor(lHDCTemp, lColorRef)
    126.  
    127. ' Create the inverse of the mask.
    128.  
    129. Call BitBlt(lHDCBack, 0&, 0&, X, Y, lHDCObject, 0&, 0&, vbNotSrcCopy)
    130.  
    131. ' Copy the background of the main DC to the destination.
    132.  
    133. If (BkGrndHdc = 0) Then
    134.     Call BitBlt(lHDCMem, 0&, 0&, X, Y, lHDCDest, lDestStartX, lDestStartY, vbSrcCopy)
    135. Else
    136.     Call BitBlt(lHDCMem, 0&, 0&, X, Y, BkGrndHdc, lDestStartX, lDestStartY, vbSrcCopy)
    137. End If
    138.  
    139. ' Mask out the places where the bitmap will be placed by AND-ing
    140. ' the memory DC with the mask with the 0's where the foreground is.
    141.  
    142. Call BitBlt(lHDCMem, 0&, 0&, X, Y, lHDCObject, 0&, 0&, vbSrcAnd)
    143.  
    144. ' Mask out the transparent colored pixels on the bitmap. The
    145. ' background of the sprite is masked out so only the foreground remains.
    146. ' lHDCTemp is the colored sprite.  This is AND-ed with the mask
    147. ' which has the 1's where the foreground is and 0's where the background is.
    148.  
    149. Call BitBlt(lHDCTemp, lSrcStartX, lSrcStartY, X, Y, lHDCBack, 0&, 0&, vbSrcAnd)
    150.  
    151. ' Combine the colored foreground of the sprite with the colored
    152. ' background image by OR-ing the bitmap in the prior step with
    153. ' that from two steps back.
    154.  
    155. Call BitBlt(lHDCMem, 0&, 0&, X, Y, lHDCTemp, lSrcStartX, lSrcStartY, vbSrcPaint)
    156.  
    157. ' By varying lDestStartX and lDestStartY we can
    158. ' place the sprite in a different location on the
    159. ' background image (but now the sprite backgroud will be off).
    160.  
    161. Call BitBlt(lHDCDest, lDestStartX, lDestStartY, X, Y, lHDCMem, 0&, 0&, vbSrcCopy)
    162. 'Call BitBlt(lHDCDest, 0, 0, x, y, lHDCMem, 0&, 0&, vbSrcCopy)
    163.  
    164. ' Place the original sprite bitmap back into the bitmap sent here.
    165.  
    166. Call BitBlt(lHDCTemp, lDestStartX, lDestStartY, X, Y, lHDCSave, 0&, 0&, vbSrcCopy)
    167.  
    168. ' Delete memory bitmaps.
    169.  
    170. DeleteObject SelectObject(lHDCBack, lBmBackOld)
    171. DeleteObject SelectObject(lHDCObject, lBmObjectOld)
    172. DeleteObject SelectObject(lHDCMem, lBmMemOld)
    173. DeleteObject SelectObject(lHDCSave, lBmSaveOld)
    174.  
    175. ' Delete memory DC's
    176.  
    177. DeleteDC lHDCMem
    178. DeleteDC lHDCBack
    179. DeleteDC lHDCObject
    180. DeleteDC lHDCSave
    181. DeleteDC lHDCTemp
    182.  
    183. End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency

    Hey,

    the easiest way is to make pieces as transparent gifs and load them in image controls, but thats is not good solution (flickering may and probably will appear)

    i suggest you to use sprites, you can find usefull code at: http://www.vbaccelerator.com/home/V...ary/article.asp
    krckoorascic I did try to look at the website that you gave me however it just gave me some knowledge on how to create sprints and masks but not how to use them in Visual Basic. However while reading it I came across the TransparentBit and BitBlt Api. I looked them up in the Api Guide.

    The Transparent Bit Api may help me but the example they provided was with a Picture Box. I am using Images however I will try merging it onto a Image. Though, could you please confirm if it will work or not.

    The BitBlt Api just made a circular form however since the example is big I will try to look at it.

    Could someone explain what carlandrewg was trying to say because I quite did not understand him. Sorry!

    Triggernum5 I will be looking at your example.

    Thanx for your help guys

    Khanjan
    Hey... If you found this post helpful please rate it.

  8. #8
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency

    Its not an example, its a fullblown function you can just plug into your project after you declare a few api's etc.. Found it somewhere ages ago.. I don't know where I found it, and I'm not sure if there were ever any credientials attached to it, but the process is pretty much common knowledge today anyhow..

  9. #9
    Member
    Join Date
    Apr 2006
    Location
    Serbia
    Posts
    32

    Re: Image Transparency

    khanjan_a2k you cannot use gdi api functions on Image control cuz it doesn't have a hdc...

    search for memDC on vbaccelerator, that will help you (just create a memory dc on which you'll do all the drawings and when all pieces are drown you just copy that memdc to you form or picturebox, depending where you want to render gfx)

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency

    Thanx for your answer krkoorascic.

    Well is it possible to have the Picture box be transparent?

    What I am doing is that there is a board full of shapes (i.e Squares) either dark gray or light gray.

    Now the images of the pieces only has white and black colours or it so I dun need anything complicated for colours. I just want the white in black pieces and the black in white pieces to be transparent.

    triggernum5 your function also talks about using Picture Boxes. Also, I don't know what hdc and DC is...

    Khanjan
    Hey... If you found this post helpful please rate it.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency

    Apparently there is a bit of a memory leak with TransparentBlt() API.. The routine I posted gives the exact results cleanly.. You don't need the shapes to have an hdc since your form will have one.. Just tell pDrawTransparentBitmap where on the form containing your grid of shapes where you wish place the piece.
    -The background color of the piece picture is the color the function expects.
    -Load your piece pics into memory dc's or A memory dc using this class if you need it.. - http://www.vbaccelerator.com/home/Re...ection_cls.asp
    -Use form.hdc as destination
    -Memory dc as source

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency

    Well I don't get you... You are trying to say that I cann't use an Image because it doesn't have HDC.

    Well if you need any reference to the program I am attaching it with.

    I am trying to look at the memDC thing on vbaccelerator.

    Khanjan
    Attached Files Attached Files
    Hey... If you found this post helpful please rate it.

  13. #13
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (Please Help) :(

    Bingo.. A picturebox however does have an hdc.. Thats good enough since you don't need any do memory intensive graphics work..

    Couple of tips, Make the background color the same on both color sets.. Purple maybe.. Saves needing to modify your code to determine bg color.
    Make a single invisible picturebox to hold all your pieces that are stored in a single picture file then calculate where to copy from..

    It would even be possible to ditch all your shapes, start with ONLY a picture of a chess board with pieces (loaded from disk), and cut/paste the pieces and blank squares of proper color using pDrawTransparentBitmap

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (Please Help) :(

    I'm really sorry but I am not familiar with any of the things u said to me. If possible could u just do what u said to one piece and I will understand it and apply to the whole thing. Pleaseee

    Khanjan
    Hey... If you found this post helpful please rate it.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (Please Help) :(

    Instead of going through the process of making the background transparent, is is possible if I can only display the part with a specific colour?

    Khanjan
    Hey... If you found this post helpful please rate it.

  16. #16
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    A few things that will really simplify your program and cut down on controls..

    Use a single image as a background for anything that will not move.. Your board is designed most excellently, you could just take a screen snapshot of that and put it in a picturebox the size of your form.. This will prevent other objects from interfering with the picture placement that you are new at anyways.. The example I coded demonstrates this

    I have incorporated the cAlphadibSection class..
    This class example refers to 3 images for one piece:
    m_cImage = Your chess piece pic
    m_cMask = Transparency Mask (Alpha Channel)
    m_cAlphaImage = AlphaBlendedwithDest image
    This allows you to use your white set as masks, and allows you to take transparency to a WHOLE new level with shadows etc..

    Black Points in Mask are 100% transparent
    White Points are 100% opaque
    Anything in between is partially transparent if you include greys in the mask
    Attached Files Attached Files

  17. #17
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    Yes, it is possible to transfer only a single color.. Slightly modifying that pDrawTransparentBitmap sub will accomplish that.. RasterOp sequences can do basically anything if you understand boolean logic/arithmetic.. Thats a moot point now though, I'm just mentioning it so you know its an option in the future

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (URGENT PLEASE HELP)

    Thank you for the example triggernum5 but unfortunately I am at work (library) right now and the office computers do not have Visual Basic. However, I would just like to respond to your suggestion about just taking an image of the board.

    The reason I do not want to take an image of the chess board is because I want the user to be able to see the possible moves when the user moves his/her mouse over a piece. I would do this by just outlining the possible squares.

    However, on the other hand I can also leave the square as the are and also have an image. But all I can do is that make the squares look like the outlines and not have the grey in them. I can just turn the visible property to false. And when I need the possible squares to be visiable = True.

    Which one do you think will work better for me.

    I will look at the example you made for me once I reach home.

    Khanjan
    Hey... If you found this post helpful please rate it.

  19. #19
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    Its still very easily doable to write a sub that draws on the pic temporarily, using vb directly, or API to outline potential moves.. Or, if you wanted you could do both, simply keep the squares invisible unless you want to outline, I honestly don't know how much memory is consumed by keeping an array of redundant shapes.. Maybe I'll learn here..
    And if you haven't noticed by now, I like working directly with images.. I'd just make a pic/mask of an outlined square (with transparent or better yet, semitransparent bodyfill in its mask since some destinations will be occupied obviously).. Then you can alphapaint that onto as many squares as you need..
    The only reason not to work with images directly would be if you needed scalability on an image that didn't rescale well, but your board/set could all be scaled nicely without degradation if you ever implement that..

    BTW, if you haven't looked at the project I'm pretty sure you will pick up on how to use the cAlphaDibSection class easily, and that will explain all this crap I'm talking about

    PS, scraping the big picturebox idea, and just putting the bg image on the form itself works too.. Slightly different properties in each, so you may find you have a prference

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (URGENT PLEASE HELP)

    Yes you are right, I will need to take a look at the project you have given me. Unfortunately, I will not be able to look at it until the next few hours. Possibly 5-6 hours. Anyways, I will look at it and reply for any further questions.

    I appreciate your help.

    Khanjan
    Hey... If you found this post helpful please rate it.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (URGENT PLEASE HELP)

    In the sample program you gave me, you attached two extra files:

    cDibSectionmod.cls
    modChroma.bas

    Since I am trying to undertsand what you are doing, line by line, it would be better if you just briefly told me what these files are for since these files are not being used by the project example you gave me. If they are just attached by an accident, it would save me time. Thanx

    Khanjan
    Hey... If you found this post helpful please rate it.

  22. #22
    New Member
    Join Date
    Aug 2006
    Posts
    10

    Re: Image Transparency (URGENT PLEASE HELP)

    You don't need those if you use alphablending
    Last edited by MacroX; Aug 18th, 2006 at 02:03 AM.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (URGENT PLEASE HELP)

    Well I was using the example you gave me. However, when I change the co-ordinates of the edited Image to (280, 560) which is where the King is supposed to be, I can't see it. This is because my board comes in the middle. Any solution to this?

    Khanjan
    Hey... If you found this post helpful please rate it.

  24. #24
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Image Transparency (URGENT PLEASE HELP)

    An easier alternative (although obviously a waste of resources) is to have each image with two background colours (one for each square colour)...however, it would be a simple matter to make the images transparent using paint shop pro, photoshop or pretty much any image program these days. I did a quick search of google and found http://graphicssoft.about.com/cs/ima...eephotoedw.htm which gives you a list of 8 programs that are free...I would guess GIMP would be able to handle your requirements :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Image Transparency (URGENT PLEASE HELP)

    Thanx a lot Smux for your research. However I cannot use the easier alternative since I would be moving the pieces around. Anyways, I did some research on the pDrawTransparentBitmap method triggernum5 was talking about. I found an example that I used to create an example of my own. But I still do not know how I am going to be able to use it in my program. I have attached the example program I create and I also attached a credential since there wasn't one.

    Smux now that I can download the program I hope it will be much easier.

    Khanjan
    Attached Files Attached Files
    Hey... If you found this post helpful please rate it.

  26. #26
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Image Transparency (URGENT PLEASE HELP)

    I would say setting transparency with one of those programs will be the easiest option. If you can't figure out how to do it, I might be able to set transparency for you...put the images into a RAR/ZIP and send in a private message (through www.yousendit.com if you have to) and I'll set transparency and return the images the same way :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  27. #27
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    Its because the shapes are drawn over the form (where the piece is painted), which is the reason I suggested eliminating the shapes and row labels, and why I placed the piece where it was half covered in the example.. You can store an outlined square pic/mask in your set, and alphapaintpicture it wherever you need, as often as you need.. The listboxes you need for move lists, and the shapes bordering it affect nothing whether they are there, or just another part of your bg picture on the form itself.. But really if it was me I'd only have the list boxes and other functional controls (If you need to add them later) and everything else would be image oriented.. Basically like dragging/dropping an edit clip in MSPaint, except with more restrictions on where the piece can be placed, and if the location is valid, snap it to proper borders.. This way you will also be able to reuse the same code for more features like a captured pieces box because virtually every action will be simply dragging/dropping a pic..
    Edit: Actually if you have shapes bordering your list then even though its not a valid place to drop, the image will hide behind it while dragging..
    Last edited by triggernum5; Aug 18th, 2006 at 09:58 AM.

  28. #28
    Member
    Join Date
    Jul 2006
    Posts
    62

    Re: Image Transparency (URGENT PLEASE HELP)

    Why does the code suggested only work for small images? I have been trying to change the settings for an image that is 720x480, but the colors don't transfer like they should. Any ideas?

    Code:
    Public Sub pDrawTransparentBitmap(lHDCDest As Long, lBmSource As Long, _
    lMaskColor As Long, Optional lDestStartX As Long, _
    Optional lDestStartY As Long, Optional lDestWidth As Long, _
    Optional lDestHeight As Long, Optional lSrcStartX As Long, _
    Optional lSrcStartY As Long, Optional BkGrndHdc As Long)
    ' Draw a sprite onto a picture. The background of
    ' the sprite is made transparent so the pictures
    ' shows through.
    Dim lColorRef As Long
    Dim lBmAndBack As Long
    Dim lBmAndObject As Long
    Dim lBmAndMem As Long
    Dim lBmSave As Long
    Dim lBmBackOld As Long
    Dim lBmObjectOld As Long
    Dim lBmMemOld As Long
    Dim lBmSaveOld As Long
    Dim lHDCMem As Long
    Dim lHDCBack As Long
    Dim lHDCObject As Long
    Dim lHDCTemp As Long
    Dim lHDCSave As Long
    Dim x As Long
    Dim y As Long
    Dim udtBitMap As BITMAP
    Dim udtSize As POINTAPI
    '
    ' Create a temporary Device Context compatible with
    ' the destination DC (picturebox's DC).
    lHDCTemp = CreateCompatibleDC(lHDCDest)
    ' Select the sprite's bitmap into the temporary DC.
    Call SelectObject(lHDCTemp, lBmSource)
    ' Store the sprite bitmap's characteristics in
    ' the udtBitMap.
    Call GetObject(lBmSource, Len(udtBitMap), udtBitMap)
    ' Set the size of the temporary bitmap.
    With udtSize
    .x = udtBitMap.bmWidth
    .y = udtBitMap.bmHeight
    ' Use the optionally passed in width and height values.
    If lDestWidth <> 0 Then .x = lDestWidth
    If lDestHeight <> 0 Then .y = lDestHeight
    x = .x
    y = .y
    End With
    ' Create some DCs compatible with
    ' the picture to hold temporary data.
    ' This creates one pixel by one pixel
    lHDCBack = CreateCompatibleDC(lHDCDest)
    lHDCObject = CreateCompatibleDC(lHDCDest)
    lHDCMem = CreateCompatibleDC(lHDCDest)
    lHDCSave = CreateCompatibleDC(lHDCDest)
    ' Create a bitmap for each DC. DCs are required
    ' for a number of GDI functions.
    ' Monochrome bitmaps.
    lBmAndBack = CreateBitmap(x, y, 1&, 1&, 0&)
    lBmAndObject = CreateBitmap(x, y, 1&, 1&, 0&)
    ' Color Compatible bitmaps.
    lBmAndMem = CreateCompatibleBitmap(lHDCDest, x, y)
    lBmSave = CreateCompatibleBitmap(lHDCDest, x, y)
    ' Each DC must select a bitmap object to store pixel data.
    ' Monochrome
    lBmBackOld = SelectObject(lHDCBack, lBmAndBack)
    lBmObjectOld = SelectObject(lHDCObject, lBmAndObject)
    ' Color
    lBmMemOld = SelectObject(lHDCMem, lBmAndMem)
    lBmSaveOld = SelectObject(lHDCSave, lBmSave)
    ' Set the mapping mode of the temporary (sprite)
    ' DC to that of the picture's DC. The mapping mode
    ' defines the unit of measure used to transform
    ' page-space units into device-space units, and
    ' also defines the orientation of the device's
    ' x and y axes.
    Call SetMapMode(lHDCTemp, GetMapMode(lHDCDest))
    ' Save the sprite bitmap that was passed in
    ' because it will be overwritten.
    Call BitBlt(lHDCSave, 0&, 0&, x, y, lHDCSave, lSrcStartX, lSrcStartY, vbSrcCopy)
    ' Set the background color of the sprite's DC to
    ' the color in the sprite that should be transparent.
    ' Background color of the DC to MaskColor
    lColorRef = SetBkColor(lHDCTemp, lMaskColor)
    ' Create a mask for the sprite by performing a BitBlt from
    ' the sprite's bitmap to a monochrome bitmap. The result is
    ' a matrix of 1's and 0's where 0 represents the foreground
    ' color and 1 represents the the background color.
    Call BitBlt(lHDCObject, 0&, 0&, x, y, lHDCTemp, lSrcStartX, lSrcStartY, vbSrcCopy)
    ' Set the background color of the sprite's
    ' DC back to its original color.
    Call SetBkColor(lHDCTemp, lColorRef)
    ' Create the inverse of the mask.
    Call BitBlt(lHDCBack, 0&, 0&, x, y, lHDCObject, 0&, 0&, vbNotSrcCopy)
    ' Copy the background of the main DC to the destination.
    If (BkGrndHdc = 0) Then
    Call BitBlt(lHDCMem, 0&, 0&, x, y, lHDCDest, lDestStartX, lDestStartY, vbSrcCopy)
    Else
    Call BitBlt(lHDCMem, 0&, 0&, x, y, BkGrndHdc, lDestStartX, lDestStartY, vbSrcCopy)
    End If
    ' Mask out the places where the bitmap will be placed by AND-ing
    ' the memory DC with the mask with the 0's where the foreground is.
    Call BitBlt(lHDCMem, 0&, 0&, x, y, lHDCObject, 0&, 0&, vbSrcAnd)
    ' Mask out the transparent colored pixels on the bitmap. The
    ' background of the sprite is masked out so only the foreground remains.
    ' lHDCTemp is the colored sprite. This is AND-ed with the mask
    ' which has the 1's where the foreground is and 0's where the background is.
    Call BitBlt(lHDCTemp, lSrcStartX, lSrcStartY, x, y, lHDCBack, 0&, 0&, vbSrcAnd)
    ' Combine the colored foreground of the sprite with the colored
    ' background image by OR-ing the bitmap in the prior step with
    ' that from two steps back.
    Call BitBlt(lHDCMem, 0&, 0&, x, y, lHDCTemp, lSrcStartX, lSrcStartY, vbSrcPaint)
    ' By varying lDestStartX and lDestStartY we can
    ' place the sprite in a different location on the
    ' background image (but now the sprite backgroud will be off).
    Call BitBlt(lHDCDest, lDestStartX, lDestStartY, x, y, lHDCMem, 0&, 0&, vbSrcCopy)
    'Call BitBlt(lHDCDest, 0, 0, x, y, lHDCMem, 0&, 0&, vbSrcCopy)
    ' Place the original sprite bitmap back into the bitmap sent here.
    Call BitBlt(lHDCTemp, lDestStartX, lDestStartY, x, y, lHDCSave, 0&, 0&, vbSrcCopy)
    ' Delete memory bitmaps.
    DeleteObject SelectObject(lHDCBack, lBmBackOld)
    DeleteObject SelectObject(lHDCObject, lBmObjectOld)
    DeleteObject SelectObject(lHDCMem, lBmMemOld)
    DeleteObject SelectObject(lHDCSave, lBmSaveOld)
    ' Delete memory DC's
    DeleteDC lHDCMem
    DeleteDC lHDCBack
    DeleteDC lHDCObject
    DeleteDC lHDCSave
    DeleteDC lHDCTemp
    End Sub

  29. #29
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    Set some breakpoints in the sub before/after each step, create a temporary PBox with autoredraw enabled and use the debug window to BitBlt() the contents of the hdc's to the picbox.. That will show you exactly when the unexpected results occur.. Works with a 1000:1000 pixel image for me..

  30. #30
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Image Transparency (URGENT PLEASE HELP)

    Uuuuuh.... I am good with Photoshop


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  31. #31
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: Image Transparency (URGENT PLEASE HELP)

    Atleast post a screnshot with the actual pic, and the unexpected result..

  32. #32
    Member
    Join Date
    Jul 2006
    Posts
    62

    Re: Image Transparency (URGENT PLEASE HELP)

    here is what i am looking at. Basically the colors get faded out. the original image is 860x645. Thanks!
    Attached Images Attached Images  

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