Question

Draw outline/border around a shape on TCanvas in Delphi

Asked by: colani

I have a coloured shape (or more specifically, a series of shapes joined together to make one, single-coloured irregular shape) on a TCanvas.

My question is, how can I draw a single-pixel outline/border in a different colour around the shape on the TCanvas?

In the attached image, the blue area represents "standing space" on a venue's seating plan. Ideally, I'd like to outline/border the blue area in black.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-07-31 at 05:57:29ID24616508
Tags

Delphi

,

canvas

,

border

,

outline

,

draw

Topics

Graphics and Delphi Programming

,

Delphi Programming

,

Graphics

Participating Experts
3
Points
500
Comments
26

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Using TCanvas
    I have a bitmap that I load from a file and put into an integer array. To do this I draw the bitmap onto a canvas and then uses the canvas' pixels property to get the pixels into the array. The problem is it won't work if the canvas isn't a part of a VCL. When I made a variab...
  2. Clipping on a TCanvas.
    I draw on a TCanvas object, let's say on Printer.Canvas. It is possible to limit my drawing to a given rectangle inside Printer.Canvas.ClipRect? Anticipated thanks.
  3. Drawing rectangles and circles on a TCanvas
    Hi. I want to make a drawing interface similar to the one in MS Paint, eg. when you draw a rectangle, there should be an outline painted when you hold how the mousebutton so you can see the size of the to-be-drawn rectangle, then when you let go of the button it draws itself...
  4. Drawing and arrow shape ?
    I have a method which is MEANT to draw and arrow shape, like ---> I can draw the line, but i am having problems drawing the arrow head. Here is my code void TDependancy::plot(TCanvas *canvas) { DebugOut("TDependancy::plot"); canvas->Pen->Style ...
  5. Drawing a line (One pixel at the time)
    Does anyone have a source-code for a procedure, that can draw a line? (on a tcanvas or something). And how can I make it draw 1 pixel at the time, so it looks like a shot (i am making a game). Hope you can help me.
  6. Drawing in Delphi 7
    I was just wondering how to draw shapes in Delphi 7. I need to draw a slice of pie as well as a rectangle. These shapes will be colour-filled as well as moved around on the screen/form when compiled so I cannot simply create a .bmp Any help would be greatly appreciated.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rfwoolfPosted on 2009-07-31 at 06:16:29ID: 24988461

If you're simply drawing ON the Canvas, then this should be no problem. You can use pens, and by referring to the pixels you can draw lines as you like - you could have 4 proceduresL
DrawTop
DrawLeft
DrawRight
DrawBottom

Then you would call each shape, e.g.
DrawTop(Shape1)
DrawLeft(Shape1)
DrawBottom(Shape1)

On the other hand, if you want to draw OUTSIDE the Canvas of the shape, then you are drawing on the TForm's Canvas, that can be a little complicated.

 

by: colaniPosted on 2009-07-31 at 06:24:18ID: 24988518

Thanks, but I don't think the solution quite fits.

To explain further, multiple shapes (without a border colour) are drawn on to the canvas (similar to the blue area in the attached example)

What I want to be able to do is draw a 1-pixel black border over the outer-most blue pixels from the resulting shape.

 

by: rfwoolfPosted on 2009-07-31 at 06:33:17ID: 24988589

umm... if you are drawing the shapes, then I don't see why you can't draw a border around it, especially if you succeeded in doing other shapes

 

by: rfwoolfPosted on 2009-07-31 at 06:45:54ID: 24988700

Here's an example of drawing a line on a canvas:

procedure TForm3.Button1Click(Sender: TObject);
begin
  self.Canvas.PenPos := point(50,50);
  self.Canvas.LineTo(250,50);
end;

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: colaniPosted on 2009-07-31 at 06:46:21ID: 24988702

Attached image 2: the blue area shows each of the shapes drawn on to the canvas with a black border,
Attached image 3: shows the result that I'm trying to achieve

 

by: rfwoolfPosted on 2009-07-31 at 06:47:49ID: 24988719

Please explain how you are drawing the shapes Give me an example to work with. You are probably drawing shapes on a canvas and setting a 'border'. What you want to do in this case is draw partial borders - to say "only draw the top, left and bottom" etc.

 

by: colaniPosted on 2009-07-31 at 06:49:29ID: 24988737

What I'm ideally looking for is either:
- an algorithm that will trace around the outside edge of the shape on the canvas and replace (in this example) the outer-most blue pixel with a black one, or
- a floodfill-type alogrithm that hits the boundary and sets it to an alternative colour

 

by: Geert_GruwezPosted on 2009-07-31 at 06:50:21ID: 24988743

it all depends on how you are drawing the shapes ...
if it is a completely closed shape then are you using a polygon ?

with that, you can do all at once:
The Polygon function draws a polygon consisting of two or more vertices connected by straight lines. The polygon is outlined by using the current pen and filled by using the current brush and polygon fill mode.

BOOL Polygon(

    HDC hdc,      // handle to device context
    CONST POINT *lpPoints,      // pointer to polygon's vertices
    int nCount       // count of polygon's vertices
   );

 

by: rfwoolfPosted on 2009-07-31 at 06:50:32ID: 24988745

Question, must this be dynamic? In other words, right now you have a 'T' shape made up of little rectangles, and if the user rearranges this to say, an 'L' shape you want it to have the intelligence to draw a border?
In that case the algorithm I came up with is to iterate through all shapes in that object, and detect if they have any shapes to their north, west, south and east, and draw lines according to that.

 

by: Geert_GruwezPosted on 2009-07-31 at 06:54:48ID: 24988780

actually if you would have a algorithm to create polygons from your shapes (i assume they are all rectangles)
you could also use PolyPolygon as well

even not adjacent ones ...

 

by: colaniPosted on 2009-07-31 at 07:00:19ID: 24988819

Apologies, I should provide some more background information...

The problem is dynamic. The overall shape is based on a grid made up of individual (cells) rectangles (either selected or not)

I agree that you could use your approach to detect if an individual rectangle has any other rectangles to it's north, west, south or east... but it starts getting complicated if you end up with a shape something like the one attached below!

That's why I was hoping to draw the rectangles without borders initially and retrospectively draw the border around the final shape.

 

by: Geert_GruwezPosted on 2009-07-31 at 07:01:35ID: 24988835

i'm actually trying to come up with an algoritm to convert adjacent rectangles into polygons

from there it would be simple ...

 

by: rfwoolfPosted on 2009-07-31 at 07:03:45ID: 24988854

No, if all the shapes are the same size, then you could use the North-East-South-West method.

If a shape has no shapes to it's direct EAST, draw a EASTern line,
If a shape has no shapes to it's direct NORTH, draw a NORTHern line,
If a shape has no shapes to it's direct WEST, draw a WESTern line,
If a shape has no shapes to it's direct SOUTH, draw a SOUTHern line,

Yes I can see how it's not easy, but it's possible to get something working.
The only problem is if your shapes are not all the same dimensions, or if 1 is just 1 pixel off than it should be.

 

by: colaniPosted on 2009-07-31 at 07:04:58ID: 24988864

The problem with the polygon approach is that each of the rectangle's co-ordinates on the grid are being retrieved from a database. It would be a huge task to retrieve the rectangles' co-ordinates in the correct order to be able to draw the whole shape as a single polygon.

 

by: Geert_GruwezPosted on 2009-07-31 at 07:06:24ID: 24988875

but ... you are drawing the shapes individually ...
and for that you need ... those coordinates too
or am i missing something obvious ?

 

by: rfwoolfPosted on 2009-07-31 at 07:09:04ID: 24988901

I guess that polygon approach is the most feasible...

"It would be a huge task to retrieve the rectangles' co-ordinates in the correct order to be able to draw the whole shape as a single polygon."
- If your program has the intelligence to know what rectangles constitute 1 'shape', then you could iterate through each one and put them 'in order' working left to right, bottom to top.
..and if you can do that, then you can also use the algorithm I described, but something makes me wonder if the polygon approach isn't better

 

by: colaniPosted on 2009-07-31 at 07:11:13ID: 24988918

Thanks rfwoolf... I think we're getting somewhere with your solution.

The rectangles have an associated X and Y position on the grid (which is stored in the database), so I could use the North-East-South-West method to determine a rectangle's relationship to all others.

Final question, how do I draw the border (say to black) for just one of the rectangles edges?

 

by: Geert_GruwezPosted on 2009-07-31 at 07:12:18ID: 24988930

i'm off elsewhere ... home ...
i'll look into such an algorithm tonight ...

 

by: Geert_GruwezPosted on 2009-07-31 at 07:20:14ID: 24988996

Set pen options
Pen.color := clBlack;
Pen.Width := 3;
MoveTo(X1,Y1);
LineTo(X2,Y2);

 

by: colaniPosted on 2009-07-31 at 07:35:09ID: 24989136

Thank you both for your solutions.

The North-East-South-West method offers the best immediate solution to this particular problem.

However, if I have time to re-visit the issue then the polygon method might well be more efficient.

 

by: EddieShipmanPosted on 2009-07-31 at 08:07:34ID: 24989446

He can also set the brush style to bsClear and use the Canvas.Rectangle function to draw the rect around the
shape at the same correds of the shape, he also needs to set the pen.width and pen.color to his desired values.

 

by: Geert_GruwezPosted on 2009-07-31 at 18:57:02ID: 24993762

uhm, i gave it a start for finding a set of polygons for the adjacent rectangles
i allready had to make the assumption of rectangles and ortogonal grid

looking at where this is going NESW seems indeed like your best valid option
unless you find a algorithm for finding the polygon
i bumped into dff looking for a solution myself
it seems some people are hooked on delphi:
http://www.delphiforfun.org/default.html

 

by: Geert_GruwezPosted on 2009-07-31 at 19:10:25ID: 24993817

just a idea that springs to mind:
add a layer of shapes behind the ones you see, 1 pixel bigger (like GrowRect function)
draw those shapes first
then draw your normal visible layer

and that would be by far the most simple solution !!!
attached is screenshot

type
  TForm4 = class(TForm)
    Panel1: TPanel;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Shape8: TShape;
    Shape9: TShape;
    Shape10: TShape;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  end;
 
var
  Form4: TForm4;
 
procedure TForm4.Button2Click(Sender: TObject);
var
  I: Integer;
  s, X: TShape;
 
begin
  for I := 1 to 10 do
  begin
    s := TShape(FindComponent(Format('Shape%d', [I])));
    if Assigned(s) then
    begin
      x := TShape.Create(Self);
      x.Parent := Panel1;
      x.Left := s.Left - 1;
      x.Top := s.Top -1;
      x.Width := s.Width + 2;
      x.Height := s.Height + 2;
      x.Pen.Color := clGreen;
      x.Pen.Width := 3;
      x.Brush.Color := clBlack;
      x.Brush.Style := bsClear;
      x.Visible := True;
      x.SendToBack;
    end;
  end;
end;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:

Select allOpen in new window

 

by: Geert_GruwezPosted on 2009-07-31 at 19:12:20ID: 24993824

with a pen width of 3 it had to be :

      x.Left := s.Left - 2;
      x.Top := s.Top -2;
      x.Width := s.Width + 3;
      x.Height := s.Height + 3;

 

by: rfwoolfPosted on 2009-08-01 at 13:27:18ID: 24996509

Hmm I haven't read through Geert's code, but it occured to me at the time I suggested the NESW approach that if some shapes are say, 1 pixel off where they're supposed to be the algorithm might not work - so just keep that in mind. Fortunately colani's software might ensure that this doesn't happen because it's based on a grid system, but if it does, the algorithm might file. That's why this polygon idea seems good in that (from what I understand) the shapes are 'connected'.

I see Colani is closing the question. I assume he has found a solution. Hopefully you will share the outcome.

 

by: rfwoolfPosted on 2009-08-01 at 13:28:20ID: 24996512

"the algorithm might file"  ---> "fail"

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...