Solved

Paint Example

Posted on 2001-09-16
22
461 Views
Last Modified: 2010-04-06
Hi,

does anyone know ressources, where a simple
paint - program (draw primitives like lines, rectangles,
ellipses) and selecting ranges or objects
(using raster-op.) are available?

Or maybe a detailled discussion of raster operations
with source examples?

Thanks,

Gamba
0
Comment
Question by:Gamba
  • 9
  • 6
  • 2
  • +4
22 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 6485790
Use TCanvas from Graphics unit which makes it simple.
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6485800
Try this simple code:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Canvas.Pen.Color := clBlack;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Form1.Canvas.MoveTo(x, y);
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Form1.Canvas.LineTo(x, y);
end;

end.
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6485831
If you want to put pixels you might try this:

unit Unit1;

interface

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

type
  TRgb = record
    r: byte;
    g: byte;
    b: byte;
  end;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormDestroy(Sender: TObject);
  private
    Color: TRgb;
    Bmp: TBitmap;
    Draw: boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Color.r := 0; // Red value = 0;     \
  Color.g := 0; // Green value := 0;   }-  rgb 0, 0, 0 = Black
  Color.b := 0; // Blue value := 0;   /

  Bmp := TBitmap.Create; // Create a invisible bitmap to work with
  Bmp.PixelFormat := pf24bit; // Make it 24bit color depth
  Bmp.Width := Form1.ClientWidth;
  Bmp.Height := Form1.ClientHeight; // Make it same size as form1 canvas

  Draw := false; // do not draw yet!
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Draw := true;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Draw := false;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  ScanLine: pByteArray;
begin
  if (Draw) and (x>=0) and (x<Bmp.Width) and (y>=0) and (y<Bmp.Height) then begin
    ScanLine := Bmp.ScanLine[y];  // Get Bitmap Scanline
    ScanLine[(x*3)] := Color.r;
    ScanLine[(x*3)+1] := Color.g;
    ScanLine[(x*3)+2] := Color.b;
    // Scanline in 24 bit colordepth is built like this:
    // pixel[0, r], pixel[0, g], pixel[0, b], pixel[1, r], pixel[1, g], pixel[1, b]... etc...

    Form1.Canvas.Draw(0, 0, Bmp); // Finally draw the bitmap on our form
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Bmp.Free; // Free bitmap
end;

end.
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:Gamba
ID: 6485855
Thanks for your fast commenting
and sorry, because I did not explain exactly enough
what I need:

I need a complete base-application, with ability
to draw selection-rects (with setting the
raster-modes correctly to XOR-move lines over
the canvas), store drawn elements as objects to be able to redraw or delete them later.

There was a such a complete draw-application in an early
version of Delphi or Turbo-Pascal (as demo-code)
I think, but  I can't find it anymore, so I think, there might be anything similar available somewhere.

Gamba




0
 

Author Comment

by:Gamba
ID: 6485859
Thanks for your fast commenting
and sorry, because I did not explain exactly enough
what I need:

I need a complete base-application, with ability
to draw selection-rects (with setting the
raster-modes correctly to XOR-move lines over
the canvas), store drawn elements as objects to be able to redraw or delete them later.

There was a such a complete draw-application in an early
version of Delphi or Turbo-Pascal (as demo-code)
I think, but  I can't find it anymore, so I think, there might be anything similar available somewhere.

Gamba




0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6486092
listening...
0
 
LVL 3

Expert Comment

by:rondi
ID: 6486753
check out the free DIB library (stuckindoors.com or .co.uk)
0
 

Author Comment

by:Gamba
ID: 6486843
Could you give me the exact link to the library?
I could not find it there.
0
 
LVL 3

Expert Comment

by:rondi
ID: 6486918
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 6487491
listening
0
 

Author Comment

by:Gamba
ID: 6487560
I found it and will test it.
Wil take a little time...

Thank you,

Gamba

0
 
LVL 6

Accepted Solution

by:
zebada earned 50 total points
ID: 6490101
I decided to have a go at writing a simple paint program, I've always wanted to do it. It's not complete but the basics are there (includes source)....

http://www.blacky.co.nz/free/Paint.zip

Cheers
Paul
0
 

Author Comment

by:Gamba
ID: 6490271
Paul,

could you please check the zip-file,
cause Delphi says: "invalid stream format" for
the *.dfm-files after decompression,
so I could not open it.

Thank you

Gamba
0
 
LVL 6

Expert Comment

by:zebada
ID: 6490297
I just uploaded a new version (with all the project settings in it). I also downloaded it and it appeared to work fine.
Paul
0
 

Author Comment

by:Gamba
ID: 6490862
Thanks. I will try again.

Is it Delphi5/Delphi6?

I opened it with Delphi4. Maybe
this was the problem.

Gamba



0
 
LVL 6

Expert Comment

by:zebada
ID: 6491789
Delphi 5
0
 
LVL 6

Expert Comment

by:zebada
ID: 6491790
Delphi 5
0
 

Author Comment

by:Gamba
ID: 6491853
Thank you Paul,
exactly that what I was looking for
(the zip-file was o.k., with Delphi5 it opened
 immediately).
May I modify the code for my purpose
(develop basic functions for generating svg-templates)?

Gamba

0
 
LVL 6

Expert Comment

by:zebada
ID: 6491858
Sure, do whatever you want with it :)
0
 

Author Comment

by:Gamba
ID: 6492654
Thank you Paul!

Will you further develop your application?
Maybe you would be interested in sharing some
elements, so we don't develop the same things
two times?
If so, I would like to mail to you (to the
above address?) for giving you an outline
of what I would like to develop.

Gamba
0
 
LVL 6

Expert Comment

by:zebada
ID: 6493041
I am not planning on developing it further - one day I might. Right now I have no use for my own paint program, I only wrote that code in response to your question.
Glad I could help and Good luck.
Paul
0
 

Author Comment

by:Gamba
ID: 6494500
Thank you very much.

Gamba
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

Suggested Solutions

Title # Comments Views Activity
Open a URL with Internet Explorer in a new tab (not a new window) 1 140
select query - oracle 16 101
update joined tables 2 62
Firemonkey how to swip panel with finger tap 1 18
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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