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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Copy TImageEnView to TBitmap with alpha

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
Marsianin Posted - Dec 28 2014 : 02:29:09
Hi there!
I need to load an image, resample it and copy to TBitmap with alpha channel and put this image to TImageList as toolbar glyph.
Images I tried to load were PNG's with alpha channel, everything works fine except transferring to BMP.

What I have:
...
ime :TImageEnView;
bmp :TBitmap;
begin
  ime:=TImageEnView.Create(nil);
  ime.LegacyBitmap:=False;  
  ime.IO.LoadFromFile(fname, True);
  ime.Proc.Resample(16, 16, rfLanczos3, True);
  bmp:=TBitMap.Create;
  bmp.PixelFormat:=pf32bit;
  bmp.Height:=16;
  bmp.Width:=16;

  ime.IEBitmap.CopyToTBitmap(bmp); // BLACK BACKGROUND
  
  bmp.Assign(ime.Bitmap); // BLACK BACKGROUND

  ime.IEBitmap.DrawToCanvasWithAlpha(bmp.Canvas,0,0,255,1); // WHITE BACKGROUND
  ...
1   L A T E S T    R E P L I E S    (Newest First)
w2m Posted - Dec 28 2014 : 15:24:39
I created a example for you. You may download it here:

attach/w2m/20141228153650_ImageList.zip
55.25 KB

The code is as follows:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  ieview, imageenview, Vcl.ImgList, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ExtDlgs,
  Vcl.ComCtrls, Vcl.ToolWin;

type
  TForm1 = class(TForm)
    OpenPictureDialog1: TOpenPictureDialog;
    Panel1: TPanel;
    Open1: TButton;
    AddToImageList1: TButton;
    ImageList1: TImageList;
    ImageEnView1: TImageEnView;
    ListView1: TListView;
    ToolBar1: TToolBar;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Open1Click(Sender: TObject);
    procedure AddToImageList1Click(Sender: TObject);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses hyiedefs, hyieutils, imageenproc;

procedure TForm1.AddToImageList1Click(Sender: TObject);
{ Create a TBitmap, copy the image to the bitmap with alpha, add the image
  to the ImageList, and add an item to the ListView and toolbar to show the image in the imagelist. }
var
  iBitmap: TBitmap;
  iListItem: TListItem;
  iToolButton: TToolButton;
begin
  iBitmap := TBitmap.Create;
  try
    iBitmap.Width := 32;
    iBitmap.Height := 32;
    ImageEnView1.CopyToBitmapWithAlpha(iBitmap, 0, 0);
    ImageList1.Add(iBitmap, iBitmap);
  finally
    iBitmap.Free;
  end;
    iListItem := ListView1.Items.Add;
    iListItem.Caption := ExtractFilename(ImageEnView1.IO.Params.FileName);
    iListItem.ImageIndex := ImageList1.Count - 1;
    iToolButton := TToolButton.Create(ToolBar1);
    iToolButton.Caption := 'Button' + IntToStr(ToolBar1.ButtonCount);
    iToolButton.Parent := ToolBar1;
    iToolButton.ImageIndex := ToolBar1.ButtonCount-1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IERegisterFormats;
  OpenPictureDialog1.Filter := GraphicFilter(TGraphic);
  ImageEnView1.Gestures.Pan.Enabled := True;
  ImageEnView1.Gestures.Zoom.Enabled := True;
  ImageEnView1.SetChessboardStyle(8, bsSolid);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  IEUnregisterFormats;
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
{ Display the selected imagelist item. }
var
  iRGB: TRGB;
begin
  ImageEnView1.Blank;
  ImageList1.GetBitmap(Item.ImageIndex, ImageEnView1.Bitmap);
  iRGB := ImageEnView1.IEBitmap.Pixels[0, ImageEnView1.IEBitmap.Height - 1];
  ImageEnView1.Proc.SetTransparentColors(iRGB, iRGB, 0);
  ImageEnView1.Update;
end;

procedure TForm1.Open1Click(Sender: TObject);
{ Load the image, resample to fit the ImageList if necessary, snd set the transparent color. }
var
  iRGB: TRGB;
  iImageEnProc: TImageEnProc;
begin
  if OpenPictureDialog1.Execute then
  begin
    ImageEnView1.IO.LoadFromFile(OpenPictureDialog1.FileName);
    if (ImageEnView1.IEBitmap.Width <> 32) or
      (ImageEnView1.IEBitmap.Height <> 32) then
    begin
      ImageEnView1.Proc.Resample(32, 32);
      ImageEnView1.IO.Params.BitsPerSample := 8;
      ImageEnView1.IO.Params.SamplesPerPixel := 4;
      iRGB := ImageEnView1.IEBitmap.Pixels[0, ImageEnView1.IEBitmap.Height - 1];
      ImageEnView1.Proc.SetTransparentColors(iRGB, iRGB, 0);
      ImageEnView1.Update;
    end;
  end;
end;

end.


Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development