Rank: Member
Groups: Joined: 3/23/2006(UTC) Posts: 17
|
How do I go about changing a transparant images color without doing anything to the transparent part?
Thank you.
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
If you are working with full-color pixels, you can use Transparentize method. If you are working with indexed pixel formats, you can just set appropriate palette entry to transparent color. The only caveat - GIF format supports only one transparent palette entry. So if you save the result to GIF, you should have only one transparent element in the palette.
|
|
|
|
Rank: Member
Groups: Joined: 3/23/2006(UTC) Posts: 17
|
Thanks. Can you supply sample code please?
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Let's assume that you want to make white color transparent. In this case call (for full-color images): Code:objBitmap.Channels.Transparentize &hFFFFFFFF
or (for indexed bitmaps): Code:objBitmap.Palette.SetTransparentColor &hFFFFFFFF
If you know that white color is, say, 24th item of the palette, you can use SetTransparentIndex instead to speed it up. Just pass item index instead of color value (note, index is zero-based).
|
|
|
|
Rank: Member
Groups: Joined: 3/23/2006(UTC) Posts: 17
|
Hi Andrew,
How can I change the gif image color? The whole image but leave the transparent part transparent.
Thanks
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Let's assume you load GIF file. You will get a Bitmap object which has 8-bit indexed pixel format (GIF images are always stored that way). It means the following: - objBitmap.Data.PixelFormat will equal to Format8bppIndexed.
- The bitmap will have the palette initialized. The palette stores colors used in this image. Each color is identified with an index (a number in a range [0, color count -1]). So the palette can be considered as a table of colors.
- Pixels of bitmap stores indices of colors in palette instead of colors.
E.g. let's assume that we have 4-color image with the following palette: 0 - white 1 - red 2 - green 3 - blue Pixels of bitmap will be looking like this: 0 1 0 0 2 3 1 ... instead of [white] [red] [white] [white] [green] [blue] [red] ... This way, to change some color of this image it is enough to change a color value in the palette rather than modify pixels. For example, you want to replace blue color by yellow. The code will be looking like this: Code:' ...
Dim intBlueIndex As Long
intBlueIndex = objBitmap.Data.Palette.FindNearestIndex(ColorBlue)
objBitmap.Data.Palette(intBlueIndex) = ColorYellow
Procedure of searching of nearest color in palette is quite slow, that's why if you know the index of the necessary color, you can skip FindNearestIndex call. Of course, it will not affect other entries of the palette. So if it contains any transparent elements, they will not be changed and transparency will be preserved. P.S. Keep in mind, it is true only if you work with indexed pixel format. If you convert it to some 24-bit or 32-bit pixel format (which implies that each pixel stores direct color value instead of an index of palette entry), all mentioned above will not work. Edited by user Thursday, December 20, 2007 4:58:37 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Member
Groups: Joined: 3/23/2006(UTC) Posts: 17
|
So what you are saying is that I can not change the image in one go?
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Sorry, I did not quite understand the question. What you mean saying "can not change the image in one go"? What you expect to get?
|
|
|
|
Rank: Member
Groups: Joined: 3/23/2006(UTC) Posts: 17
|
I would like to change the whole image's color without affecting the transparent part. I want to do this in one go.
Something like image.color = green
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
So you mean the same color modification as described in your previous post? See my answer there. However it will require to convert to 32-bit ARGB.
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.