Autor Beitrag
TimKellner
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Di 25.03.08 16:24 
Hallo ich habe ein TJPEGImage welches ich aus einer Datei einlese neu skaliere und verkleinere. Es wird ein TJPEG Image ausgegeben (Dieses wird nicht gespeichert).

Nun möchte ich dieses Bild mit der IdFTP Komponente ins Webladen. Nur dafür brauche ich ja einen Filestream oder eine Datei. Wie bekomme ich das Bild nun hochgeladen ohne es Zwischen zuspeichern?

Gruß
Tim
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Di 25.03.08 18:55 
ein TJPEGImage kennt auch die Methode SaveToStream, die dein Problem lösen dürfte!

_________________
In the beginning was the word.
And the word was content-type: text/plain.
TimKellner Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Di 25.03.08 20:26 
Hi, die Idee / Methode hatte ich auch schon aber das hat nicht so geklappt wie ichs wollte

Abstrakter Fehler

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
procedure TFM_Image_Loader.CompressJPG(AFileName: WideString; var AStream: TStream);
var
  oRBit, oBit: TBitmap;
  oJPG: TJPEGImage;
  iWidth, iHeight: Integer;
  oStream: TStream;
begin
  oRBit := nil;
  oBit := nil;
  oJPG := nil;
  oStream := nil;
  try
    oBit := TBitmap.Create;
    oRBit := TBitmap.Create;
    oJPG := TJPEGImage.Create;
    oJPG.LoadFromFile(AFileName);
    oStream := TStream.Create;

    if oJPG.Height > oJPG.Width then
    begin
      iWidth := PictureHeight;
      iHeight := PictureWidth;
    end
    else
    begin
      iWidth := PictureWidth;
      iHeight := PictureHeight;
    end;

    oBit.Assign(oJPG);
    oRBit.Width := iWidth;
    oRBit.Height := iHeight;
    oRBit.Canvas.StretchDraw(Rect(0, 0, iWidth, iHeight), oBit);
    oJPG.Assign(oRBit);
    oJPG.CompressionQuality := 80;
    oJPG.Compress;

    oJPG.SaveToStream(oStream);
    AStream := oStream;
  finally
    FreeAndNil(oBit);
    FreeAndNil(oRBit);
    FreeAndNil(oJPG);
    FreeAndNil(oStream);
  end;
end;

procedure TFM_Image_Loader.Upload;
var
  oFile: TStream;
begin
  oFile := nil;
  try
    oFile := TStream.Create;
    CompressJPG(FFile, oFile);

    FTP.Username :=...p;HPControl.FFTPUser;
    FTP.Password :=...Control.FFTPPassword;
    FTP.Connect;

    FTP.ChangeDir(FPath);

    FTP.Put(oFile, ...tr(FID) + '.jpg');
  finally
    FreeAndNil(oFile);
  end;

  ModalResult := mrOk;
end;
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 25.03.08 20:36 
du kannst nicht mit TStream direkt arbeiten.
Nimm TMemoryStream, TFileStream, oder sowas.
TimKellner Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Di 25.03.08 21:02 
Hallo ich habe das ganze jetzt in nen TMemoryStream umgeändert aber bei

FTP.Put stürt das ganze mit einer Zugriffsverletzung ab.
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 25.03.08 21:10 
Hab mir den Quellcode grad mal etwas genauer angesehen..
WAS machst du da?

Wieso übergibst du an CompressJPG einen Stream als Var Parameter, aber überschreibst den in Zeile 39? Du brauchst keinen lokalen Stream zum "auslagern" sondern kannst direkt mit dem übergebenen Stream arbeiten.
Außerdem: Schreib das Create jeweils vor das Try, dann kannst du dir die Zuweisung auf nil sparen.

Was ist denn FTP für eine Komponente? Hast du die aufs Formular gezogen, oder erzeugst du die selber? Wenn letzteres: machst du das auch wirklich?
Und: Vor dem Put auf den FTP musst du den Stream evtl. noch "zurückspulen" (Position := 0)

So, glaub das wars erstmal, so auf den ersten Blick.
Wenn danach noch Probleme bestehen, bitte nochmal den aktuellen Quellcode posten ;)

Ganz nebenbei gehört das nun eigentlich schon in einen eigenen Thread, da es eine neue Frage ist...
TimKellner Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 73

Win XP Pro
Delphi 7 Enterprise, RAD Studio
BeitragVerfasst: Mi 26.03.08 09:08 
Hi,

also ich habs jetzt hinbekommen.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
procedure TFM_Image_Loader.CompressJPG(AFileName: WideString; var AStream: TMemoryStream);
var
  oRBit, oBit: TBitmap;
  oJPG: TJPEGImage;
  iWidth, iHeight: Integer;
begin
  oRBit := nil;
  oBit := nil;
  oJPG := nil;
  try
    oBit := TBitmap.Create;
    oRBit := TBitmap.Create;
    oJPG := TJPEGImage.Create;

    oJPG.LoadFromFile(AFileName);

    if oJPG.Height > oJPG.Width then
    begin
      iWidth := PictureHeight;
      iHeight := PictureWidth;
    end
    else
    begin
      iWidth := PictureWidth;
      iHeight := PictureHeight;
    end;

    oBit.Assign(oJPG);
    oRBit.Width := iWidth;
    oRBit.Height := iHeight;
    oRBit.Canvas.StretchDraw(Rect(0, 0, iWidth, iHeight), oBit);
    oJPG.Assign(oRBit);
    oJPG.CompressionQuality := 80;
    oJPG.Compress;

    oJPG.SaveToStream(AStream);
  finally
    FreeAndNil(oBit);
    FreeAndNil(oRBit);
    FreeAndNil(oJPG);
  end;
end;

procedure TFM_Image_Loader.Upload;
var
  oFile: TMemoryStream;
begin
  oFile := nil;
  try
    oFile := TMemoryStream.Create;
    CompressJPG(FFile, oFile);
    oFile.Position := 0;

    FTP.Username :=...p;HPControl.FFTPUser;
    FTP.Password :=...Control.FFTPPassword;
    FTP.Connect;

    FTP.ChangeDir(FPath);

    FTP.Put(oFile, ...tr(FID) + '.jpg');
  finally
    FreeAndNil(oFile);
  end;

  ModalResult := mrOk;
end;


Warum ich das Objekt vor dem Create auf nil setze:

Falls man ein Objekt erzeugt ist

Creaty
try / finally
FreeAndNil

auch richtig.

Aber wenn man zwei oder mehr Objekte erzeugt und das erste Objekt wird erzeugt das zweite gibt einen Fehler, dann kommt er nicht mehr in richtig den try / finally Block und du hast ein Speicherleck.