Autor Beitrag
JustusJonas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 222



BeitragVerfasst: Do 13.04.06 11:41 
Hallo Leute,

ich möchte gerne ein Tool schreiben um alle Bilder in einem Ordner zu verkleinern.

Bisher hab ich eine ListBox, in die alle Bilder geladen und angezeigt werden.

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:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
procedure TForm1.MakeThumbNail(src, dest: TBitmap; ThumbSize: Word);
type
  PRGB24 = ^TRGB24;
  TRGB24 = packed record
    B: Byte;
    G: Byte;
    R: Byte;
  end;
var
  x, y, ix, iy: integer;
  x1, x2, x3: integer;

  xscale, yscale: single;
  iRed, iGrn, iBlu, iRatio: Longword;
  p, c1, c2, c3, c4, c5: tRGB24;
  pt, pt1: pRGB24;
  iSrc, iDst, s1: integer;
  i, j, r, g, b, tmpY: integer;

  RowDest, RowSource, RowSourceStart: integer;
  w, h: integer;
  dxmin, dymin: integer;
  ny1, ny2, ny3: integer;
  dx, dy: integer;
  lutX, lutY: array of integer;

begin
  if src.PixelFormat <> pf24bit then src.PixelFormat := pf24bit;
  if dest.PixelFormat <> pf24bit then dest.PixelFormat := pf24bit;
  dest.Width := ThumbSize;
  dest.Height := ThumbSize;
  w := ThumbSize;
  h := ThumbSize;

  if (src.Width <= ThumbSize) and (src.Height <= ThumbSize) then
  begin
    dest.Assign(src);
    exit;
  end;

  iDst := (w * 24 + 31) and not 31;
  iDst := iDst div 8; //BytesPerScanline
  iSrc := (Src.Width * 24 + 31) and not 31;
  iSrc := iSrc div 8;

  xscale := 1 / (w / src.Width);
  yscale := 1 / (h / src.Height);

  // X lookup table
  SetLength(lutX, w);
  x1 := 0;
  x2 := trunc(xscale);
  for x := 0 to w - 1 do
  begin
    lutX[x] := x2 - x1;
    x1 := x2;
    x2 := trunc((x + 2) * xscale);
  end;

  // Y lookup table
  SetLength(lutY, h);
  x1 := 0;
  x2 := trunc(yscale);
  for x := 0 to h - 1 do
  begin
    lutY[x] := x2 - x1;
    x1 := x2;
    x2 := trunc((x + 2) * yscale);
  end;

  dec(w);
  dec(h);
  RowDest := integer(Dest.Scanline[0]);
  RowSourceStart := integer(Src.Scanline[0]);
  RowSource := RowSourceStart;
  for y := 0 to h do
  begin
    dy := lutY[y];
    x1 := 0;
    x3 := 0;
    for x := 0 to w do
    begin
      dx := lutX[x];
      iRed := 0;
      iGrn := 0;
      iBlu := 0;
      RowSource := RowSourceStart;
      for iy := 1 to dy do
      begin
        pt := PRGB24(RowSource + x1);
        for ix := 1 to dx do
        begin
          iRed := iRed + pt.R;
          iGrn := iGrn + pt.G;
          iBlu := iBlu + pt.B;
          inc(pt);
        end;
        RowSource := RowSource - iSrc;
      end;
      iRatio := 65535 div (dx * dy);
      pt1 := PRGB24(RowDest + x3);
      pt1.R := (iRed * iRatio) shr 16;
      pt1.G := (iGrn * iRatio) shr 16;
      pt1.B := (iBlu * iRatio) shr 16;
      x1 := x1 + 3 * dx;
      inc(x3, 3);
    end;
    RowDest := RowDest - iDst;
    RowSourceStart := RowSource;
  end;

  if dest.Height < 3 then exit;

  // Sharpening...
  s1 := integer(dest.ScanLine[0]);
  iDst := integer(dest.ScanLine[1]) - s1;
  ny1 := Integer(s1);
  ny2 := ny1 + iDst;
  ny3 := ny2 + iDst;
  for y := 1 to dest.Height - 2 do
  begin
    for x := 0 to dest.Width - 3 do
    begin
      x1 := x * 3;
      x2 := x1 + 3;
      x3 := x1 + 6;

      c1 := pRGB24(ny1 + x1)^;
      c2 := pRGB24(ny1 + x3)^;
      c3 := pRGB24(ny2 + x2)^;
      c4 := pRGB24(ny3 + x1)^;
      c5 := pRGB24(ny3 + x3)^;

      r := (c1.R + c2.R + (c3.R * -12) + c4.R + c5.R) div -8;
      g := (c1.G + c2.G + (c3.G * -12) + c4.G + c5.G) div -8;
      b := (c1.B + c2.B + (c3.B * -12) + c4.B + c5.B) div -8;

      if r < 0 then r := 0 else if r > 255 then r := 255;
      if g < 0 then g := 0 else if g > 255 then g := 255;
      if b < 0 then b := 0 else if b > 255 then b := 255;

      pt1 := pRGB24(ny2 + x2);
      pt1.R := r;
      pt1.G := g;
      pt1.B := b;
    end;
    inc(ny1, iDst);
    inc(ny2, iDst);
    inc(ny3, iDst);
  end;
end;


Und den Funktionsaufruf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TForm1.btnDoItClick(Sender: TObject);
var
i                                     : Integer;
dest                                  : TBitmap;
begin
for i := 0 to lbBilder.Items.Count -1 do
  begin
  dest := TBitmap.Create;
  Image1.Picture.LoadFromFile(Folder + lbBilder.Items[i]);
  try
    MakeThumbNail(Image1.Picture.Bitmap, dest, seGroesse.Value);
    Image2.Picture.Bitmap.Assign(dest);
    Image2.Picture.SaveToFile(Folder + copy(lbBilder.Items[i],0,length(lbBilder.Items[i]) -4) + '_thumb.jpg');
  finally
    dest.Free;
  end;
  end;
end;


Er erstellt das Bild dann auch, aber leider hat es 0k und man kann es natürlich nicht öffnen.

Ich find den Fehler einfach nicht. Kann mir jemand von euch helfen?

Greetz


Moderiert von user profile iconraziel: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Do 13.04.2006 um 13:07
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 13.04.06 19:25 
Also bei mir geht das. Wo setzt du Folder? hat das auch ein \ am Ende?

Zitat:

'_thumb.jpg'

Aber ein Jpeg erzeugst du nicht, du speicherst immer noch ein Bitmap!, die Dateiendung ändert doch nichts am eigentlichem Dateiformat.

in deiner Buttonklick würde cih das Bitmap nicht innerhalb der schleife jedesmal neu erezugen, bei vielen Bilder wird dich das Zeit kosten, 1xaußerhalb der Schleife erzeugen reicht.

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Philipp_Reitter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 430



BeitragVerfasst: Do 13.04.06 21:05 
das geht dock viel einfacher auch...

wenn du Jpeg's verkleiner willst dann lass es so und sonst ersetze es mit BMP

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
uses jpeg;

Procedure BildformatAendern(AltesFile:, NeuesFile: String; NewWidth, NewHeight: Integer);
var JPEG: TJPEGImage;
    JPEG2: TJPEGImage;  // das mit der neuen größe
begin
  JPEG:=TJPEGImage.Create;
  JPEG.LoadFromFile(AltesFile);


  JPEG2:=TJPEGImage.Create;
  Jpeg2.Height := NewHeight;
  Jpeg2.Width  := NewWidth;

  Jpeg2.Canvas.StretchDraw(Rect(0,0,BMP.Width,BMP.Height),JPEG);
  //JPEG2.Assign(Jpeg); (kann sein dass man das hinschreiben muss habs jetzt nur aus dem gedächtnis

  JPEG2.SaveToFile(NeuesFile);
  JPEG.Free;
  Jpg2.Free;

end;


kann sein dass fehler drinnen sind aber ansonsten haut das ganz gut hin...
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Fr 14.04.06 11:21 
user profile iconPhilipp_Reitter hat folgendes geschrieben:
das geht dock viel einfacher auch...

klar geht das auch einfacher. Aber probiers mal aus, mit canvas.stretchdraw sind die ergebnisse öfters grauenvoll, gerade bei extremen Verkleinerungen. Der original code interpoliert die Bilder und die thumbs sehen um einiges besser aus.

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)