ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Drawing a transparent grid of lines
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

bmesser

United Kingdom
224 Posts

Posted - Dec 16 2013 :  06:03:25  Show Profile  Reply
Hi

Sorry for requesting help on what's probably quite an easy problem to solve if you know how.

I want to create a transparent PNG image that displays a grid of lines (lat & long) in it.

I got this code on how to creates a transparent base layer from Nigel in answer to another problem it solved, but try as I might, I can't seem to draw a visible line on it. I tried adding another layer, but I reckon you should just be able to just draw on the base bitmap. I did think of using a TImageEnVect component instead, but thought that was a bit overboard as I may have dozens of horizontal and vertical lines to draw as objects.

Bruce.


ImageEnView1.LegacyBitmap:=False;
  ImageEnView1.IEBitmap.Width:=256;
  ImageEnView1.IEBitmap.Height:=256;
  ImageEnView1.EnableAlphaChannel:=True;
  ImageEnView1.AlphaChannel.Fill(0);

//ImageEnView1.LayersAdd;

  ImageEnView1.IEBitmap.Canvas.Pen.Width:=1;
  ImageEnView1.IEBitmap.Canvas.Pen.Color:=clBlack;
  ImageEnView1.IEBitmap.Canvas.MoveTo(10,10);
  ImageEnView1.IEBitmap.Canvas.LineTo(30,30);


w2m

USA
1990 Posts

Posted - Dec 16 2013 :  11:05:26  Show Profile  Reply
To draw the grid transparently you must also draw on the alphachannel. This uses OnBackbuffer to draw the grid. Here is one way to do it:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Draw A Grid'
ClientHeight = 299
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object ImageEnView1: TImageEnView
Left = 0
Top = 0
Width = 635
Height = 299
Background = clBtnFace
ParentCtl3D = False
OnResize = ImageEnView1Resize
BackgroundStyle = iebsChessboard
OnDrawBackBuffer = ImageEnView1DrawBackBuffer
EnableInteractionHints = True
Align = alClient
TabOrder = 0
end
end

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, imageenview, ieview, imageenproc, hyieutils, hyiedefs;

type
  TForm1 = class(TForm)
    ImageEnView1: TImageEnView;
    procedure FormShow(Sender: TObject);
    procedure ImageEnView1Resize(Sender: TObject);
    procedure ImageEnView1DrawBackBuffer(Sender: TObject);
  private
  { Private declarations }
    procedure CreateHorizontal(i, GridWidth, CanvasWidth: Integer);
    procedure CreateVertical(i, GridWidth, CanvasHeight: Integer);
    procedure DrawGrid;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreateHorizontal(i, GridWidth, CanvasWidth: Integer);
{ Draw horzontal rectangles. }
begin
  ImageEnView1.IEBitmap.Canvas.Rectangle(0, 0, CanvasWidth,
    i * GridWidth + GridWidth);
  ImageEnView1.IEBitmap.AlphaChannel.Canvas.Rectangle(0, 0, CanvasWidth,
    i * GridWidth + GridWidth);
end;

procedure TForm1.CreateVertical(i, GridWidth, CanvasHeight: Integer);
{ Draw vertical rectangles. }
begin
  ImageEnView1.IEBitmap.Canvas.Rectangle(0, 0, i * GridWidth,
    CanvasHeight + GridWidth);
  ImageEnView1.IEBitmap.AlphaChannel.Canvas.Rectangle(0, 0, i * GridWidth,
    CanvasHeight + GridWidth);
end;

procedure TForm1.DrawGrid;
{ Draw the grid. }
const
  iGridWidth: Integer = 50;
var
  iMaxWidth: Integer;
  i: Integer;
begin
  { Fill the bitmap with white }
  ImageEnView1.IEBitmap.Fill(clWhite);
  { Draw a rect around the image }
  ImageEnView1.IEBitmap.Canvas.Pen.Style := psSolid;
  ImageEnView1.IEBitmap.Canvas.Pen.Width := 1;
  ImageEnView1.IEBitmap.Canvas.Pen.Color := clBlack;
  ImageEnView1.IEBitmap.Canvas.Rectangle(1, 1, ImageEnView1.IEBitmap.Width-1,
    ImageEnView1.IEBitmap.Height-1);
  ImageEnView1.IEBitmap.AlphaChannel.Canvas.Rectangle(1, 1,  ImageEnView1.IEBitmap.Width-1,
    ImageEnView1.IEBitmap.Height-1);
  ImageEnView1.IEBitmap.Canvas.Brush.Style := bsClear;
  ImageEnView1.IEBitmap.Canvas.Pen.Style := psSolid;
  ImageEnView1.IEBitmap.Canvas.Pen.Width := 1;
  ImageEnView1.IEBitmap.Canvas.Pen.Color := clBlack;
  ImageEnView1.IEBitmap.Canvas.Brush.Style := bsClear;
  { Draw inside rectangles with dots }
  ImageEnView1.IEBitmap.Canvas.Pen.Style := psDot;
  ImageEnView1.IEBitmap.Canvas.Pen.Width := 1;
  ImageEnView1.IEBitmap.Canvas.Pen.Color := clBlack;
  iMaxWidth := ImageEnView1.IEBitmap.Width;
  for i := 0 to iMaxWidth do
  begin
    { Draw the horzontal rectangles }
    CreateHorizontal(i, iGridWidth, ImageEnView1.IEBitmap.Width);
    { Draw the vertical rectangles }
    CreateVertical(i, iGridWidth, ImageEnView1.IEBitmap.Height);
  end;
  ImageEnView1.EnableAlphaChannel := True;
  { Make the white transparent }
  ImageEnView1.Proc.SetTransparentColors(CreateRGB(255, 255, 255), CreateRGB(255, 255, 255), 0);
  ImageEnView1.Update;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  { Create Transparent bitmap }
  ImageEnView1.AlphaChannel.Fill(0);
  { Force the form to redraw itself }
  Form1.Width := Form1.Width + 1;
  ImageEnView1.Update;
end;

procedure TForm1.ImageEnView1DrawBackBuffer(Sender: TObject);
{ Draw the grid in the backbuffer canvas. }
begin
  DrawGrid;
end;

procedure TForm1.ImageEnView1Resize(Sender: TObject);
{ Resize the bitmap to fit the component and redraw the grid. }
begin
  ImageEnView1.Proc.ImageResize(ImageEnView1.Width, ImageEnView1.Height, iehCenter, ievCenter, 0);
  DrawGrid;
end;

end.

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html
Go to Top of Page

bmesser

United Kingdom
224 Posts

Posted - Dec 16 2013 :  11:36:07  Show Profile  Reply
Thanks Bill
What would I do without you!
Bruce.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: