unit ClientMainUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DBXDataSnap, IPPeerClient, Data.DBXCommon, Data.DB,
  Data.SqlExpr, Vcl.StdCtrls, Vcl.DBCtrls, Vcl.Grids, Vcl.DBGrids,ClientMethods, Datasnap.DBClient,
  Datasnap.DSConnect, ClientMethods1, Datasnap.Provider, Vcl.ExtCtrls;

type
  TForm2 = class(TForm)
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    DSProviderConnection1: TDSProviderConnection;
    SQLConnection1: TSQLConnection;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    OpenDialog1: TOpenDialog;
    Button3: TButton;
    Image1: TImage;
    Button4: TButton;
    Button5: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var bmp:tbitmap; twic:twicimage;
begin

bmp:=tbitmap.Create; twic:=twicimage.Create;

opendialog1.Execute;
twic.LoadFromFile(opendialog1.FileName);
bmp.Assign(twic);


clientdataset1.append;
clientdataset1.FieldByName('Primary_key').AsInteger:=clientdataset1.RecordCount+1;
clientdataset1.FieldByName('Picture').Assign(bmp);
clientdataset1.Post;
clientdataset1.ApplyUpdates(0);

//clientdataset1.Refresh;
clientdataset1.Close;
clientdataset1.Open;

end;

procedure TForm2.Button2Click(Sender: TObject);
begin
clientdataset1.Delete;
clientdataset1.ApplyUpdates(-1);
end;

procedure TForm2.Button3Click(Sender: TObject);
var bmp:tbitmap;

begin
  image1.Picture:=nil;

  bmp:=tbitmap.Create;
  bmp.Assign(clientdataset1.FieldByName('Picture'));
  bmp.AlphaFormat:=afDefined;
  bmp.Transparent:=true;


  image1.Picture.Assign(bmp);
  bmp.Free

end;

procedure TForm2.Button4Click(Sender: TObject);
var SMC:TServerMethods1Client; ms:tmemorystream;
    twic:twicimage; bmp:tbitmap;
begin
smc:=TServerMethods1Client.Create(sqlconnection1.DBXConnection);
ms:=tmemorystream.Create;
bmp:=tbitmap.Create; twic:=twicimage.Create;

opendialog1.Execute;

twic.LoadFromFile(opendialog1.FileName);
bmp.Assign(twic);


bmp.SaveToStream(ms);

if ms<>nil then showmessage(inttostr(ms.Size));

smc.GetPicture(ms);


end;

procedure TForm2.Button5Click(Sender: TObject);
var SMC:TServerMethods1Client; ms:tmemorystream;
    twic:twicimage; bmp:tbitmap; ss:tstringstream; s:widestring;
begin
smc:=TServerMethods1Client.Create(sqlconnection1.DBXConnection);

ss:=tstringstream.Create;
bmp:=tbitmap.Create; twic:=twicimage.Create;

opendialog1.Execute;

twic.LoadFromFile(opendialog1.FileName);
bmp.Assign(twic);

bmp.SaveToStream(ss);

s:=ss.DataString;

// Проверка длины строки на клиенте...
showmessage('string on client '+inttostr(length(s)));

smc.GetString(s);

clientdataset1.Refresh;
clientdataset1.Close;
clientdataset1.Open;

end;

procedure TForm2.FormCreate(Sender: TObject);
begin
clientdataset1.Open;
end;

end.
