Results 1 to 20 of 20

Thread: How To Make A Transparent GIF With A Transparent Background?

  1. #1

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question How To Make A Transparent GIF With A Transparent Background?

    Hi, guys
    I'm trying to make nice form with a custom picture I designed. Since the picture isn't rectangular i have to use transparency so it doesn't stay ugly. If I just set the transparent GIF as a background the transparent section of the gif will show the form background (red) and not the desktop (or whatever is behind the app).

    The form in designer mode:



    The form in execution looks like this:


    [IMG]
    http://img407.imageshack.us/img407/6987/immaginedv5.th.jpg[/IMG]


    Thanks everybody...

    Andrea

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    What you are asking is pretty advanced stuff. There are basically two ways to do this. Both require use of APIs.

    1) For Win2K & above, you can use the SetLayeredWindowAttributes & UpdateLayeredWindow APIs

    2) For all operating systems, you can create a window region using the various region APIs: CreateRectRgn, CombineRgn & SetWindowRgn. The region will probably include your entire form minus the "transparent" area of the GIF.

    Search the forums for terms similar to "shaping forms" and "translucent windows".

    An example I created (using method 2 above) can be found on psc.com

    Edited: Be forewarned. Once you start shaping your form and remove things like menubars, titlebars, borders, etc, you will have to add a lot more code to allow the form to resize, move, allow menus and more. Custom shaped windows are not easy.
    Last edited by LaVolpe; Jan 14th, 2009 at 05:22 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: How To Make A Transparent GIF With A Transparent Background?

    lol, If you do search you might well find links posted by myself to one of LaVolpes projects on PSC.

    Maybe someone else can confirm this but I've found I seem to get much better performance using Regions rather than SetLayeredWindowAttributes. SetLayeredWindowAttributes however can be used for fancier things.

  4. #4

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Hi guys, thank you both!

    I tested the LaVolpe's solution but I obtained this:

    [IMG][/IMG]

    The problem is that the background of the form under the gif image doesn't change! Also, when a make transparent the form, became transparent the images and the others controls!

    Can you help me more?

    P.S. I have already searched everywhere in the web but I find nothing!

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: How To Make A Transparent GIF With A Transparent Background?

    You need to apply the region to the form not the image, you also don't need to use a Gif if you don't want to. The region is created from a colour (presumably the top left pixel)

    The attached loads a region (as created by Lavolpe's class) from a resource and applies it to the form.
    Attached Files Attached Files

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    As Milk suggested. Do not use a gif, use a bitmap. Bring your gif into Paint and fill the top/transparent color with a color not otherwise in the image, maybe magenta?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Thank you Milk, but I need some explanation... I'm new to VB... ;-)
    Your project works well !

    You put your image as form background, isn't it?
    The image must be in BMP, JPEG, GIF format?
    You add "clsRegions" Module to your project, isn't it?

    How can I make the "trans.RES"?
    How can you made the background transparent?

    Thank you again, you're saving my job!

  8. #8

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Re: How To Make A Transparent GIF With A Transparent Background?

    Yes, I've tried with a bitmap too... but the result is the same. :'(
    The image color that I want to make transparent is disappeared, but
    instead of be totally transparent I see the form background color.

    Thank you too

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    Use BMP, not JPG. Using regions to shape a form requires a consistent color to be considered "transparent". JPG compresses colors so if your transparent color was Red, it may look red to your eyes, but its color value in JPGs could be reddish, not red. If you are going to use GIF, then don't use Transparent GIFs.

    Can you post your GIF/image here on this site? We can apply it to Milk's region.zip example.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Quote Originally Posted by LaVolpe
    Use BMP, not JPG. Using regions to shape a form requires a consistent color to be considered "transparent". JPG compresses colors so if your transparent color was Red, it may look red to your eyes, but its color value in JPGs could be reddish, not red. If you are going to use GIF, then don't use Transparent GIFs.

    Can you post your GIF/image here on this site? We can apply it to Milk's region.zip example.
    Ok.
    This attach contain the BMP, GIF, GIF with transparent images:
    Images.zip

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    Ok, simple enough
    1. Using Milk's regions.zip project
    2. Add your bitmap to the form's Picture property
    3. Resize the form as needed so that the form does not go beyond any pink/magenta area of the bitmap
    4. Tweak the code in the form to look like this and then run the project
    Code:
    Option Explicit
    
    Private Sub Form_Click()
       Unload Me
    End Sub
    
    Private Sub Form_Load()
    Dim cRgn As clsRegions, hRgn As Long
       Set cRgn = New clsRegions
       'hRgn = cRgn.ImportRegion(rgn_FromResource, "Custom", 101)
       hRgn = cRgn.RegionFromBitmap(Me.Picture, Me.hWnd)
       'cRgn.SetRegionToWindow hRgn, Me.hWnd
    End Sub
    Note: That clsRegions class contains much more region functions that you will probably use. Feel free to remove functions that you will not need. Leaving those functions in does no harm, only makes your project a wee bit larger.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Quote Originally Posted by LaVolpe
    Ok, simple enough
    1. Using Milk's regions.zip project
    2. Add your bitmap to the form's Picture property
    3. Resize the form as needed so that the form does not go beyond any pink/magenta area of the bitmap
    4. Tweak the code in the form to look like this and then run the project
    Code:
    Option Explicit
    
    Private Sub Form_Click()
       Unload Me
    End Sub
    
    Private Sub Form_Load()
    Dim cRgn As clsRegions, hRgn As Long
       Set cRgn = New clsRegions
       'hRgn = cRgn.ImportRegion(rgn_FromResource, "Custom", 101)
       hRgn = cRgn.RegionFromBitmap(Me.Picture, Me.hWnd)
       'cRgn.SetRegionToWindow hRgn, Me.hWnd
    End Sub
    Note: That clsRegions class contains much more region functions that you will probably use. Feel free to remove functions that you will not need. Leaving those functions in does no harm, only makes your project a wee bit larger.
    Ok, using Milk's regions.zip project all works well but when I put into my
    project I obtain this:


    using this form in design-time:


    Why so???

  13. #13
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    Did you use the bitmap in your project or are you still using the transparent GIF in the form's Picture property? Additionally, you will want to resize your form to the exact dimensions of the bitmap. How now?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #14

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Quote Originally Posted by LaVolpe
    Did you use the bitmap in your project or are you still using the transparent GIF in the form's Picture property? Additionally, you will want to resize your form to the exact dimensions of the bitmap. How now?
    Yes I've used the BMP file, and now I've resize the form, but nothing change.
    The only thing that I don't have included in my project is the "trans.RES" file. It's necessary?

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    I think I know what the problem is. Change your form's BorderStyle=0 (no borders). Duh, I should have noticed that. You might also want to compare your other form properties against Milk's sample form.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  16. #16

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Question Re: How To Make A Transparent GIF With A Transparent Background?

    Quote Originally Posted by LaVolpe
    I think I know what the problem is. Change your form's BorderStyle=0 (no borders). Duh, I should have noticed that. You might also want to compare your other form properties against Milk's sample form.
    I've already changed the BorderStyle to 0. I've compered the 2 forms proprierties but even if I made some changes the result is the same!
    I'm unhopeful

  17. #17
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How To Make A Transparent GIF With A Transparent Background?

    Can you copy your form, remove all code except the region stuff & zip it & post it so we can take a look see.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  18. #18
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: How To Make A Transparent GIF With A Transparent Background?

    I'm guessing perhaps you have not loaded the bitmap into the Forms Picture property, but you are using a PictureBox or some such.

    Here's the project again with your image, using LaVolpes class in the way he suggested earlier.

    Double click to close this time. Click and drag to move the window.
    Attached Files Attached Files

  19. #19

    Thread Starter
    Lively Member saturnus83's Avatar
    Join Date
    Jan 2009
    Location
    Mantova, Italy
    Posts
    78

    Resolved Re: How To Make A Transparent GIF With A Transparent Background?

    Quote Originally Posted by LaVolpe
    Can you copy your form, remove all code except the region stuff & zip it & post it so we can take a look see.
    What you asked me is no more necessary...
    Because when I created a new project to send you, this one works very well. So, I decide to delete my old form and crete a new one, and now all works!
    I don't now how... but works! STRANGENESS OF PROGRAMMING !!!!

    Thanks everyone!!!!
    You're very kind!!!

  20. #20
    Hyperactive Member Darkened Linux's Avatar
    Join Date
    Jun 2009
    Location
    Canada
    Posts
    296

    Re: How To Make A Transparent GIF With A Transparent Background?

    I had the same problem and I just found out why. You need to use Adobe Photoshop. When you save you should save your image as a .gif. Then it prompted me "Exact Black and White?". I entered yes. I put the image into a ImageBoxb in VB. I also made the tranceparency white and the background white. Then it should worked, it worked for me!
    [COLOR="Black"]

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