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:
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.
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"
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.
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 ?
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"
- 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.
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
Insomnia is just a byproduct of, "It can't be done"
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.
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 ?
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.
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"
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).
I just read this thread, and I've got the same questions, primarily two:
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.
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.
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 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.
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.
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.
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.
Last edited by dilettante; Oct 16th, 2020 at 10:17 PM.
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.