Solved

Bitmap and color

Posted on 2000-04-30
10
440 Views
Last Modified: 2013-12-03
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
Comment
Question by:pserj
  • 5
  • 3
  • 2
10 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2764645
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
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2764683
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
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2764684
Oh, sorry last comment. The Copyrect/MaskBlt methods will be lightning fast...
0
 

Author Comment

by:pserj
ID: 2789709
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
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2790907
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:mullet_attack
ID: 2791059
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
 
LVL 17

Expert Comment

by:inthe
ID: 2794482
inthe changed the proposed answer to a comment
0
 

Author Comment

by:pserj
ID: 2796201
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
 
LVL 2

Accepted Solution

by:
mullet_attack earned 50 total points
ID: 2796750
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
 

Author Comment

by:pserj
ID: 2797053
Thanks Mullet!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 89
Newbie Thread Programming 1 139
Help on project with Soap 10 47
how to update exe applicatio from internet ? 6 67
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now