source: trunk/Packages/VCard/ContactImage.pas

Last change on this file was 148, checked in by chronos, 18 months ago
  • Modified: Update Common and VCard package. Remove U prefix from unit names.
File size: 9.2 KB
Line 
1unit ContactImage;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics, VCard, ExtCtrls;
7
8type
9 TContactImageFormat = (ifNone, ifBmp, ifJpeg, ifPng, ifGif);
10
11 { TContactImage }
12
13 TContactImage = class(TComponent)
14 private
15 FOnChange: TNotifyEvent;
16 FUrl: string;
17 FUsed: Boolean;
18 function GetImageFormat(ContactProperty: TContactProperty): TContactImageFormat;
19 procedure SetUrl(AValue: string);
20 procedure SetUsed(AValue: Boolean);
21 procedure DoOnChange;
22 procedure SaveImageToStream(ImageFormat: TContactImageFormat;
23 Stream: TStream);
24 procedure LoadImageFromStream(ImageFormat: TContactImageFormat;
25 Stream: TStream);
26 public
27 Bitmap: TBitmap;
28 Format: TContactImageFormat;
29 Loaded: Boolean;
30 Modified: Boolean;
31 Contact: TContact;
32 FieldIndex: TContactFieldIndex;
33 procedure Load;
34 procedure Save;
35 procedure Clear;
36 procedure LoadFromFile(FileName: string);
37 procedure SaveToFile(FileName: string);
38 procedure Assign(Source: TPersistent); override;
39 constructor Create(AOwner: TComponent); override;
40 destructor Destroy; override;
41 published
42 property Url: string read FUrl write SetUrl;
43 property Used: Boolean read FUsed write SetUsed;
44 property OnChange: TNotifyEvent read FOnChange write FOnChange;
45 end;
46
47procedure Register;
48
49
50implementation
51
52procedure Register;
53begin
54 RegisterComponents('VCard', [TContactImage]);
55end;
56
57{ TContactImage }
58
59function TContactImage.GetImageFormat(ContactProperty: TContactProperty
60 ): TContactImageFormat;
61begin
62 if (ContactProperty.Attributes.IndexOf('JPEG') <> -1) or
63 (ContactProperty.Attributes.IndexOf('jpeg') <> -1) then Result := ifJpeg
64 else
65 if (ContactProperty.Attributes.IndexOf('GIF') <> -1) or
66 (ContactProperty.Attributes.IndexOf('gif') <> -1) then Result := ifGif
67 else
68 if (ContactProperty.Attributes.IndexOf('PNG') <> -1) or
69 (ContactProperty.Attributes.IndexOf('png') <> -1) then Result := ifPng
70 else
71 if (ContactProperty.Attributes.IndexOf('BMP') <> -1) or
72 (ContactProperty.Attributes.IndexOf('bmp') <> -1) then Result := ifBmp
73 else
74 Result := ifNone;
75end;
76
77procedure TContactImage.SetUrl(AValue: string);
78begin
79 if FUrl = AValue then Exit;
80 FUrl := AValue;
81 Modified := True;
82 Used := FUrl <> '';
83 DoOnChange;
84end;
85
86procedure TContactImage.SetUsed(AValue: Boolean);
87begin
88 if FUsed = AValue then Exit;
89 FUsed := AValue;
90 DoOnChange;
91end;
92
93procedure TContactImage.DoOnChange;
94begin
95 if Assigned(FOnChange) then
96 FOnChange(Self);
97end;
98
99procedure TContactImage.SaveImageToStream(ImageFormat: TContactImageFormat;
100 Stream: TStream);
101var
102 JpegImage: TJpegImage;
103 PngImage: TPortableNetworkGraphic;
104 GifImage: TGIFImage;
105begin
106 if ImageFormat = ifJpeg then begin
107 JpegImage := TJPEGImage.Create;
108 try
109 try
110 JpegImage.SetSize(Bitmap.Width, Bitmap.Height);
111 JpegImage.Canvas.Draw(0, 0, Bitmap);
112 JpegImage.SaveToStream(Stream);
113 except
114 end;
115 finally
116 JpegImage.Free;
117 end;
118 end else
119 if ImageFormat = ifPng then begin
120 PngImage := TPortableNetworkGraphic.Create;
121 try
122 try
123 PngImage.SetSize(Bitmap.Width, Bitmap.Height);
124 PngImage.Canvas.Draw(0, 0, Bitmap);
125 PngImage.SaveToStream(Stream);
126 except
127 end;
128 finally
129 PngImage.Free;
130 end;
131 end else
132 if ImageFormat = ifGif then begin
133 GifImage := TGIFImage.Create;
134 try
135 try
136 GifImage.SetSize(Bitmap.Width, Bitmap.Height);
137 GifImage.Canvas.Draw(0, 0, Bitmap);
138 GifImage.SaveToStream(Stream);
139 except
140 end;
141 finally
142 GifImage.Free;
143 end;
144 end else
145 if ImageFormat = ifBmp then begin
146 try
147 Bitmap.SaveToStream(Stream);
148 except
149 end;
150 end else begin
151 // Use default type
152 SaveImageToStream(ifJpeg, Stream);
153 end;
154end;
155
156procedure TContactImage.LoadImageFromStream(ImageFormat: TContactImageFormat;
157 Stream: TStream);
158var
159 JpegImage: TJpegImage;
160 PngImage: TPortableNetworkGraphic;
161 GifImage: TGIFImage;
162 BmpImage: TBitmap;
163begin
164 if ImageFormat = ifJpeg then begin
165 try
166 JpegImage := TJPEGImage.Create;
167 try
168 JpegImage.LoadFromStream(Stream);
169 with Bitmap do begin
170 PixelFormat := pf24bit;
171 SetSize(JpegImage.Width, JpegImage.Height);
172 Canvas.Draw(0, 0, JpegImage);
173 end;
174 finally
175 JpegImage.Free;
176 end;
177 Used := True;
178 except
179 Used := False;
180 end;
181 end else
182 if ImageFormat = ifPng then begin
183 try
184 PngImage := TPortableNetworkGraphic.Create;
185 try
186 PngImage.LoadFromStream(Stream);
187 with Bitmap do begin
188 PixelFormat := pf24bit;
189 SetSize(PngImage.Width, PngImage.Height);
190 Canvas.Draw(0, 0, PngImage);
191 end;
192 finally
193 PngImage.Free;
194 end;
195 Used := True;
196 except
197 Used := False;
198 end;
199 end else
200 if ImageFormat = ifGif then begin
201 try
202 GifImage := TGIFImage.Create;
203 try
204 GifImage.LoadFromStream(Stream);
205 with Bitmap do begin
206 PixelFormat := pf24bit;
207 SetSize(GifImage.Width, GifImage.Height);
208 Canvas.Draw(0, 0, GifImage);
209 end;
210 finally
211 GifImage.Free;
212 end;
213 Used := True;
214 except
215 Used := False;
216 end;
217 end else
218 if ImageFormat = ifBmp then begin
219 try
220 BmpImage := TBitmap.Create;
221 try
222 BmpImage.LoadFromStream(Stream);
223 with Bitmap do begin
224 PixelFormat := pf24bit;
225 SetSize(BmpImage.Width, BmpImage.Height);
226 Canvas.Draw(0, 0, BmpImage);
227 end;
228 finally
229 BmpImage.Free;
230 end;
231 Used := True;
232 except
233 Used := False;
234 end;
235 end else begin
236 // Unknown image type, let TPicture guess what it is
237 try
238 with TImage.Create(nil) do
239 try
240 Picture.LoadFromStream(Stream);
241 with Bitmap do begin
242 PixelFormat := pf24bit;
243 SetSize(Picture.Bitmap.Width, Picture.Bitmap.Height);
244 Canvas.Draw(0, 0, Picture.Bitmap);
245 end;
246 finally
247 Free;
248 end;
249 Used := True;
250 except
251 Used := False;
252 end;
253 end;
254end;
255
256procedure TContactImage.Load;
257var
258 PhotoProperty: TContactProperty;
259 Photo: string;
260 Stream: TMemoryStream;
261begin
262 PhotoProperty := Contact.GetProperty(FieldIndex);
263 if not Loaded then begin
264 if Assigned(PhotoProperty) then begin
265 Loaded := True;
266 Modified := True;
267 Photo := Contact.Fields[FieldIndex];
268 if (Photo <> '') and (PhotoProperty.Encoding <> veNone) then begin
269 Stream := TMemoryStream.Create;
270 try
271 Stream.Write(Photo[1], Length(Photo));
272 Stream.Position := 0;
273 LoadImageFromStream(GetImageFormat(PhotoProperty), Stream);
274 finally
275 Stream.Free;
276 end;
277 end else begin
278 Url := Photo;
279 Used := True;
280 end;
281 end else Used := False;
282 end;
283end;
284
285procedure TContactImage.Save;
286var
287 PhotoProperty: TContactProperty;
288 Photo: string;
289 Stream: TMemoryStream;
290begin
291 if Modified then begin
292 if Used then begin
293 PhotoProperty := Contact.GetProperty(FieldIndex);
294 if not Assigned(PhotoProperty) then begin
295 PhotoProperty := TContactProperty.Create;
296 PhotoProperty.Name := 'PHOTO';
297 PhotoProperty.Attributes.DelimitedText := 'JPEG';
298 Contact.Properties.Add(PhotoProperty);
299 end;
300 if Url <> '' then begin
301 Contact.Fields[FieldIndex] := Url;
302 PhotoProperty.Encoding := veNone;
303 end else begin
304 PhotoProperty.Encoding := veBase64;
305 Stream := TMemoryStream.Create;
306 try
307 SaveImageToStream(GetImageFormat(PhotoProperty), Stream);
308 Photo := '';
309 SetLength(Photo, Stream.Size);
310 Stream.Position := 0;
311 Stream.Read(Photo[1], Length(Photo));
312 Contact.Fields[FieldIndex] := Photo;
313 finally
314 Stream.Free;
315 end;
316 end;
317 end else begin
318 PhotoProperty := Contact.GetProperty(FieldIndex);
319 if Assigned(PhotoProperty) then
320 Contact.Properties.Remove(PhotoProperty);
321 end;
322 Modified := False;
323 end;
324end;
325
326procedure TContactImage.Clear;
327begin
328 Url := '';
329 Used := False;
330 Modified := True;
331end;
332
333procedure TContactImage.LoadFromFile(FileName: string);
334begin
335 with TImage.Create(nil) do
336 try
337 Picture.LoadFromFile(FileName);
338 Bitmap.Assign(Picture.Bitmap);
339 Url := '';
340 Used := True;
341 Modified := True;
342 finally
343 Free;
344 end;
345end;
346
347procedure TContactImage.SaveToFile(FileName: string);
348begin
349 with TImage.Create(nil) do
350 try
351 Picture.Bitmap.Assign(Bitmap);
352 Picture.SaveToFile(FileName);
353 finally
354 Free;
355 end;
356end;
357
358procedure TContactImage.Assign(Source: TPersistent);
359var
360 SourceImage: TContactImage;
361begin
362 if Source is TContactImage then begin
363 SourceImage := Source as TContactImage;
364 Bitmap.Assign(SourceImage.Bitmap);
365 Url := SourceImage.Url;
366 Loaded := SourceImage.Loaded;
367 Modified := SourceImage.Modified;
368 Format := SourceImage.Format;
369 FieldIndex := SourceImage.FieldIndex;
370 Contact := SourceImage.Contact;
371 Used := SourceImage.Used;
372 end;
373end;
374
375constructor TContactImage.Create(AOwner: TComponent);
376begin
377 inherited;
378 Bitmap := TBitmap.Create;
379end;
380
381destructor TContactImage.Destroy;
382begin
383 FreeAndNil(Bitmap);
384 inherited;
385end;
386
387end.
388
Note: See TracBrowser for help on using the repository browser.