Solved

Paint Example

Posted on 2001-09-16
22
457 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

757 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