• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Bitmap and color

This is my puzzle:
How i can "colorize" my bitmap? I must turn color form black to another. What is the right code? Please help me!
0
pserj
Asked:
pserj
  • 5
  • 3
  • 2
1 Solution
 
intheCommented:
Hi,
here an example of changing pixel colors from red to blue using a loop on the pixels:


procedure TForm1.Button1Click(Sender: TObject);
 VAR
Bitmap: TBitmap;
 i,j : INTEGER;
 begin
  Bitmap := TBitmap.Create;
 TRY
Bitmap.LoadFromFile('C:\Program Files\Borland\Delphi 3\Images\Buttons\Alarm.BMP'); // Change "red" part of alarm to "blue"
 FOR j := 0 TO Bitmap.Height-1
 DO BEGIN
FOR i := 0 TO Bitmap.Width-1
 DO BEGIN
IF Bitmap.Canvas.Pixels[i,j] = clRed
THEN Bitmap.Canvas.Pixels[i,j] := clBlue
END
END;
 Bitmap.SaveToFile('C:\Temp\BlueAlarm.BMP');
 Image2.Picture.Graphic := Bitmap
 FINALLY
Bitmap.Free
END;
end;
0
 
mullet_attackCommented:
Hi pseri,

Couple of ways to do this.

first way is simple, but slow for all but the small bitmaps. Use the Bitmap's Pixels array to determine each pixel's color, then change it if it is black

for row := 0 to height do
  for col := 0 to width do
    if pixels[row,col]=clBlack then pixels[row,col] := clYourColor

if your bitmap is black and white only, you could do this:

create another bitmap of the same size, fillrect it clYourColor. Then Copyrect the new bitmap to your bitmap with the CopyMode set to cmSrcPaint. All black stuff will be clYourColor, and white will be unchanged.

if your bitmap is black and multicolor, then :

create new bitmap, exact copy of yours. convert it to a monochrome bitmap, then back to a color bitmap.(will now be black and white only) Create another bitmap, same size and fillrect it with clYourColor. CopyRect this bitmap
onto the B & W bitmap using cmSrcPaint. Will now be clYourColor and white only. Copy this bitmap to your original Bitmap using MaskBlt, with the foreground dwRop set to srcAnd and the background dwRop set to srcPaint.(you will need another copy of the mono bitmap to use as the bit mask)

A hassle to do, but it gives some appreciation of why graphics progs can be slow.

0
 
mullet_attackCommented:
Oh, sorry last comment. The Copyrect/MaskBlt methods will be lightning fast...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pserjAuthor Commented:
Noooooo!
Inthe way is very easy but too slow :(
Mullet_Attack way doesn't work (why?)

For Mullet:
   you saied "Will now be clYourColor and white only." but I obtain my color and black only :( why? I have followed all your instruction but isn't work!
0
 
mullet_attackCommented:
are you sure copymode of dest canvas is cmSrcPaint?

I can write a sample, but I need some info first, please.

Is your bitmap black and one other color, or black and many colors?

Do you want to change only black stuff to a different other color?
0
 
mullet_attackCommented:
Hi pseri,

I thought I'd have a look at some code for you, and I apologise for making hard something that is very easy. The BrushCopy method is used to replace the specified color with the color of the destination canvas.

DrawBitmap button creates a picture to demo with, since I can't post a bitmap file here :-)

BlacktoYellow button copies the original bitmap, replacing the black with yellow.

Here's some code :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    imgOriginal: TImage;
    btnDrawBitmap: TButton;
    btnChangeBlacktoYellow: TButton;
    imgMono: TImage;
    imgYourColor: TImage;
    procedure btnDrawBitmapClick(Sender: TObject);
    procedure btnChangeBlacktoYellowClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnDrawBitmapClick(Sender: TObject);
begin
 with imgOriginal do
   begin
     canvas.brush.color := clred;
     canvas.Fillrect(rect(0,0,width,height div 2));
     canvas.brush.color := clLime;
     canvas.Fillrect(rect(0,height div 2,width,height));
     canvas.brush.color := clBlack;
     canvas.pen.color := clBlack;
     canvas.ellipse(width div 4,
             height div 4,
             width - (width div 4),
             height - (height div 4));
   end;
end;

procedure TForm1.btnChangeBlacktoYellowClick(Sender: TObject);
begin
imgMono.canvas.Brush.color := clYellow;
imgmono.canvas.BrushCopy(Imgmono.ClientRect,imgOriginal.picture.bitmap,imgOriginal.ClientRect,clBlack);
end;

end.
0
 
intheCommented:
inthe changed the proposed answer to a comment
0
 
pserjAuthor Commented:
Tnx Mullet, i'll be glad if you can help me.
My images are small bmp (18x18 or 20x20 max) with a black draw and a backgroun color (clFuchsia) for trasp.
I want to turn black in other color.
The result img should have clFuchsia as transparent background and my color on black place.

Sorry if my english is not perfect, I hope you understand my puzzle!
0
 
mullet_attackCommented:
simplest way is a form with two TImages, set to the size you want.
Name the first one imgOriginal, and the other imgDestination. Add Three buttons, LoadBitmap, SaveBitmap, and ChangeBlacktoOtherColor. ( I chose clYellow as my color ).

Load this demo into Delphi, and have a play around, you will see how it works.

Good Luck

-------------------

procedure TForm1.btnLoadBitmapClick(Sender: TObject);
begin
  imgOriginal.picture.LoadFromFile('mypic.bmp');
end;

procedure TForm1.btnChangeBlacktoOtherColorClick(Sender: TObject);
begin
imgDestination.canvas.Brush.color := clYellow;
imgDestination.canvas.BrushCopy(imgDestination.ClientRect,imgOriginal.picture.bitmap,imgOriginal.ClientRect,clBlack);
end;

procedure TForm1.btnSaveBitmapClick(Sender: TObject);
begin
  imgDestination.picture.SaveToFile('mynewpic.bmp');
end;
0
 
pserjAuthor Commented:
Thanks Mullet!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now