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
 Creating a new image with transparent background
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

EricNat

USA
37 Posts

Posted - Jan 28 2014 :  20:36:53  Show Profile  Reply
I am trying to create a new image in TImageEnVect that has a transparent background. I have tried setting PixelFormat to ie24RGB
and setting the EnableAlphaChannel=true; What else am I missing? The background is always white. I am trying to create a transparent image and then place a new bitmap in the center of the transparent image, then save as PNG. Using C++ Builder.

w2m

USA
1990 Posts

Posted - Jan 28 2014 :  21:49:09  Show Profile  Reply
Convert to 32-bit:
ImageEnView.EnableAlphaChannel := True;
ImageEnView.IEBitmap.Allocate(iImageWidth, iImageHeight);
ImageEnView.IEBitmap.Fill(clBlack);
ImageEnView.IO.Params.BitsPerSample := 8;
ImageEnView.IO.Params.SamplesPerPixel := 4;


Set transparent color (black)
ImageEnView.Proc.SetTransparentColors(ImageEnView.IEBitmap.Pixels[0,
              iImageHeight - 1], ImageEnView.IEBitmap.Pixels[0,
              iImageHeight - 1], 0);

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

EricNat

USA
37 Posts

Posted - Jan 29 2014 :  13:54:11  Show Profile  Reply
Thanks for your help. Something is still not right though. This is with a TImageEnVect. When I put in the code you provided, it does create a completely transparent image. The problem now is that I am trying to overlay an image on top of the transparent image and not have that new image be transparent. So I create a new TImageEnView image for the overlay and then I am using AddNewObject to add the IEBitmap to the TImageEnVect component. Then CopyObjectsToBack and RemoveObjects This all works without the transparency -- but the background of the main image is not transparent. I am trying to get the background of the main image to be transparent but nothing in the overlaid image should be transparent.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 29 2014 :  14:08:26  Show Profile  Reply
I think that problem is that when you CopyObjectsToBack it converts the IEBitmap back to 24-bit... That is why it is not transparent anymore.

To fix this, just repeat the same process to convert the IEBitmap back to 32-bit transparent AFTER CopyObjectsToBack is executed.

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

EricNat

USA
37 Posts

Posted - Jan 29 2014 :  14:15:54  Show Profile  Reply
That worked! The only issue with doing it afterwards is that if the background of the overlay image is the same as the transparent color (black in this case) it makes the background of the overlay also transparent. But I can probably work around that by choosing a weird fill color that isn't likely to be used in the overlay.

I suppose another method to do this would be to copy the bitmap of the overlay image directly onto the main one instead of using vector objects and CopyObjectsToBack(). Hmm..

Thanks!
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 29 2014 :  14:29:43  Show Profile  Reply
After writing my previous post I tested what you are doing and I found that if I open a transparent png 32-bit image into the background layer or layer 0, then dropped a 24 bit Bitmap object, the background image remained transparent after CoptObjectToBack... So it is difficult for me to understand what exactly is occurring on your end.

Also another easy way the make a new blank bitmap transparent is to fill the new bitmap with white or some other color then fill the alphachannel:
ImageEnVect1.Proc.Fill(clWhite);
ImageEnVect1.AlphaChannel.Fill(0);
or
ImageEnVect1.IEBitmap.AlphaChannel.Fill(0);

In this case, all the pixels in the bitmap are clWhite so filling the AlphaChannel with 0 makes all the white pixels transparent.

William Miller
Go to Top of Page

EricNat

USA
37 Posts

Posted - Feb 02 2014 :  23:09:03  Show Profile  Reply
Hmm. I tried the other method you mentioned and I am getting the same thing - a completely transparent image. I am losing the top image that is being overlayed. What I am trying to do in this case (different from the other one) is make an image larger than the overylay image where the area outside of the overlay image is transparent. But I want the background on the overlay image to remain as is. Here is the code I am currently using:


If you have any ideas why this isn't working, I would really appreciate any help.

Thanks!

==

TImageEnVect *Image=new TImageEnVect((TComponent *)NULL);
Image->EnableAlphaChannel=true;
Image->IEBitmap->Allocate(SetToWidth,SetToHeight);
Image->Proc->Fill(clWhite);
// Convert to 32-bit
Image->IO->Params->BitsPerSample=8;
Image->IO->Params->SamplesPerPixel=4;

// Set the background color to transparent
Image->AlphaChannel->Fill(0);

// Load the actual image
TImageEnView *OverlayImage=new TImageEnView((TComponent *)NULL);
OverlayImage->IO->LoadFromFile(PicFileName);
int picwidth=OverlayImage->IEBitmap->Width;
int picheight=OverlayImage->IEBitmap->Height;

// Add the overlay image to the base image centered in the middle
int picx, picy;
if (picwidth<SetToWidth) picx=(SetToWidth-picwidth)/2;
else picx=0;
if (picheight<SetToHeight) picy=(SetToHeight-picheight)/2;
else picy=0;
Image->AddNewObject(iekBITMAP,Rect(picx,picy,picwidth+picx,picheight+picy), clNone);
Image->ObjBitmap[-2]=OverlayImage->IEBitmap;
delete OverlayImage;
Image->CopyObjectsToBack(true);
Image->RemoveAllObjects();

// Save the new image to the file name
Image->IO->Params->PNG_Filter=ioPNG_FILTER_PAETH;
Image->IO->Params->PNG_Compression=9;
DeleteFile(NewPicFileName);
Image->IO->SaveToFilePNG(NewPicFileName);

// Cleanup
delete Image;
Go to Top of Page

w2m

USA
1990 Posts

Posted - Feb 03 2014 :  12:40:58  Show Profile  Reply
Is your objective to add an image on top of a new blank transparent image that is in the background?

If that is the case you would be much better off to just use an TImageENView, and fill the background with alpha = 0, then add a layer and add your second transparent image to the layer. Then place the layer at the XY position (centered) you wish then merge the layers and save as a png.

Why are you using a vectorial bitmap object for this?

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

EricNat

USA
37 Posts

Posted - Feb 04 2014 :  21:58:24  Show Profile  Reply
I managed to get that to work - using TImageEnView and using the layers to place the bitmap on top of the transparent image. Thanks very much for the help!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: