Autor Beitrag
MrFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 02.03.06 21:47 
Hallo,

Ich möchte eigentlich nur ganz simpel ein Bitmap ohne Dateizugriff (Warum poste ich eigentlich unter Dateizugriff? - naja, wegen der Streams), also im Speicher, komprimieren.
Dazu habe ich mir gedacht nehme ich einen TMemoryStream und speichere die Bitmap per SaveToStream.
Nun habe ich aber noch keine einzige unter Delphi 7 funktionierende Unit gefunden die eine Funktion wie CompressStream(AStream:TStream) bereithält.

Also meine Frage: Wer kennt eine Unit die eine solche Funktion beeinhaltet...?
--Bitte nicht auf die Forumssuche verweisen, habe sie nämlich bereits genutzt, mit dem Ergebnis, dass ich jetzt den Wald vor lauter Bäumen nicht mehr sehe--

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Do 02.03.06 21:50 
Hallo.

In der Unit ZLib gibt es die Klassen TCompressionStream und TDecompressionStream, sie sind jedoch etwas gewöhnungsbedürftig.

Cu,
Udontknow
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Do 02.03.06 21:55 
Genau die kann man irgendwie nicht verwenden, die schlucken das SaveToStream nicht. Es passiert ganz einfach gar nichts, der Stream bleibt wie zuvor.

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 02.03.06 23:11 
ausblenden Delphi-Quelltext
1:
2:
With MissingSource Do
    Fix(Problem);


ausblenden Quelltext
1:
EGlaskugelATMaufWeltreiseException					

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 03.03.06 00:15 
@user profile iconBenBE: :lol: :rofl: :D ...ist der gut... :!:

_________________
There are 10 types of people - those who understand binary and those who don´t.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 08:52 
Für alle, die den Sourcecode brauchen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TmemoryStream;
CStream: TCompressionStream;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
Bitmap.LoadFromFile(Desktop+'\Bildso.bmp');

Stream := TMemoryStream.Create;
CStream := TCOmpressionStream.Create(clMax,Stream);

Bitmap.SaveToStream(Stream);
Stream.SaveToFIle(Desktop+'\Bild_komprimiert.bmp');

Cstream.Free;
Stream.Free;
Bitmap.Free;
end;


Nach Ausführung dieser Prozedur habe ich eine Kopie des Bildes mit anderem Namen auf dem Desktop - komprimiert ist da nichts.

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 03.03.06 09:00 
Ich hba jetzt nicht ganz die Ahnung gavon, aber wäre die Reihenfolge nicht sinniger?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Bitmap := TBitmap.Create;   
Stream := TMemoryStream.Create;   
Bitmap.LoadFromFile(Desktop+'\Bildso.bmp');   

// Bitmap im Stream speichern
// Warum eigentlich erst das Bitmap laden, und dann in den Stream speichern???
// Warum nicht direkt die BitmapDatei in den Stream laden?
Bitmap.SaveToStream(Stream);   
// Stream komprimieren
CStream := TCOmpressionStream.Create(clMax,Stream);   
// Komprimierten Stream speichern
C{?}Stream.SaveToFIle(Desktop+'\Bild_komprimiert.bmp');   

Cstream.Free;   
Stream.Free;   
Bitmap.Free;

_________________
We are, we were and will not be.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 09:09 
Das ganze ist eine Beispielprozedur, im echten Programm muss ich die Bitmap garnicht laden, sie wird während der Laufzeit vom Benutzer erstellt.

Leider funktioniert deine Variante auch nicht...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Bitmap := TBitmap.Create;
Stream := TMemoryStream.Create;
Bitmap.LoadFromFile(Desktop+'\Bildso.bmp');

// Bitmap im Stream speichern
// Warum eigentlich erst das Bitmap laden, und dann in den Stream speichern???
// Warum nicht direkt die BitmapDatei in den Stream laden?
Bitmap.SaveToStream(Stream);
// Stream komprimieren
CStream := TCOmpressionStream.Create(clMax,Stream);
// Komprimierten Stream speichern
CStream.SaveToFIle(Desktop+'\Bild_komprimiert.bmp');

Cstream.Free;
Stream.Free;
Bitmap.Free;


Der CompressionStream kennt SaveToFile nicht.

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Fr 03.03.06 09:22 
user profile iconMrFox hat folgendes geschrieben:
Der CompressionStream kennt SaveToFile nicht.

Nee stimmt, aber wozu auch, er ist ja ein Compressionstream.
Also: Bitmapstream -> CompressionStream -> FileStream
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
...
  Bitmap := TBitmap.Create;
  Stream := TMemoryStream.Create;
  FStream := TFileStream.Create (Desktop+'\Bildso.comprimiert', fmCreate);
  Try
    Bitmap.LoadFromFile(Desktop+'\Bildso.bmp');
    Bitmap.SaveToStream(Stream);
    CStream := TCOmpressionStream.Create(clMax,Stream);
    Try
      FStream.From (cStream, 0);
    finally
      CStream.Free;
    End;
  Finally
    fStream.Free;
    Stream.Free;
    Bitmap.Free;
  End;
...

_________________
Na denn, dann. Bis dann, denn.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 09:34 
Ich habe die nebulöse Anweisung FStream.From jetzt mal durch FStream.CopyFrom ersetzt.
Das funtkioniert aber leider auch nicht, da gibt's einen Laufzeitfehler ("Invalid Stream Operation").

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Fr 03.03.06 09:42 
Hupsa, das mit dem 'From' ist ja merkwürdig. Muss an der Uhrzeit liege... Na gut, dann unterstützt dieser TCompressionStream das eben nicht. Mehr fällt mir auch nicht ein. Du kannst ja versuchen, die Daten aus dem TCompressionStream per Hand in den TFileStream zu kopieren (so in Blocks a 64k) etwas fundamental anderes macht CopyFrom auch nicht

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Procedure MyStreamCopy (aSrc, aDst : TStream);
Var
  aBuffer : Array [0..32767] Of Byte; // Ja ja, das macht man nicht auf dem Stack

Begin
  Repeat
    n := aSrc.Read (aBuffer, SizeOf (abuffer));
    if n>0 Then aDst.Write (aBuffer, n);
  Until n <> SizeOf (abuffer);
End;

_________________
Na denn, dann. Bis dann, denn.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 09:48 
Auch das funktioniert nicht, gleiche Invalid Stream Operation. :(
Ich glaub das kann alles auch gar nicht gehen, denn die Auto-Vervollständigung bewies mir soeben, dass Der Compr.Stream nur ca. 10 Methoden hat. Nur Create, Free und dieses ganze von TObject geerbte Zeug. Read bzw. Write waren nicht darunter.

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Jailbird
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 127

Windows XP Pro SP2
Delphi 7 Professional
BeitragVerfasst: Fr 03.03.06 10:30 
Wenn die N00bs scheitern muss der Pro ran :P :P ;)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var
  Bitmap: TBitmap;
  FStream: TFileStream;
  CStream: TCompressionStream;
begin
  FStream := TFileStream.Create('datei_komprimiert.bmp', fmCreate);
  Bitmap := TBitmap.Create;
  CStream := TCompressionStream.Create(clMax, FStream);

  Bitmap.LoadFromFile('datei.bmp');
  Bitmap.SaveToStream(CStream);

  CStream.Free;
  Bitmap.Free;
  FStream.Free;
end;


Edit: Optimiert mittels Kommentar von user profile iconUdontknow


Zuletzt bearbeitet von Jailbird am Fr 03.03.06 13:57, insgesamt 1-mal bearbeitet
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 11:09 
Danke es funktioniert. D.h. der CompressionStream komprimiert nur das, was man mit Write in ihn reinschreibt?

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Jailbird
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 127

Windows XP Pro SP2
Delphi 7 Professional
BeitragVerfasst: Fr 03.03.06 11:27 
Ja...ich zitiere hier die Delphi-Hilfe:

Zitat:
TCompressionStream is a write-only, sequential access stream that compresses data as it is written. The compressed data is written to a separate stream, which is passed to the TCompressionStream constructor.


Heisst: Der Stream, welcher beim Kontruktor (Create) übergeben wird, ist der output-Stream. Der Input muss über Write geliefert werden und es macht keinen Sinn, dass dies der gleiche Stream ist wie der Output-Stream.
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 03.03.06 13:33 
@Jailbird:

Ich würde spontan bei deinem Code den MemoryStream weglassen und das Bild direkt in den CStream schreiben.

@Mr Fox:

Bei deinem Code erzeugst du zwar den CompressionStream, nutzt aber bei der Methode Bitmap.SaveToStream den normalen Stream, deshalb bleibt alles beim alten.

Aus
Bitmap.SaveToStream(Stream);
muss
ausblenden Delphi-Quelltext
1:
Bitmap.SaveToStream(CStream);					

werden.

Cu,
Udontknow
Jailbird
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 127

Windows XP Pro SP2
Delphi 7 Professional
BeitragVerfasst: Fr 03.03.06 13:56 
user profile iconUdontknow hat folgendes geschrieben:
@Jailbird:

Ich würde spontan bei deinem Code den MemoryStream weglassen und das Bild direkt in den CStream schreiben.


Ja, das wäre wohl optimaler. Ich hab lediglich aus dem bereits Vorhandenen etwas Funktionierendes gebastelt. Hab nie gesagt, dass es optimal ist
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.03.06 14:03 
@UDontKnow: Deine Methode könnte zu Problemen führen, da die VCL Bitmaps nicht immer wirklich Sequenziell schreibt. Von daher ist die Methode über den Zwischen-Memory-Stream die sicherere (Zumindest für TReader\TWriter weiß ich, dass es Probleme gibt, bei TPicture-Nachfahren bin ich mir ATM nicht völlig sicher).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
MrFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 208

WIN 2000 Professional, Ubuntu 5.10
D3 Prof, D7 Pers, D2005 Pers, Java (Eclipse)
BeitragVerfasst: Fr 03.03.06 14:48 
Ich hab jetzt einfach mal zwei Funktionen geschrieben...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function CompressBitmapToStream(ABitmap: TBitmap): TMemoryStream;
var OutputStream: TMEmoryStream;
    CStream: TCompressionStream;
    TempStream: TMemoryStream;
begin
OutPutStream := TMemoryStream.Create;
TempStream := TMemoryStream.Create;
Abitmap.SaveToStream(TempStream);
  try
   CStream := TCOmpressionStream.Create(clMax,OutputStream);

   TempStream.Position := 0;
   Cstream.Write(TempStream.Memory^,SizeOf(TempStream));
  finally
   CStream.Free;
   TempStream.Free;
  end;
Result := OutputStream;
end;

Die erste Funktion funzt meines Erachtens.

Nur die zweite, die Umkehrung bekomm ich nicht so richtig hin...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function DecompressBitmapFromStream(InputStream: TMemoryStream): TBitmap;
var DStream: TDeCompressionStream;
    TempStream: TMemoryStream;
begin
Result := TBitmap.Create;
TempStream:= TMemoryStream.Create;
   try
   DStream := TDeCompressionStream.Create(InputStream);

   ...

   finally
   DStream.Free;
   end;
Result.LoadFromStream(TempStream);
TempStream.Free;
end;

_________________
Das Leben auf der Erde mag zwar teuer sein, aber eine jährliche Rundreise um die Sonne ist gratis mit dabei.
Jailbird
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 127

Windows XP Pro SP2
Delphi 7 Professional
BeitragVerfasst: Fr 03.03.06 14:57 
Bei der Decompression ist's genau umgekehrt...ich schreib mal pseudomässig, wie's funzt.

Lade komp. Datei in TFileStream (z.B.)
Create den TDecompressionStream mit dem FileStream als Attribut
Wähle Bitmap.LoadFromStream(decStream)
Free