14/08/11 19:29
Goblin
allora .. in base alla richiesta di Track posto 2 righe di come farei io in un applicazione con solo 1 form, in quanto ho deciso di utilizzare una variabile public della form.
la form è semplice 2 componenti TImage (image1 sorgente e image2 destinazione), 2 Tbutton (button1 carica nello stream button2 scarica dallo stream al componente image).
Ho deciso di discernere i tipi di immagine dunque nelle uses bisogna aggiungere jpeg,pngimage in modo da gestire oltre le bmp anche png e jpeg(jpg)
dichiaro la variabile.
public
foStreamImage: TMemoryStream;
creazione e distruzione oggetto stream
procedure TForm1.FormCreate(Sender: TObject);
begin
foStreamImage := TMemoryStream.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(foStreamImage) then
FreeAndNil(foStreamImage);
end;
Button1click carico nello stream l'immagine
procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.Graphic.SaveToStream(foStreamImage);
end;
button2click scrivo lo stream in image scegliendo il tipo di immagine
procedure TForm1.Button2Click(Sender: TObject);
var
FirstBytes: AnsiString;
Graphic: TGraphic;
begin
Graphic := nil;
Screen.Cursor := crHourGlass;
Image2.Picture := Nil;
foStreamImage.Seek(0, soFromBeginning ); // reset stream
try
SetLength(FirstBytes, 8);
foStreamImage.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = 'BM' then
Graphic := TBitmap.Create
else if FirstBytes = #137'PNG'#13#10#26#10 then
Graphic := TPngImage.Create
else if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
Graphic := TJPEGImage.Create;
if Assigned(Graphic) then
begin
try
foStreamImage.Seek(0, soFromBeginning );
Graphic.LoadFromStream(foStreamImage);
Image2.Picture.Assign(Graphic);
except
// gestione eccezione
end;
end
else
ShowMessage('Formato immagine non riconosciuto');
finally
Screen.Cursor := crDefault;
Graphic.Free;
end;
Image2.Refresh;
end;
..... il tutto ovviamente è migliorabile per mezzo di una property con funzioni get/set in modo da evitare il button1 ... lo lascio come compito per le vacanze a chi lo vuole svolgere
un saluto e spero di non aver detto troppe cavolate
G.
Ultima modifica effettuata da Goblin 14/08/11 19:35
Ibis redibis non morieris in bello