Ignore:
Timestamp:
Dec 2, 2021, 3:19:57 PM (3 years ago)
Author:
chronos
Message:
  • Added: Allow to load photo from file or save it to file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormContact.pas

    r43 r45  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ComCtrls, ActnList, Menus, ExtCtrls, UContact, UFormProperties;
     9  ComCtrls, ActnList, Menus, ExtCtrls, ExtDlgs, UContact, UFormProperties;
    1010
    1111type
     
    1414
    1515  TFormContact = class(TForm)
    16     AEditField: TAction;
     16    APhotoSave: TAction;
     17    APhotoLoad: TAction;
    1718    ActionList1: TActionList;
    1819    ButtonCancel: TButton;
     
    114115    MemoNotes: TMemo;
    115116    MenuItem1: TMenuItem;
     117    MenuItem2: TMenuItem;
     118    OpenPictureDialog1: TOpenPictureDialog;
    116119    PageControlContact: TPageControl;
    117     PopupMenu1: TPopupMenu;
     120    PopupMenuPhoto: TPopupMenu;
     121    SavePictureDialog1: TSavePictureDialog;
    118122    TabSheetOthers: TTabSheet;
    119123    TabSheetHome: TTabSheet;
     
    121125    TabSheetGeneral: TTabSheet;
    122126    TabSheetAll: TTabSheet;
     127    procedure APhotoLoadExecute(Sender: TObject);
     128    procedure APhotoSaveExecute(Sender: TObject);
    123129    procedure ButtonNextClick(Sender: TObject);
    124130    procedure ButtonOkClick(Sender: TObject);
     
    140146    procedure LoadData;
    141147    procedure SaveData;
     148    procedure UpdateInterface;
    142149    property Contact: TContact read FContact write SetContact;
    143150    property OnPrevious: TNotifyEvent read FOnPrevious write FOnPrevious;
     
    166173  FormProperties.Align := alClient;
    167174  FormProperties.Show;
     175  UpdateInterface;
    168176end;
    169177
     
    195203  if FContact = AValue then Exit;
    196204  FContact := AValue;
    197   LoadData;
     205  if Visible then LoadData;
    198206end;
    199207
     
    212220begin
    213221  if Assigned(FOnNext) then FOnNext(Self);
     222end;
     223
     224procedure TFormContact.APhotoLoadExecute(Sender: TObject);
     225begin
     226  if OpenPictureDialog1.Execute then begin
     227    ImagePhoto.Picture.LoadFromFile(OpenPictureDialog1.FileName);
     228  end;
     229end;
     230
     231procedure TFormContact.APhotoSaveExecute(Sender: TObject);
     232begin
     233  if SavePictureDialog1.Execute then begin
     234    ImagePhoto.Picture.SaveToFile(SavePictureDialog1.FileName);
     235  end;
    214236end;
    215237
     
    294316  MemoNotes.Lines.Text := Contact.Fields[cfNote];
    295317
     318  // Photo
    296319  ImagePhoto.Picture.Bitmap.Clear;
    297320  PhotoProperty := Contact.GetProperty(cfPhoto);
     
    326349    end;
    327350  end;
     351  UpdateInterface;
    328352end;
    329353
    330354procedure TFormContact.SaveData;
     355var
     356  Photo: string;
     357  PhotoProperty: TContactProperty;
     358  Stream: TMemoryStream;
     359  JpegImage: TJpegImage;
    331360begin
    332361  // General
     
    382411  // Others
    383412  Contact.Fields[cfNote] := MemoNotes.Lines.Text;
     413
     414  // Photo
     415  if (ImagePhoto.Picture.Bitmap.Width <> 0) and (ImagePhoto.Picture.Bitmap.Height <> 0) then begin
     416    PhotoProperty := Contact.GetProperty(cfPhoto);
     417    if not Assigned(PhotoProperty) then begin
     418      PhotoProperty := TContactProperty.Create;
     419      PhotoProperty.Name := 'PHOTO';
     420      PhotoProperty.Attributes.DelimitedText := 'JPEG';
     421      Contact.Properties.Add(PhotoProperty);
     422    end;
     423    PhotoProperty.Encoding := 'BASE64';
     424    Stream := TMemoryStream.Create;
     425    try
     426      if PhotoProperty.Attributes.IndexOf('JPEG') <> -1 then begin
     427        JpegImage := TJPEGImage.Create;
     428        try
     429          try
     430            JpegImage.SetSize(ImagePhoto.Picture.Bitmap.Width, ImagePhoto.Picture.Bitmap.Height);
     431            JpegImage.Canvas.Draw(0, 0, ImagePhoto.Picture.Bitmap);
     432            JpegImage.SaveToStream(Stream);
     433          except
     434          end;
     435        finally
     436          JpegImage.Free;
     437        end;
     438      end else begin
     439        try
     440          ImagePhoto.Picture.SaveToStream(Stream);
     441        except
     442        end;
     443      end;
     444
     445      SetLength(Photo, Stream.Size);
     446      Stream.Position := 0;
     447      Stream.Read(Photo[1], Length(Photo));
     448      Contact.Fields[cfPhoto] := Photo;
     449    finally
     450      Stream.Free;
     451    end;
     452  end;
     453end;
     454
     455procedure TFormContact.UpdateInterface;
     456begin
     457  APhotoSave.Enabled := (ImagePhoto.Picture.Bitmap.Width <> 0) and
     458    (ImagePhoto.Picture.Bitmap.Height <> 0);
    384459end;
    385460
Note: See TracChangeset for help on using the changeset viewer.