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
 embedding bitmap into metafile with transparency
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

davenovo

USA
26 Posts

Posted - Aug 25 2011 :  16:40:54  Show Profile  Reply
Hello.

I have a TIEBitmap that has an alpha channel where some pixels are partially transparent. I need to render that bitmap to a metafile and preserve the transparency. How can I do this?

I tried RenderToCanvasWithAlpha but that does not work if I pass in a metafile canvas. It does not work because RenderToCanvasWithAlpha tries to get the image from the metafileCanvas, but you cannot retreive an image from a metafile canvas in that way.

Any ideas?

fab

1310 Posts

Posted - Aug 25 2011 :  23:27:23  Show Profile  Reply
Hello,
RenderToCanvasWithAlpha merges (alpha blend) source pixels with destination pixels, using source alpha channel. For this reason it needs to read pixels from destination.
Anyway it doesn't preserve the original alpha channel.

Actually I don't know a way to produce a metafile with embedded a bitmap+alpha channel: maybe it could be possible embedding a PNG (with alpha) into the metafile using GDI+.
Go to Top of Page

fab

1310 Posts

Posted - Aug 25 2011 :  23:51:19  Show Profile  Reply
I know another way, that has following limitations:

1) works only with alpha=0 or alpha=255. No semi-transparent pixels allowed.
2) it is very slow (for each pixel draws a rectangle on the canvas)
3) of course, when the metafile is scaled produces "big pixels"

Here is an example:


var
  x, y:integer;
  MetaFile:TMetaFile;
  MetaCanvas:TCanvas;
  bmp:TIEBitmap;
begin
  // load image with alpha in ImageEnView1
  ImageEnView1.IO.LoadFromFile('myimage.xxx');

  bmp := ImageEnView1.IEBitmap;
  MetaFile := TMetaFile.Create();
  MetaFile.Enhanced := true;
  MetaFile.Width := bmp.Width;
  MetaFile.Height := bmp.Height;
  MetaCanvas := TMetaFileCanvas.Create(MetaFile, 0);
  for y:=0 to bmp.Height-1 do
  begin
    for x:=0 to bmp.Width-1 do
    begin
      if bmp.AlphaChannel.Pixels_ie8[x, y] > 0 then
      begin
        MetaCanvas.Brush.Color := TRGB2TColor(bmp.Pixels[x, y]);
        MetaCanvas.FillRect(Rect(x, y, x+1, y+1));
      end;
    end;
  end;
  MetaCanvas.Free();
  MetaFile.SaveToFile('testmeta.emf');
  MetaFile.Free();
end;


TRGB2TColor is defined in "imageenproc" unit, while TIEBitmap in "hyieutils".
Go to Top of Page

davenovo

USA
26 Posts

Posted - Aug 26 2011 :  09:39:32  Show Profile  Reply
Hi Fabrizio,

Thanks. We will have to find a better way. I did not realize that the standard Metafile did not really have a way of supporting transparent bitmaps.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: