Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
BarryP  
#1 Posted : Saturday, March 25, 2006 5:55:52 AM(UTC)
BarryP

Rank: Member

Groups: Member
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.
Andrew  
#2 Posted : Sunday, March 26, 2006 5:39:54 PM(UTC)
Andrew

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.
BarryP  
#3 Posted : Sunday, March 26, 2006 6:17:48 PM(UTC)
BarryP

Rank: Member

Groups: Member
Joined: 3/23/2006(UTC)
Posts: 17

Thanks. Can you supply sample code please?
Andrew  
#4 Posted : Sunday, April 2, 2006 12:55:36 PM(UTC)
Andrew

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).
BarryP  
#5 Posted : Sunday, April 2, 2006 7:32:11 PM(UTC)
BarryP

Rank: Member

Groups: Member
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
Andrew  
#6 Posted : Monday, April 3, 2006 2:21:48 PM(UTC)
Andrew

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:
  1. objBitmap.Data.PixelFormat will equal to Format8bppIndexed.
  2. 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.
  3. 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

BarryP  
#7 Posted : Monday, April 3, 2006 4:53:43 PM(UTC)
BarryP

Rank: Member

Groups: Member
Joined: 3/23/2006(UTC)
Posts: 17

So what you are saying is that I can not change the image in one go?
Andrew  
#8 Posted : Monday, April 3, 2006 5:23:30 PM(UTC)
Andrew

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?
BarryP  
#9 Posted : Monday, April 3, 2006 5:35:05 PM(UTC)
BarryP

Rank: Member

Groups: Member
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
Andrew  
#10 Posted : Wednesday, April 5, 2006 2:14:14 PM(UTC)
Andrew

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.
Users browsing this topic
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.