{
  Autor: Michael Springwald

  Erstellt: Samstag, 06.Oktober.2007
  Updates:

}

unit uOpExt;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Graphics,lazbridge, opbitmapformats,RegisterLazOp,opbitmap,LCLIntf,LCLType;

  // Verößert eine TBITMap und umgeht dabei den Bug
  procedure BMPSetSize(const aBuffer:TBitMap; const aW,aH:Integer);

  // Speichert eine TBitmap in ein Bliebiges Format ab
  // Untershieden wird nach der Dateiendung
  procedure SaveBMPToFile(const aFileName:String; aBuffer:TBitMap);

  // Lädt ein Grafik Format in aBuffer.
  // Unterschieden wird nach der Dateiendung
  procedure LoadFileToBMP(const aFileName:String; Var aBuffer:TBitMap);

  // Erstllt auf der Angeben Grafik ein Tranzparentes Viereck
  procedure DrawTranzParentRect(aBuffer:TBItMap; const aRect:Trect; const aBrushColor:TColor; const aAlpha:Integer=100);

implementation

procedure BMPSetSize(const aBuffer:TBitMap; const aW, aH: Integer);
var
  oldY,oldH:Integer;
  oldBitMap:TBitMap;
begin
  // Kopie von Aktuellen Bild wird erstellt
  oldBitMap:=TBitMap.Create;
  oldBitMap.Width:=aBuffer.Width;
  oldBitMap.Height:=aBuffer.Height;
  oldBitMap.Canvas.Brush.color:=clWhite;
  oldBitMap.Canvas.FillRect(0,0,oldBitMap.Width,oldBitMap.Height);
  BitBlt(oldBitMap.Canvas.Handle,0,0,oldBitMap.Width,oldBitMap.Height,aBuffer.Canvas.Handle,0,0,SRCCOPY);

  // Die Größe wird gesetzt
  aBuffer.Width:=aW;
  aBuffer.Height:=aH;
  aBuffer.Canvas.Brush.color:=clWhite;

  // Das Bild wird Installisert(Der BUG in der LCL)
  // Nach jeder Größen Änderung muss
  // erst einmal ein FillRect gezeichnet werden
  aBuffer.Canvas.FillRect(0,0,aW,aH);

  // Das alte Bild wird zurück kopiert
  BitBlt(aBuffer.Canvas.Handle,0,0,oldBitMap.Width,oldBitMap.Height,oldBitMap.Canvas.Handle,0,0,SRCCOPY);
  oldBitMap.Free;
end;

procedure SaveBMPToFile(const aFileName: String; aBuffer: TBitMap);
var
  op:TOPPicture;
begin
  op:=TOpPicture.Create;
  AssignBitmapToOpBitmap(aBuffer,op.Bitmap);
  op.SaveToFile(aFilename);
  op.free;
end;

procedure LoadFileToBMP(const aFileName: String; var aBuffer: TBitMap);
var
  op:TOPPicture;
begin
  op:=TOpPicture.Create;
  op.LoadFromFile(aFileName);
  AssignOpBitmapToBitmap(op.Bitmap,aBuffer);
//  AssignBitmapToOpBitmap(,);
  op.free;
end;

procedure DrawTranzParentRect(aBuffer: TBItMap; const aRect: Trect;
  const aBrushColor: TColor; const aAlpha:Integer=100);
var
  Hintergrund:TOPPicture;
  ah:TCanvasOPBitmap;
begin
  Hintergrund:=TOPPicture.Create;
  AssignBitmapToOpBitmap(abuffer,Hintergrund.Bitmap);

  ah:=TCanvasOPBitmap.Create;
  ah.AlphaBlend:=True;
  ah.Width:=aRect.Right; ah.Height:=arect.Bottom;
  ah.Canvas.Brush.color:=aBrushColor;
  ah.Canvas.FillRect(Rect(0,0,ah.Width,ah.Height));
  ah.SetAlpha(aAlpha);
  
  Hintergrund.Bitmap.AlphaBlend := true;
  Hintergrund.Bitmap.Canvas.Draw(aRect.left,aRect.Top, ah);
  Hintergrund.Bitmap.AlphaBlend := false;

  AssignOpBitmapToBitmap(Hintergrund.Bitmap,aBuffer);
  Hintergrund.Free; ah.Free;
end;

end.

