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



Go to last post Go to first unread
#1 Posted : Friday, May 19, 2006 7:07:42 AM(UTC)

Rank: Member

Groups: Member
Joined: 9/13/2004(UTC)
Posts: 4

I need to take a black and white image that is uploaded and convert it to red and white, or blue and white, etc. The color will be one of a few (5 or 6) and I need to do this basically without user intervention. Is there an easy way to process the image to apply the color in place of the black in the image? The interface I have is so incredibly simple that the user is just "approving" uploaded images for copyright and objectional material, so I want to apply the color in VB6 code in the background when I open the file. Then it will be saved for printing with the color instead of the black. And ideas or help would be greatly appreciated?
#2 Posted : Friday, May 19, 2006 11:43:35 AM(UTC)

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 865

Thanks: 2 times
Was thanked: 25 time(s) in 25 post(s)
If you work with indexed image (1-bit, 4-bit, or non-grayscale 8-bit), you can just replace an appropriate color in the palette. Even if you use 24-bit RGB bitmaps, you can convert them to 8-bit indexed bitmap. Since you said that actually the image contains only few colors, it will not make any problems (moreover, it will save some memory).
Dim objBmp As New Bitmap
objBmp.LoadFromFile "c:\image.bmp"

If Not objBmp.Data.IsIndexed Then
   objBmp.Data.ConvertTo8bppIndexed DitheringTypeNone
End If 

Dim intBlackColorIndex As Integer
intBlackColorIndex = objBmp.Data.Palette.FindNearestIndex(ColorBlack)
objBmp.Data.Palette(intBlackColorIndex) = ColorBlue

objBmp.SaveToFile "c:\result.bmp"
FindNearestIndex method works quite slowly (which is noticeable for large palettes). If you are sure that the color you replace is presented in the bitmap, you can just iterate the palette entries and replace an appropriate ones:
Dim I As Integer
For I = 0 To objBmp.Data.Palette.Count - 1
  If objBmp.Data.Palette(I) = ColorBlack Then
    objBmp.Data.Palette(I) = ColorBlue
  End If
Hope this helps.

Edited by user Thursday, December 20, 2007 4:33:13 PM(UTC)  | Reason: Not specified

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.