W
wjh_wy
Unregistered / Unconfirmed
GUEST, unregistred user!
// 版权所有 cjsh
unit MGraphics;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, jpeg, Contnrs;
type
TMultiGraphic = class
private
FGraphics: TObjectList;
function GetCount: Integer;
function GetItem(index: Integer): TGraphic;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Graphic: TGraphic);
procedure Delete(Index: Integer);
procedure LoadFromFile(const FileName: string); virtual;
procedure LoadFromStream(Stream: TStream);
procedure SaveToFile(const FileName: string); virtual;
procedure SaveToStream(Stream: TStream);
property Count: Integer read GetCount;
property Items[index: Integer]: TGraphic read GetItem;
end;
implementation
function ReadInt(Stream: TStream): Word;
begin
Stream.ReadBuffer(Result, SizeOf(Word));
end;
procedure WriteInt(Stream: TStream; I: Word);
begin
Stream.WriteBuffer(I, SizeOf(Word));
end;
function ReadStr(Stream: TStream): string;
var
L: Word;
begin
Stream.ReadBuffer(L, SizeOf(Word));
SetString(Result, PChar(nil), L);
if L > 0 then Stream.ReadBuffer(Result[1], L);
end;
procedure WriteStr(Stream: TStream; const S: string);
var
L: Integer;
begin
L := Length(S);
if L > $FFFF then L := $FFFF;
Stream.WriteBuffer(L, SizeOf(Word));
if L > 0 then Stream.WriteBuffer(S[1], L);
end;
function ReadGraphic(Stream: TStream): TGraphic;
var
L: Int64;
ClassName: string;
InstanceClass: TPersistentClass;
MS: TMemoryStream;
begin
ClassName := ReadStr(Stream);
InstanceClass := FindClass(ClassName);
Result := TGraphic(InstanceClass.NewInstance);
Result.Create;
Stream.ReadBuffer(L, SizeOf(Int64));
if L > 0 then
begin
MS := TMemoryStream.Create;
try
MS.CopyFrom(Stream, L);
MS.Position := soFromBeginning;
Result.LoadFromStream(MS);
finally
MS.Free;
end;
end;
end;
procedure WriteGraphic(Stream: TStream; G: TGraphic);
var
L: Int64;
ClassName: string;
MS: TMemoryStream;
begin
ClassName := G.ClassName;
WriteStr(Stream, ClassName);
MS := TMemoryStream.Create;
try
G.SaveToStream(MS);
L := MS.Size;
Stream.WriteBuffer(L, SizeOf(Int64));
if L > 0 then
begin
MS.Position := soFromBeginning;
Stream.CopyFrom(MS, 0);
end;
finally
MS.Free;
end;
end;
{ TMultiGraphic }
constructor TMultiGraphic.Create;
begin
FGraphics := TObjectList.Create;
end;
destructor TMultiGraphic.Destroy;
begin
FGraphics.Free;
inherited;
end;
procedure TMultiGraphic.Clear;
begin
FGraphics.Clear;
end;
procedure TMultiGraphic.Add(Graphic: TGraphic);
begin
FGraphics.Add(Graphic);
end;
procedure TMultiGraphic.Delete(Index: Integer);
begin
FGraphics.Delete(Index);
end;
function TMultiGraphic.GetCount: Integer;
begin
Result := FGraphics.Count;
end;
function TMultiGraphic.GetItem(index: Integer): TGraphic;
begin
Result := FGraphics[index] as TGraphic;
end;
procedure TMultiGraphic.LoadFromStream(Stream: TStream);
var
lCount: Word;
I: Integer;
G: TGraphic;
begin
Clear;
lCount := ReadInt(Stream);
for I := 0 to lCount-1 do
begin
G := ReadGraphic(Stream);
Add(G);
end;
end;
procedure TMultiGraphic.SaveToStream(Stream: TStream);
var
lCount: Word;
I: Integer;
G: TGraphic;
begin
lCount := Count;
WriteInt(Stream, lCount);
for I := 0 to lCount-1 do
begin
G := Items;
WriteGraphic(Stream, G);
end;
end;
procedure TMultiGraphic.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TMultiGraphic.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
begin
RegisterClasses([TBitmap, TIcon, TMetafile, TJPEGImage]);
end.
unit MGraphics;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, jpeg, Contnrs;
type
TMultiGraphic = class
private
FGraphics: TObjectList;
function GetCount: Integer;
function GetItem(index: Integer): TGraphic;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Graphic: TGraphic);
procedure Delete(Index: Integer);
procedure LoadFromFile(const FileName: string); virtual;
procedure LoadFromStream(Stream: TStream);
procedure SaveToFile(const FileName: string); virtual;
procedure SaveToStream(Stream: TStream);
property Count: Integer read GetCount;
property Items[index: Integer]: TGraphic read GetItem;
end;
implementation
function ReadInt(Stream: TStream): Word;
begin
Stream.ReadBuffer(Result, SizeOf(Word));
end;
procedure WriteInt(Stream: TStream; I: Word);
begin
Stream.WriteBuffer(I, SizeOf(Word));
end;
function ReadStr(Stream: TStream): string;
var
L: Word;
begin
Stream.ReadBuffer(L, SizeOf(Word));
SetString(Result, PChar(nil), L);
if L > 0 then Stream.ReadBuffer(Result[1], L);
end;
procedure WriteStr(Stream: TStream; const S: string);
var
L: Integer;
begin
L := Length(S);
if L > $FFFF then L := $FFFF;
Stream.WriteBuffer(L, SizeOf(Word));
if L > 0 then Stream.WriteBuffer(S[1], L);
end;
function ReadGraphic(Stream: TStream): TGraphic;
var
L: Int64;
ClassName: string;
InstanceClass: TPersistentClass;
MS: TMemoryStream;
begin
ClassName := ReadStr(Stream);
InstanceClass := FindClass(ClassName);
Result := TGraphic(InstanceClass.NewInstance);
Result.Create;
Stream.ReadBuffer(L, SizeOf(Int64));
if L > 0 then
begin
MS := TMemoryStream.Create;
try
MS.CopyFrom(Stream, L);
MS.Position := soFromBeginning;
Result.LoadFromStream(MS);
finally
MS.Free;
end;
end;
end;
procedure WriteGraphic(Stream: TStream; G: TGraphic);
var
L: Int64;
ClassName: string;
MS: TMemoryStream;
begin
ClassName := G.ClassName;
WriteStr(Stream, ClassName);
MS := TMemoryStream.Create;
try
G.SaveToStream(MS);
L := MS.Size;
Stream.WriteBuffer(L, SizeOf(Int64));
if L > 0 then
begin
MS.Position := soFromBeginning;
Stream.CopyFrom(MS, 0);
end;
finally
MS.Free;
end;
end;
{ TMultiGraphic }
constructor TMultiGraphic.Create;
begin
FGraphics := TObjectList.Create;
end;
destructor TMultiGraphic.Destroy;
begin
FGraphics.Free;
inherited;
end;
procedure TMultiGraphic.Clear;
begin
FGraphics.Clear;
end;
procedure TMultiGraphic.Add(Graphic: TGraphic);
begin
FGraphics.Add(Graphic);
end;
procedure TMultiGraphic.Delete(Index: Integer);
begin
FGraphics.Delete(Index);
end;
function TMultiGraphic.GetCount: Integer;
begin
Result := FGraphics.Count;
end;
function TMultiGraphic.GetItem(index: Integer): TGraphic;
begin
Result := FGraphics[index] as TGraphic;
end;
procedure TMultiGraphic.LoadFromStream(Stream: TStream);
var
lCount: Word;
I: Integer;
G: TGraphic;
begin
Clear;
lCount := ReadInt(Stream);
for I := 0 to lCount-1 do
begin
G := ReadGraphic(Stream);
Add(G);
end;
end;
procedure TMultiGraphic.SaveToStream(Stream: TStream);
var
lCount: Word;
I: Integer;
G: TGraphic;
begin
lCount := Count;
WriteInt(Stream, lCount);
for I := 0 to lCount-1 do
begin
G := Items;
WriteGraphic(Stream, G);
end;
end;
procedure TMultiGraphic.LoadFromFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TMultiGraphic.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
begin
RegisterClasses([TBitmap, TIcon, TMetafile, TJPEGImage]);
end.