Einzelnen Beitrag anzeigen

capo

Registriert seit: 7. Jul 2006
453 Beiträge
 
Delphi 11 Alexandria
 
#17

AW: Quadratische Thumbnails erstellen

  Alt 2. Nov 2010, 15:27
Hallo,
ich habe mitlerweile eine Lösung gefunden, leider sehen die Thumbs entweder zu pixeli oder zu unscharf aus.
Hat jemand eine Idee?
So sieht mein Code momentan aus:

Delphi-Quellcode:
procedure SquareMyJPG(JPGFilePathSource, JPGFilePathDest: string;
  SquareLengthPx: Integer);
var
  JPGOrig, JPGThumbSquare: TJPEGImage;
  BMPThumbNoSquare, BMPThumbSquare: TBitmap;
  IsLandscape: Boolean;
begin
  JPGOrig := TJPEGImage.Create;
  try
    JPGOrig.LoadFromFile(JPGFilePathSource);

    IsLandscape := JPGOrig.Width > JPGOrig.Height;

    BMPThumbNoSquare := TBitmap.Create;
    try
      // Thumbnail proportional skalieren
      if IsLandscape then
      begin
        BMPThumbNoSquare.Height := SquareLengthPx;
        BMPThumbNoSquare.Width := Round(SquareLengthPx * JPGOrig.Width /
          JPGOrig.Height);
      end
      else
      begin
        BMPThumbNoSquare.Height := Round(SquareLengthPx * JPGOrig.Height /
          JPGOrig.Width);
        BMPThumbNoSquare.Width := SquareLengthPx;
      end;

      BMPThumbNoSquare.Canvas.StretchDraw(
        Rect(0, 0, BMPThumbNoSquare.Width, BMPThumbNoSquare.Height),
        JPGOrig
        );

      // Überflüssiges wegschnibbeln
      BMPThumbSquare := TBitmap.Create;
      try

        BMPThumbSquare.Width := SquareLengthPx;
        BMPThumbSquare.Height := SquareLengthPx;

        {
        if not IsLandscape then
          BMPThumbSquare.Canvas.Draw(
            -(BMPThumbNoSquare.Width - BMPThumbNoSquare.Height) div 2,
            0,
            BMPThumbNoSquare
          )
        else
          BMPThumbSquare.Canvas.Draw(
            -(BMPThumbNoSquare.Height - BMPThumbNoSquare.Width) div 2,
            0,
            BMPThumbNoSquare
          );
        }

        BMPThumbSquare.Canvas.CopyRect(Rect(0, 0, SquareLengthPx,
          SquareLengthPx), BMPThumbNoSquare.Canvas, Rect(0, 0, SquareLengthPx,
          SquareLengthPx));

        BMPThumbSquare.PixelFormat := pf24Bit;
        // smoothH(BMPThumbSquare);
         // smoothV(BMPThumbSquare);
        JPGThumbSquare := TJPEGImage.Create;
        try
          // JPGThumbSquare.Width := SquareLengthPx;
         // JPGThumbSquare.Height := SquareLengthPx;

          Antialiasing(BMPThumbSquare, Rect(0, 0, BMPThumbSquare.Width, BMPThumbSquare.Height), 10);
          // Sharpen(BMPThumbSquare,BMPThumbSquare,2);
          JPGThumbSquare.Assign(BMPThumbSquare);
          JPGThumbSquare.CompressionQuality := 100;
          JPGThumbSquare.Compress;
          JPGThumbSquare.Smoothing := not JPGThumbSquare.Smoothing;
          JPGThumbSquare.SaveToFile(JPGFilePathDest);

        finally
          FreeAndNil(JPGThumbSquare);
        end;
      finally
        FreeAndNil(BMPThumbSquare);
      end;
    finally
      // BMPThumbNoSquare.SaveToFile('C:\bmpnosquare.bmp');
      FreeAndNil(BMPThumbNoSquare);
    end;
  finally

    FreeAndNil(JPGOrig);
  end;
end;
Hotte
  Mit Zitat antworten Zitat