Delphi Programming
Advertisement

How do I create a rounded rect[]

function CreateRoundRectangle(rectangle: TGPRect;
  radius: integer): TGPGraphicsPath;
var
  path : TGPGraphicsPath;
  l, t, w, h, d : integer;
begin
  path := TGPGraphicsPath.Create;
  l := rectangle.X;
  t := rectangle.y;
  w := rectangle.Width;
  h := rectangle.Height;
  d := radius div 2; // divide by 2

  // the lines beween the arcs are automatically added by the path
  path.AddArc(l, t, d, d, 180, 90); // topleft
  path.AddArc(l + w - d, t, d, d, 270, 90); // topright
  path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
  path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
  path.CloseFigure();
  result := path;
end;

How do I flip text[]

procedure TForm29.PaintBox1Paint(Sender: TObject);
var
  graphics : TGPGraphics;
  FontFamily: TGPFontFamily;
  Font: TGPFont;
  SolidBrush: TGPSolidBrush;
  Matrix : TGPmatrix;
  SolidPen : TGPPen;
  r : TGPrectF;
  GraphicsPath : TGPGraphicsPath;
begin
  graphics   := TGPGraphics.Create(PaintBox1.Canvas.handle);
  FontFamily := TGPFontFamily.Create('Times New Roman');
  Font       := TGPFont.Create(FontFamily, 32, FontStyleRegular, UnitPixel);
  SolidBrush := TGPSolidBrush.Create(MakeColor(255, 0, 0, 255));
  SolidPen := TGPPen.Create(MakeColor(255, 0, 0, 255));

//  graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);

// so you can see the box
  graphics.drawRectangle(SolidPen, MakeRect(0,0,PaintBox1.Width-1, PaintBox1.height-1));

// the normal text offset to the right half of the paintbox
  graphics.TranslateTransform(PaintBox1.width div 2, 0);
  graphics.DrawString('AntiAlias', -1, font, MakePoint(0.0, 0.0), solidBrush);

  // x mirror
  matrix := TGPMatrix.Create(-1,0,0,1,PaintBox1.width div 2,0);
  graphics.settransform(matrix);
  graphics.DrawString('AntiAlias', -1, font, MakePoint(0.0, 0.0), solidBrush);
  matrix.free;

  // y mirror
  matrix := TGPMatrix.Create(1,0,0,-1,PaintBox1.width div 2,PaintBox1.height);
  graphics.settransform(matrix);
  graphics.DrawString('AntiAlias', -1, font, MakePoint(0.0, 0.0), solidBrush);
  matrix.free;

  // x and y mirror
  matrix := TGPMatrix.Create(-1,0,0,1,PaintBox1.width div 2,0);
  graphics.settransform(matrix);
  matrix.free;
  matrix := TGPMatrix.Create(1,0,0,-1,0,PaintBox1.height);
  graphics.multiplytransform(matrix);
  graphics.DrawString('AntiAlias', -1, font, MakePoint(0.0, 0.0), solidBrush);
  matrix.free;

  graphics.Free;
  FontFamily.Free;
  Font.Free;
  SolidBrush.Free;
end; 

How do I Access the pixels in a GDI+ bitmap?[]

This assumes a 32 bit pixel depth. To access the pixels, first lock the bitmap and obtain a TBitmapData. The TBitmapData structure contains information about the bitmap and a pointer to the first scanline. To use the scanline like the TBitmap call the scanline function for the row required. See the example below.

function lockBitmap(Bitmap : TGPBitmap) : TBitmapData;
var
  r : TGPRect;
begin
  r.x := 0;
  r.y := 0;
  r.width := bitmap.getWidth;
  r.height := bitmap.getHeight;

  Bitmap.LockBits(r, ImageLockModeRead or ImageLockModeWrite,
    Bitmap.GetPixelFormat, result);
end;

function Scanline(BitmapData : TBitmapData; Row : integer) : PRGBQuad;
begin
    result := bitmapData.Scan0;
    inc(PByte(result), Row * bitmapData.stride);
end;

procedure doStuff(Bitmap :TGPBitmap);
Var
  Row, Col : integer;
  theRow  : PRGBQuad;
  pixel : TRGBQuad;
  BitmapData : TBitmapData;
begin
  BitmapData := lockBitmap(Bitmap);
  for Row := 0 to bitmapData.Height - 1 do
  begin
    theRow := Scanline(BitmapData, Row);
    for Col := 0 to Bitmap.getWidth - 1 do
    begin
      pixel := theRow^;
      ..do stuff..
      inc(theRow);
    end;
  end;
  Bitmap.UnLockBits(bitmapData);
end;

How Do I create a rounded rectangle in GDI+[]

function CreateRoundRectangle(rectangle: TGPRect;
  radius: integer): TGPGraphicsPath;
var
  path : TGPGraphicsPath;
  l, t, w, h, d : integer;
begin
  path := TGPGraphicsPath.Create;
  l := rectangle.X;
  t := rectangle.y;
  w := rectangle.Width;
  h := rectangle.Height;
  d := radius div 2; // divide by 2

  // the lines beween the arcs are automatically added by the path
  path.AddArc(l, t, d, d, 180, 90); // topleft
  path.AddArc(l + w - d, t, d, d, 270, 90); // topright
  path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
  path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
  path.CloseFigure();
  result := path;
end;

See also[]

Advertisement