Dette er et lite knippe Delphi triks lagret online mest for min egen del, men Google er snille mot oss.

ImageMagick, for fancy billedbehandlig.

ImageMagick for Delphi & varianter ligger her:
http://sourceforge.net/project/showfiles.php?group_id=92177&package_id=174103&release_id=431432

Se også:
http://wiki.lazarus.freepascal.org/PascalMagick

Alle API kall ligger beskrevet her:
http://imagemagick.org/script/magick-wand.php

  • Lage et bilde:
  • uses
      DFCreatorPilot3Lib_TLB, Magick_wand, ImageMagick;
    
    procedure ThrowWandException(wand: PMagickWand);
    var
      description : PChar;
      severity : ExceptionType;
    begin
      description:= MagickGetException(wand, @severity);
      MessageBox(Form1.Handle, PChar(Format('An error ocurred. Description: %s', [description])), 'ImageMagick', 0);
      MagickRelinquishMemory(description);
    end;
    
    var
      wand : PMagickWand;
      pixelwand : PPixelWand;
      status : MagickBooleanType;
    begin
      MagickWandGenesis;
    
      // Some bugs here...
    
      wand:= NewMagickWand;
      pixelwand:= NewPixelWand;
      PixelSetColor(pixelwand, 'white');
    
      status:= MagickNewImage(wand, newwidth, newheight, pixelwand);
      if status = MagickFalse then
        ThrowWandException(wand);
    
      DestroyPixelWand(pixelwand);
    
      // Even more bugs here...
    
      MagickWandTerminus;
    end;
    
  • Lese et bilde fra fil:
  • begin
      status:= MagickReadImage(wand, 'bilde.jpg');
      if status = MagickFalse then
        ThrowWandException(wand);
    end;
    
  • Sette et bilde (tempwand) inn i et annet (wand):
  • begin
      status:= MagickCompositeImage(wand, tempwand, AtopCompositeOp, x-pos, y-pos);
      if status = MagickFalse then
        ThrowWandException(wand);
    end;
    
  • Ta et utsnitt av et bilde:
  • begin
      status:= MagickCropImage(wand, width, height, x-pos, y-pos);
      if status = MagickFalse then
        ThrowWandException(wand);
    end;
    
  • Fjerne alt 'hvitt' rundt et bilde:
  • begin
      status:= MagickTrimImage(wand, 0);
      if status = MagickFalse then
        ThrowWandException(wand);
    end;
    
  • Endre størrelse på et bilde:
  • begin
      status:= MagickScaleImage(wand, new-width, new-height);
      if status = MagickFalse then
        ThrowWandException(wand);
    
    // Andre muligheter:
    //  status:= MagickResizeImage(wand, new-width, new-height, LanczosFilter, 0);
    //  status:= MagickSampleImage(wand, new-width, new-height);
    
    end;
    
  • Frigjøre et bilde fra minne:
  • begin
     if IsMagickWand(wand) = MagickTrue then
       DestroyMagickWand(wand);
    end;
    
  • Bruke ImageMagick og PDF Creator Pilot sammen via minne:
  • http://www.colorpilot.com/pdflibrary.html

    var
      wand : PMagickWand;
      status : MagickBooleanType;
      PDF : TPDFDocument3; // Not tested on TPDFDocument4, should be the same...
      t : integer;
      b : pointer;
      l : cardinal;
    begin
      status:= MagickSetImageFormat(wand, 'png');
      if status = MagickFalse then
        ThrowWandException(wand);
    
      status:= MagickSetImageCompression(wand, NoCompression);
      if status = MagickFalse then
        ThrowWandException(wand);
    
    // Må legges til i magick_image.inc:
    // function MagickWriteImageBlob(wand: PMagickWand; var length: Cardinal): Pointer; cdecl; external WandExport;
    
      b:= MagickWriteImageBlob(wand, l);
      t:= PDF.AddImageFromMemory(b, l, itcJpeg);
      MagickRelinquishMemory(b);
    
      if t = -1 then
        'Handle some PDF Creator Error!'
      else
        PDF.PDFPAGE_ShowImage(t, x-pos, y-pos, width, height, 0);
    end;
    
  • Ta en kopi av et bilde:
  • begin
    //  cropwand:= NewMagickWand;
      cropwand:= CloneMagickWand(wand);
      if status = MagickFalse then
        ThrowWandException(cropwand);
    end;
    
  • Lagre et bilde:
  • begin
      status:= MagickSetImageCompressionQuality(wand, 50);
      if status = MagickFalse then
        ThrowWandException(wand);
    
      status:= MagickWriteImage(wand, PChar('nyttbilde.jpg'));
      if status = MagickFalse then
        ThrowWandException(wand);
    end;