Solved

Bitmap and color

Posted on 2000-04-30
10
445 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
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!

 

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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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