Solved

how can i fill the different color with the region  separate by the black line?

Posted on 2002-06-17
20
172 Views
Last Modified: 2010-04-04
hi,
  experts,please help me,
the image in here:
http://www26.brinkster.com/jbaswjy
i want to fill the different color with the region seperate by the black line,how can i do?
 thanks
0
Comment
Question by:jbas
  • 8
  • 6
  • 3
  • +2
20 Comments
 
LVL 2

Expert Comment

by:333
ID: 7086151
use TCanvas.FloodFill
for example:
  Image1.Picture.Bitmap.Canvas.Brush.Style:=bsSolid;
  Image1.Picture.Bitmap.Canvas.Brush.Color:=clBlue;
  Image1.Picture.Bitmap.Canvas.FloodFill(1, 1, clWhite, fsSurface);

this will paint your region (topmost on the left) with blue.
0
 

Author Comment

by:jbas
ID: 7086482
to 333
 thanks!but it not work ,mybe you not understond my meaning,i want to fill different color in a timage.and the image first assign a picture,like  this :http://www26.brinkster.com/jbaswjy
and second i hope to fill different color in the image,separate by the black line area!
thanks!
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7086564
jbas,

1. So, if I got you right, you want to fill each region
   with different color, right?

2. These regions are enclosed with black line, and "filled"
   with white color, right?

3. Do you have some idea what are these colors for each
   region, or do you just want to have regions filled
   with random colors ?

0
 

Author Comment

by:jbas
ID: 7086680
to  Cynna :
  thanks to you answer!
>>1. So, if I got you right, you want to fill each region
>>  with different color, right?
yes!
>>2. These regions are enclosed with black line, >>and "filled"  with white color, right?
yes!
>>3. Do you have some idea what are these colors for each
>>  region, or do you just want to have regions filled
>>  with random colors ?
random colors!
i waiting you answer! thanks!
0
 
LVL 3

Expert Comment

by:Slavak
ID: 7086745
it sounds as common task to color a map with 4 different colors, isn't it?
0
 
LVL 2

Accepted Solution

by:
333 earned 80 total points
ID: 7087036
jbas,

this works for me (i assume, that bitmap is monochrome):

procedure TForm1.Button1Click(Sender: TObject);
var
  i,j: integer;
begin
  // Load bitmap
  Image1.Picture.LoadFromFile('d:\temp\temp.bmp');
  // If it's monochrome, make it color
  Image1.Picture.Bitmap.PixelFormat:=pf8bit;
  Randomize;
  // Go thru bitmap pixel by pixel
  for i:=0 to Image1.Picture.Bitmap.Width-1 do
    for j:=0 to Image1.Picture.Bitmap.Height-1 do
  // See if pixels color is white
  //  If white then FloodFill area with random color
      if Image1.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then begin
        Image1.Picture.Bitmap.Canvas.Brush.Color:=StringToColor(Format('$00%0.2x%0.2x%0.2x',[Random(254),Random(254),Random(254)]));
        Image1.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
      end;
end;

0
 
LVL 7

Expert Comment

by:Cynna
ID: 7087092
jbas,

333 gave you the wright answer.
Copy/Paste that example, it will do what you want.

0
 

Author Comment

by:jbas
ID: 7089223
thank!
   when i use bmp,it can work well ,but when i use jpg,it can not work!why?
0
 

Author Comment

by:jbas
ID: 7091264
333 ,Cynna :
  thanks!i should .jpg ->.bmp ,and do your code to finished my work?
0
 
LVL 2

Expert Comment

by:333
ID: 7091881
well, to use .jpg you should add unit JPEG to your form.
for example:

uses Windows,Forms,...,JPEG;

after that, you will be able to load .jpg .
however, TJPEGImage doesn't have canvas property, so you wont be able to paint on it (fill regions). you will need to copy image on to bitmap, in order to paint on it.

so, here is some changes you need to make in order to use jpg:

1) add JPEG to your form
uses Windows,Forms,...,JPEG;

2) and my previous procedure should look like this:
procedure TForm1.Button1Click(Sender: TObject);
var
 i,j: integer;
 JPGImage : TJPEGImage;
 ImageFileName: string;
begin
 // Name of your image file
 ImageFileName:='d:\temp\temp.jpg';

 // Check here for image file extension, so you know if it's jpg or bmp
 if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then begin
 // Load bitmap
   Image1.Picture.LoadFromFile(ImageFileName);
 // If it's monochrome, make it color
   Image1.Picture.Bitmap.PixelFormat:=pf8bit;
 end
 else begin
 // Process jpg image
   JPGImage := TJPEGImage.Create;
   JPGImage.LoadFromFile(ImageFileName);
 // Copy jpg to bitmap
   Image1.Picture.Assign(JPGImage);
   JPGImage.Free;
 end;

 Randomize;
 // Go thru bitmap pixel by pixel
 for i:=0 to Image1.Picture.Bitmap.Width-1 do
   for j:=0 to Image1.Picture.Bitmap.Height-1 do
 // See if pixels color is white
 //  If white then FloodFill area with random color
     if Image1.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then begin
       Image1.Picture.Bitmap.Canvas.Brush.Color:=StringToColor(Format('$00%0.2x%0.2x%0.2x',[Random(254),Random(254),Random(254)]));
       Image1.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
     end;
end;

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:hgbdelphi
ID: 7092309
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 
LVL 2

Expert Comment

by:333
ID: 7092327
what exactly is wrong? empty bitmap? bad colors? regions aren
't filled?
0
 

Expert Comment

by:hgbdelphi
ID: 7092331
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 

Expert Comment

by:hgbdelphi
ID: 7092333
sorry,it can not work well.please help me.

procedure Tsimulate.Button1Click(Sender: TObject);
var
  i,j: integer;
  JPGImage : TJPEGImage;
  ImageFileName: string;
  icolor:Tcolor;
  wb:boolean;
begin
   if OpenPictureDialog1.Execute then
   begin
       ImageFileName:=OpenPictureDialog1.FileName;
       if LowerCase(ExtractFileExt(ImageFileName))='.bmp' then
         Imagbf.Picture.LoadFromFile(ImageFileName)
       else
       begin
        JPGImage := TJPEGImage.Create;
        JPGImage.LoadFromFile(ImageFileName);
        Imagbf.Picture.Assign(JPGImage);
        JPGImage.Free;
       end;
   end;
//   repaint;
   Randomize;

 for i:=0 to Imagbf.Picture.Bitmap.Width-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Height-1 do
 if Imagbf.Picture.Bitmap.Canvas.Pixels[i,j]=clWhite then
 begin
    icolor:= rgb(random(1000),random(1000),random(1000));
    if (icolor<>clblack) and (icolor<>clwhite) then
    begin
       Imagbf.Picture.Bitmap.Canvas.Brush.Color:=icolor;
       Imagbf.Picture.Bitmap.Canvas.FloodFill(i,j,clWhite,fsSurface);
    end;
 end;

  repaint;
{
 for i:=0 to Imagbf.Picture.Bitmap.Height-1 do
 for j:=0 to Imagbf.Picture.Bitmap.Width-1 do
 begin
     wb:=false;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]=clblack then
     begin
            Imagbf.Picture.Bitmap.Canvas.Pixels[j,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j-1,i];
            wb:=true;
     end;
     if Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]=clblack then
     begin
       if wb=true then
       Imagbf.Picture.Bitmap.Canvas.Pixels[j+1,i]:=Imagbf.Picture.Bitmap.Canvas.Pixels[j+2,i];
       wb:=false;
     end;
 end;
 repaint;
 Btnmn.Enabled:=true;
 }
end;
0
 

Author Comment

by:jbas
ID: 7092340
i use the code but empty bitmap,why?
0
 

Author Comment

by:jbas
ID: 7092882
to 333 ,Cynna :
  thanks!i do not finished it,i use:
             imagbf.Picture.Assign(JPGImage);
i shoule trans .jpg to .bmp????
0
 
LVL 2

Expert Comment

by:333
ID: 7092953
oops, sorry, my mistake :)

change the line where you assign JPEG image to TImage from
  Image1.Picture.Assign(JPGImage);
to
  Image1.Picture.Bitmap.Assign(JPGImage);

or in your code change the line from your last post from  imagbf.Picture.Assign(JPGImage);
to
  imagbf.Picture.Bitmap.Assign(JPGImage);
0
 

Author Comment

by:jbas
ID: 7094260
333,
  thanks to answer my question!
  but if i use image1.picture.bitmap.assign(jpgimage),it can appear the picture in timage,but the picture is not clear,do not finished my work.
  how can i do it ?
0
 
LVL 2

Expert Comment

by:333
ID: 7094955
what do you mean "the picture is not clear"? is there lot of white space that isn't filled?
if yes, then this is because your jpg image is not 100% white. due to jpg compression some pixels in image are not RGB(255,255,255), but their value is something near white, for example RGB(253,250,254), so those pixels will not be filled.
the best solution would be to use monochrome bitmaps (with only two colors - 100% white and 100% black).
another solution would be to convert loaded jpg image to monochrome and then back to color. to do this add this to your code (right after line Randomize;):

Image1.Picture.Bitmap.Monochrome:=true;
Image1.Picture.Bitmap.PixelFormat:=pf16bit;
but as you will see, your picture will be very scary :) this is because when converting to monochrome, delphi converts each pixel that is not 100% white to 100% black. so if you have gray pixel, that is very near to white (i.e. RGB(253,255,255)) it will be converted to clBlack.
as a workaround you may instead of those two lines add this loop:
for i:=0 to Image1.Picture.Bitmap.Width-1 do
  for j:=0 to Image1.Picture.Bitmap.Height-1 do
    if (GetRValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) and
       (GetGValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) and
       (GetBValue(Image1.Picture.Bitmap.Canvas.Pixels[i,j])>30) then
      Image1.Picture.Bitmap.Canvas.Pixels[i,j]:=clWhite
    else
      Image1.Picture.Bitmap.Canvas.Pixels[i,j]:=clBlack;

this will set any pixel that is darker than RGB(30,30,30) to black and any others to white.
0
 

Author Comment

by:jbas
ID: 7095288
thanks ,my english is poor ,sorry!
Cynna ,!because the points can not share,sorry!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

17 Experts available now in Live!

Get 1:1 Help Now