Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

Paint Example

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
Gamba
Asked:
Gamba
  • 9
  • 6
  • 2
  • +4
1 Solution
 
TOndrejCommented:
Use TCanvas from Graphics unit which makes it simple.
0
 
FractionCommented:
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
 
FractionCommented:
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
Independent Software Vendors: 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!

 
GambaAuthor Commented:
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
 
GambaAuthor Commented:
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
 
Dennis9Commented:
listening...
0
 
rondiCommented:
check out the free DIB library (stuckindoors.com or .co.uk)
0
 
GambaAuthor Commented:
Could you give me the exact link to the library?
I could not find it there.
0
 
rondiCommented:
0
 
gandalf_the_whiteCommented:
listening
0
 
GambaAuthor Commented:
I found it and will test it.
Wil take a little time...

Thank you,

Gamba

0
 
zebadaCommented:
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
 
GambaAuthor Commented:
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
 
zebadaCommented:
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
 
GambaAuthor Commented:
Thanks. I will try again.

Is it Delphi5/Delphi6?

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

Gamba



0
 
zebadaCommented:
Delphi 5
0
 
zebadaCommented:
Delphi 5
0
 
GambaAuthor Commented:
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
 
zebadaCommented:
Sure, do whatever you want with it :)
0
 
GambaAuthor Commented:
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
 
zebadaCommented:
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
 
GambaAuthor Commented:
Thank you very much.

Gamba
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 9
  • 6
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now