Results 1 to 22 of 22

Thread: Read background color of a bitmap

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Read background color of a bitmap

    Hi,

    Let's say I have a bitmap star-shaped image sitting on the clipboard.

    I need to retrieve the color of the space surrounding the star image.

    image obtained from Clipboard-Viewer:
    Name:  image_2020-10-16_111820.png
Views: 548
Size:  2.3 KB

    I know I could read pixel (0,0) and get the color but the image on the clipboard changes and could be anything like a colored rectangle shape filling the entire bitmap space leaving no room for any background, in which case reading pixel(0,0) woudn't work.

    Based on a some of tests I carried out in a couple of different machines, the bitmap backcolor is not always the same. it changes from one machine to another... In the case of the star picture scrrenshot above, the system gave a white background in one system, but on other systems, the background color is greyish.

    So the question is:
    Is it possible to know the color that the system gives a bitmap background ?

    I have tried GetSysColor (passing different Constants) along with OleTranslateColor but no success.

    Thanks.

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

    Re: Read background color of a bitmap

    Quote Originally Posted by JAAFAR View Post
    So the question is:
    Is it possible to know the color that the system gives a bitmap background ?
    No, unless you know a pixel offset or know the 'content' of the bitmap.

    Otherwise if image content is unknown, then how would one define background color? You cannot just assume it is the most-used color. You cannot even assume a background applies.

    I'm not sure a "system" background color applies at all. Images with transparency are rendered over a background defined by the application. That background color might be hard-coded white, might be system's window-background color (maybe modified by a theme)
    Last edited by LaVolpe; Oct 16th, 2020 at 06:47 AM.
    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
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Read background color of a bitmap

    Further more, bitmaps don't support the background concept the way a GIF or PNG does where there is an alpha channel - which when set is what is used to provide a transparent background.
    There is no such thing as a "system background". A "background" color is simply the color that is "behind" all of the grphical elements in an image. And it can be any color.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Read background color of a bitmap

    Thanks.

    So there is no way to make transparent the "background" area of whatever bitmap happens to be currently sitting in the clipboard and without knowing the "background" color or if a "background" applies at all ?

    Am I out of luck ?

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

    Re: Read background color of a bitmap

    Quote Originally Posted by JAAFAR View Post
    Thanks.

    So there is no way to make transparent the "background" area of whatever bitmap happens to be currently sitting in the clipboard and without knowing the "background" color or if a "background" applies at all ?

    Am I out of luck ?
    A standard bitmap on the clipboard? Out of luck. Any color you choose, via pixel location or color-usage-count, is a guess at best. If you know the content of the bitmap, then you can theoretically figure out the color regardless of image scale. With other image formats (GIF, PNG, ICO, etc), it is possible to determine background color, indirectly via known transparency, from the format in many cases. But a standard bitmap? Nope.

    The typical solutions are:
    1) Guarantee a pre-established pixel location that will contain the color (i.e., top/left corner)
    2) Have user specify the pixel location that contains the color
    3) Have user specify the transparent color

    P.S. I don't consider a 32bpp ARGB/pARGB bitmap as standard.
    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}

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Read background color of a bitmap

    more suggestions:

    - I would look into the source, from where u get this clipart, is it really from a bmp or maybe from gif/png, maybe theres a better way of getting the picture?

    - if theres no way of getting more info from the source and you only have the clipboard, its now a question of "analyzing" it and try to make an as good possible guess.

    check all 4 corners + center and a few more positions, like 1/4 width + 1/4 height, 3/4 width + 1/4 height etc, to get 4 more spots.

    I would check those 5 points "inside" the rectangle. the one color that is most used, I would point that as "most likely object-color", if theres many colors, I would make an array (most likely based array).
    now I check the 4 corners, if theres a color that is "not" in the first spot of the array, if so, check the 4 corners and compare for the most used.
    so, not that complex algorithm, sure a bit of calculations and comparison, but since theres only 9 spots it should be easy enough to code.
    Last edited by baka; Oct 16th, 2020 at 09:35 AM.

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

    Re: Read background color of a bitmap

    without knowing if transparency can even be applied, this is kind of a question with no good answer.

    For example, look at this image:
    1) If black is "transparent", you see a white vase
    2) If white is "transparent", you see 2 black faces
    3) If no color is transparent, you get this black & white image
    Name:  illusion.png
Views: 492
Size:  5.0 KB
    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}

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Read background color of a bitmap

    yeah, theres no 100% accuracy here. only a "as good possible" guess.
    the algorithm to figure out the background color could be made sensible, and it need to be sure or it will make a "default" color, like black background or the most used color (that is made solid, so transparency=disabled)
    I would go with a mid-grade algorithm that needs a "majority" color.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Read background color of a bitmap

    Yeah, you have no idea what the source's intent was. Bitmaps have nothing in them at all to indicate a background color.

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Read background color of a bitmap

    The question is: what are those figures taken from?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Read background color of a bitmap

    Thanks guys for all suggestions.

    @LaVolpe
    With other image formats (GIF, PNG, ICO, etc), it is possible to determine background color, indirectly via known transparency, from the format in many cases.
    Looking in the clipboard viewer, I can see that clipboard has PNG data along with BITMAP data - Can one query the clipboard for the PNG format and retrieve the transparency from there ?

    PNG format from clipboard viewer:
    Attachment 179041

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Read background color of a bitmap

    No.

    PNG doesn't carry any "background color."

    Even transparent pixels can be a mix of colors.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Read background color of a bitmap

    Quote Originally Posted by dilettante View Post
    No.

    PNG doesn't carry any "background color."

    Even transparent pixels can be a mix of colors.
    I thought the following comment meant there could be a possibility.. I must have misunderstood:
    With other image formats (GIF, PNG, ICO, etc), it is possible to determine background color, indirectly via known transparency, from the format in many cases.

    Thanks.

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

    Re: Read background color of a bitmap

    Quote Originally Posted by JAAFAR View Post
    Looking in the clipboard viewer, I can see that clipboard has PNG data along with BITMAP data - Can one query the clipboard for the PNG format and retrieve the transparency from there ?
    With image formats that support transparency, you would parse the format to determine a specific pixel location that is 100% transparent. Then you would locate that pixel position in the rendered image (scaled as needed) to get the color from that location. Then that should be the color used for transparency.

    P.S. PNG does have a background color chunk. It is optional.
    The bKGD chunk specifies a default background colour to present the image against
    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}

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Read background color of a bitmap

    What is the goal? Pasting a PNG from the clipboard with transparency is simple enough.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Read background color of a bitmap

    Quote Originally Posted by dilettante View Post
    What is the goal? Pasting a PNG from the clipboard with transparency is simple enough.
    Can you point me to some generic parsing code example so I can adapt it to my specific needs ... This is new territory to me.

    Thanks

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

    Re: Read background color of a bitmap

    As dilettante asked, what is the goal?

    For example, "PNG" clipboard format is not standard from what I've read. In other words, there is no guarantee it will exist and no guarantee if it exists, what the format of the data will be (since it isn't standard).

    edited: PNG parsing? Here's a start maybe
    https://www.vbforums.com/showthread....NG-Writer-v2-0
    Last edited by LaVolpe; Oct 16th, 2020 at 11:41 AM.
    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
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Read background color of a bitmap

    I just read this thread, and I've got the same questions, primarily two:

    1. What's the goal? If you outline what you're actually trying to do, people can probably come up with ideas to get you there.
    2. What exactly is meant by background? In a very real sense, there is no background color. What if a GIF with lots of transparency is displayed over a very colorful image. So, that colorful image is the background. Which color of that colorful image would we consider the "background color"???

    I suppose a GIF has an encoded background color, but that's not at all necessarily what you'll see in the transparent areas when the GIF is displayed.

    Also, as LaVolpe and others pointed out, if we're talking about something like a PNG or TGA, it even gets more complex as these image types have an alpha channel which can make any specific pixel in the image either opaque, transparent, or any degree of translucency we want. So, let's say we have a pixel that has 50% translucency. When this is shown over some "background", the color for that pixel will be a blend of the background and the PNG pixel. And that doesn't even consider the case where the background image is "colorful".

    So, the idea of a "single background color" is truly rather nonsensical.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: Read background color of a bitmap

    Quote Originally Posted by LaVolpe View Post
    As dilettante asked, what is the goal?
    I am just trying to copy excel spreadsheet shapes to the clipboard and then create a StdPicture from the data in the clipboard (in this case PNG data) and finally, load the StdPicture into an image activeX control in a form -- When the copied shape has an irregular form such as the red star shape, I get this white "background" in the image control which I want to get rid of by making that white area fully transparent.

    edited: PNG parsing? Here's a start maybe
    https://www.vbforums.com/showthread....NG-Writer-v2-0
    I run the pvReadPngChunks routine on the saved PNG file and this is the output I get:
    Processing C:\X\star2.png
    IHDR(13)
    sRGB(1)
    gAMA(4)
    pHYs(9)
    IDAT(1398)
    IEND(0)
    Done...

    I don't see the bKGD chunk in the output above... Does that mean the PNG file doesn't contain transparency ?

    This subject looks rather dautning to me. Maybe I should read more on the subject.

    Thanks.

  20. #20
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Read background color of a bitmap

    You can try the AlphaBlendImage control: https://github.com/wqweto/AlphaBlend...BlendImage.ctl

    It has Gdip/WicLoadPictureArray functions that can load PNG byte-array from clipboard to a StdPicture while preserving alpha transparency. The problem is that few controls support alpha channel on StdPictures (VB.Image does not) but fortunately the very purpose of existence of the AlphaBlendImage control is to display correctly such transparent images in StdPictures.

  21. #21
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Read background color of a bitmap

    How about backing up and simplifying? Let the user pick the transparent color on paste?

    Use the menu to copy a picture to the clipboard. Right-click in the Form client area to paste after answering the dialog.

    Name:  sshot.png
Views: 490
Size:  4.9 KB

    No idea whether this is helpful in Excel, or even if it is whether it might get you near your goal. I'm not even sure you can manipulate a VBA UserForm enough for such a dialog.
    Attached Files Attached Files
    Last edited by dilettante; Oct 16th, 2020 at 10:17 PM.

  22. #22
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Read background color of a bitmap

    Quote Originally Posted by JAAFAR View Post
    I am just trying to copy excel spreadsheet shapes to the clipboard and then create a StdPicture from the data in the clipboard (in this case PNG data) and finally, load the StdPicture into an image activeX control in a form -- When the copied shape has an irregular form such as the red star shape, I get this white "background" in the image control which I want to get rid of by making that white area fully transparent..
    I'm not sure if you're talking about "Shapes" or "Charts". But, in a sense, I guess it doesn't matter. Either of those copied to the clipboard will have one or more "image" types in the clipboard, such that it can be pasted to a paint-type program.

    And I now understand what you mean by "Background". But, I don't think those images (copied from Office "Shapes" or "Charts") actually have backgrounds. They just have the "BackColor" of whatever Excel tab (or Word page color, or whatever) they were drawn on. Again, no true background.

    But, this does open up possibilities. The first is, if this is just a onesie-twosie thing, I'd just do it manually. I'd paste the image into something like PaintShopPro (which is what I often use), specify the background color, and then turn it on to see what it looks like. Doing it this way reduces your color range to 256 colors, and is best saved as a GIF, but it'll certainly work.

    Another option, still using PaintShopPro (or any other Paint-like software that can do masks) is to create a mask (which is easiest done as a black-and-white image). This would be a second (copy) of the original image where you manipulated things where everything was white that needed to show, and the background was black. Then, you save this "mask" into the "alpha channel" of the original image, and then save it as either a PNG or TGA image. This has the advantage of still allowing a complete TrueColor range of colors.

    Both of the above do have the criteria that you can't use your BackColor anywhere except for where you want it to be transparent. For instance, if we're talking about an Excel Chart, you'd need to make sure that certain internal areas of the Chart had BackColors different from the overall Excel BackColor.

    -------

    And one more option that comes to mind. Depending on what you're actually doing with Excel, all these BackColors (not to be confused with an alpha (or GIF) background), can be fetched via the VBA. (Actually, they can also be fetched via Office automation from VB6.) If you're doing something quite specific, you might fetch those BackColors, and then create either a GIF or maybe a PNG or TGA, and create the background from that BackColor. That option is going to require a substantial amount of work though.

    -------

    So, bottom line, you're confounding the concept of BackColor (which can be nested and have several values) with the concept of a transparent background.

    -------

    Maybe That'll Help,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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