Solved

Paint Example

Posted on 2001-09-16
22
460 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

829 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