File: mainform.pas

package info (click to toggle)
lazarus 0.9.28.2-12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 93,096 kB
  • ctags: 89,312
  • sloc: pascal: 820,189; xml: 202,194; makefile: 110,323; sh: 2,460; perl: 395; sql: 174; ansic: 133
file content (192 lines) | stat: -rw-r--r-- 5,957 bytes parent folder | download | duplicates (4)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{ Copyright (C) 2004 Mattias Gaertner

  Example for loading and saving jpeg images.
  
  Important:
    This example uses the JPEGForLazarusPackage (see in the directory above).
    You must first open once this package so that the IDE knows, where to find
    the lpk file.
    
  See the README.txt.


  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit MainForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Buttons, ExtDlgs;

type

  { TJPEGExampleForm }

  TJPEGExampleForm = class(TForm)
    LoadImageButton: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    SaveImageButton: TButton;
    LoadJPEGButton: TButton;
    SaveJPEGButton: TButton;
    ImageGroupBox: TGroupBox;
    Image1: TImage;
    SavePictureDialog1: TSavePictureDialog;
    procedure LoadJPEGButtonClick(Sender: TObject);
    procedure LoadImageButtonClick(Sender: TObject);
    procedure SaveJPEGButtonClick(Sender: TObject);
    procedure SaveImageButtonClick(Sender: TObject);
  private
    procedure UpdateInfo(const Filename: string);
  end;

var
  JPEGExampleForm: TJPEGExampleForm;

implementation

{ TJPEGExampleForm }

procedure TJPEGExampleForm.LoadJPEGButtonClick(Sender: TObject);
var
  JPEG: TJPEGImage;
begin
  OpenPictureDialog1.Options:=OpenPictureDialog1.Options+[ofFileMustExist];
  if not OpenPictureDialog1.Execute then exit;
  try
    //--------------------------------------------------------------------------
    // Create a TJPEGImage and load the file, then copy it to the TImage.
    // A TJPEGImage can only load jpeg images.
    JPEG:=TJPEGImage.Create;
    try
      JPEG.LoadFromFile(OpenPictureDialog1.Filename);
      // copy jpeg content to a TImage
      Image1.Picture.Assign(JPEG);
    finally
      JPEG.Free;
    end;
    //--------------------------------------------------------------------------
    UpdateInfo(OpenPictureDialog1.Filename);
  except
    on E: Exception do begin
      MessageDlg('Error','Error: '+E.Message,mtError,[mbOk],0);
    end;
  end;
end;

procedure TJPEGExampleForm.LoadImageButtonClick(Sender: TObject);
begin
  OpenPictureDialog1.Options:=OpenPictureDialog1.Options+[ofFileMustExist];
  if not OpenPictureDialog1.Execute then exit;
  try
    //--------------------------------------------------------------------------
    // Loading directly into a TImage. This will load any registered image
    // format. .bmp, .xpm, .png are the standard LCL formats.
    // The jpeg units register .jpeg and .jpg.
    Image1.Picture.LoadFromFile(OpenPictureDialog1.Filename);
    //--------------------------------------------------------------------------

    UpdateInfo(OpenPictureDialog1.Filename);
  except
    on E: Exception do begin
      MessageDlg('Error','Error: '+E.Message,mtError,[mbOk],0);
    end;
  end;
end;

procedure TJPEGExampleForm.SaveJPEGButtonClick(Sender: TObject);
var
  JPEG: TJPEGImage;
begin
  if Image1.Picture.Graphic=nil then begin
    MessageDlg('No image','Please open an image, before save',mtError,
      [mbOk],0);
    exit;
  end;
  
  SavePictureDialog1.Options:=SavePictureDialog1.Options+[ofPathMustExist];
  if not SavePictureDialog1.Execute then exit;
  try
    //--------------------------------------------------------------------------
    // Create a TImage1 and copy the TImage into it. Then save to file.
    // This will ignore the file extension. TImage1 will always save as jpeg.
    JPEG:=TJPEGImage.Create;
    try
      // copy content of the TImage to jpeg
      JPEG.Assign(Image1.Picture.Graphic);
      // save to file
      JPEG.SaveToFile(SavePictureDialog1.Filename);
    finally
      JPEG.Free;
    end;
    //--------------------------------------------------------------------------

    UpdateInfo(SavePictureDialog1.Filename);
  except
    on E: Exception do begin
      MessageDlg('Error','Error: '+E.Message,mtError,[mbOk],0);
    end;
  end;
end;

procedure TJPEGExampleForm.SaveImageButtonClick(Sender: TObject);
begin
  if Image1.Picture.Graphic=nil then begin
    MessageDlg('No image','Please open an image, before save',mtError,
      [mbOk],0);
    exit;
  end;

  SavePictureDialog1.Options:=SavePictureDialog1.Options+[ofPathMustExist];
  if not SavePictureDialog1.Execute then exit;
  try
    //--------------------------------------------------------------------------
    // Saving directly from a TImage to a file. This will save in any registered
    // image format. .bmp, .xpm, .png are the standard LCL formats.
    // The jpeg units register .jpeg and .jpg.
    // So, saving as file1.jpg will save as jpeg, while saving a file1.bmp will
    // save as bmp.
    Image1.Picture.SaveToFile(SavePictureDialog1.Filename);

    //--------------------------------------------------------------------------

    UpdateInfo(SavePictureDialog1.Filename);
  except
    on E: Exception do begin
      MessageDlg('Error','Error: '+E.Message,mtError,[mbOk],0);
    end;
  end;
end;

procedure TJPEGExampleForm.UpdateInfo(const Filename: string);
var
  Info: String;
begin
  if Image1.Picture.Graphic<>nil then begin
    Info:=Image1.Picture.Graphic.ClassName+':'+Filename;
  end else begin
    Info:=Filename;
  end;
  ImageGroupBox.Caption:=Info;
end;

initialization
  {$I mainform.lrs}

end.