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
 problem loading PNG file
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

davenovo

USA
26 Posts

Posted - Jan 06 2013 :  19:24:07  Show Profile  Reply
Hello,

I am having a problem loading a PNG file. I have put the code below in just a button click to show the problem

procedure TForm1.Button1Click(Sender: TObject);
const
FNAME='c:\temp\spiderman.png';
var
bmp: TBitmap;
iebmp: TIEBitmap;
tmpImageIO: TImageEnIO;
tmpPicture: TPicture;
begin
iebmp:=TIEBitmap.Create;
tmpImageIO:=TImageEnIO.Create(nil);
tmpImageIO.AsyncMode:=False;
tmpImageIO.StreamHeaders:=False;
tmpImageIO.AttachedIEBitmap:=iebmp;
// tmpImageIO.Params.PNG_Background:=TColor2TRGB(clWhite);
// tmpImageIO.Params.BMP_HandleTransparency:=True;
tmpImageIO.LoadFromFile(FNAME);
bmp:=TBitmap.Create;
ieBmp.CopyToTBitmap(bmp);
bmp.SaveToFile('c:\temp\made_with_imageEn.bmp');
bmp.Free;
tmpImageIO.Free;
ieBmp.Free;
end;

I have attached the photo. When it saves, it has a black background. If I look at it in any image editor, the background is white. Any ideas?

Even if I uncomment the lines above, it still does not work. Any tips?




1008.84 KB

w2m

USA
1990 Posts

Posted - Jan 07 2013 :  06:42:27  Show Profile  Reply
PNG images usually do not requre any special processing to load correctly, but try tmpImageIO.Params.PNG_Background:=TColor2TRGB(clBlack) and/or ImageEnView1.Proc.SetTransparentColors(TColor2TRGB(clBlack), TColor2TRGB(clBlack),0) after loading the image. The problem may also be that you loose the alphachannel when calling ieBmp.CopyToTBitmap(bmp); ieBmp.CopyToTBitmap(bmp) does not support the alphachannel.

You also create tmpImageIO but then your code does not use the image it loads at all?

What are you trying to accomplish?
After loading the image into tmpImageIO you could just try tmpImageIO.SaveToFileBMP

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

Patrick Quinn

United Kingdom
81 Posts

Posted - Jan 07 2013 :  07:49:07  Show Profile  Reply
There is a post about if Delphi's PNG library is being used instead of ImageEn's this causes problems. Don't know if that helps.
http://www.imageen.com/ieforum/topic.asp?TOPIC_ID=803
and see last comment at
http://www.imageen.com/ieforum/topic.asp?TOPIC_ID=357

regards

Patrick
Go to Top of Page

davenovo

USA
26 Posts

Posted - Jan 07 2013 :  08:11:08  Show Profile  Reply
The problem is that I use ImageEn to read in various image formats, then save the image to a BMP as a common format that I then display it to the user. For many different PNGs, it is coming up with a black background.

I cannot manually set the background colors because I dont know what they are in general. I feel the library should be able to load any PNG and then be able to copy it to a bitmap.

How can I specify that if there is an alpha channel in the original PNG, to have the background white when copying to the bitmap?
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 07 2013 :  08:46:32  Show Profile  Reply
I already told you how... After loading the image into tmpImageIO you should use tmpImageIO.SaveToFileBMP... do not use TBitmap at all. But for TBitmap in ImageEn you must set tmpImageIO.Params.BMP_HandleTransparency:=True;

Here is an example that saves the image as a bitmap with the alphachannel:

procedure TForm2.FormCreate(Sender: TObject);
begin
  ImageEnView1.IO.Params.BMP_HandleTransparency := True;// Important!!
end;

procedure TForm2.SaveAs1Click(Sender: TObject);
var
  iFilename: string;
begin
  // This is a dialog component available with my Ebook
  if IEWin7FileSaveDialog1.Execute then
  begin
    iFileName := IEWin7FileSaveDialog1.FileName;
    ImageEnView1.IO.SaveToFile(iFileName);
    UpdateStatusbar;
  end;
end;

After saving the file if you reopen the file in imageen the background will be transparent as shown in an imageen application below:

172.32 KB

If you want the transparent color to be white with a solid background just call:
ImageEnView1.Background := clWhite;
ImageEnView1.BackgroundStyle := iebsSolid;


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

davenovo

USA
26 Posts

Posted - Jan 07 2013 :  09:25:13  Show Profile  Reply
Hi William,

Although this may work, loading and saving and then re-loading files to disk is not an optimal way of moving information around within a program. Is there not a way to do this without saving and reloading the image to / from disk.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 07 2013 :  09:48:44  Show Profile  Reply
This example does not save and reload the file... when you open a bitmap file it is shown with transparancy. The background color is controled by the background color with images with alphachannel. The saving mearly saves the image as a bitmap which is what you were trying to accomplish. What are you referring to?

William Miller
Go to Top of Page

davenovo

USA
26 Posts

Posted - Jan 07 2013 :  10:32:35  Show Profile  Reply
Hi William,

I must be missing something. First of all I am not using an ImageEnView, nor do I want to. I simply want to load a file as a PNG and save it as a BMP, where if there was transparency, the background will be white, or the resulting BMP will be marked with a transparentColor.

Note in the sample code I posted I did not have an ImageEnView, I just loaded the file and tried to save it.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 07 2013 :  11:48:20  Show Profile  Reply
Sorry.. I missed or overlooked that part.

As best as I can determine viewing a 32-bit bitmap with alphachannel is dependent on the viewing component or application. For example when you save a 32-bit bitmap with alphachannel with ImageEn, then view the file in Windows Explorer or many other apps the transparency is not shown even though the alpha information is present in the file.

So... it looks like it depends on what you are going to use to view the file.

I got this from StackOverflow:

First of all, you should know that transparent BMP's are very uncommon. Hence, many (most) bitmap viewers, encoders, and decoders do not support transparent bitmaps. However, there is some hope. First of all, many bitmaps are 32-bit, even though the pixels most often are stored in the format $00BBGGRR. The first byte of each "pixel" is hence unused, and one could of course use this as the opacity value: $AABBGGRR. But this is not only my personal ideas. Most bitmaps use the version 3 bitmap header, but version 4 (and version 5) actually supports transparency data. You simply specify the red, green, blue, and alpha masks (e.g. $000000FF, $0000FF00, $00FF0000, and $FF000000, respectively) and then you can store red, green, blue, and alpha intensities per pixel.

Still, as I said, most bitmap viewers, encoders, and decoders doesn't support transparent bitmaps. I think that the VCL encoders, decoders, and viewer (TImage) don't.

I would consider using PNG instead of BMP. The PNG bitmap image format supports transparency in a lot of different ways.

Andreas Rejbrand



In my work with ImageEn and 32-bit bitmap with alphachannel I have found that the simplest thing to do is use PNG as referenced by Andreas. Transparency with png is nearly automatic.

Sorry I could not be more helpful. You next bet is to email Fabrizio and get his expert opinion on this problem.

I am wondering why is it absolutely necesary to use bitmaps in your case?

What component or app are you going to use to view the bitmap?

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

davenovo

USA
26 Posts

Posted - Jan 07 2013 :  23:17:52  Show Profile  Reply
This is supporting a bit of legacy code. Right now the images are being rendered by painting them onto the forms canvas.

They are being stored in a TPicture.Graphic.Bitmap property.


Right now, if its a PNG, I use TPicture.LoadFromFile to load the PNG. It seems that currently the DevExpress PNG parser is being used, because it has registered the PNG file format using Delphi's RegisterFileFormat call. When I load it into TPicture like that, and do a stretchdraw to draw it on the form, it is fine.

For all other file formats, I use ImageEn to load the image, then put it in a TBitmap and stick the TBitmap into the TPicture.
Graphic.

The problem became when I started using ImageEn to stretch the image instead of stretchdraw. Everything worked much better, because stretchdraw sucks. However, the PNGs screwed up by having a black background. I determined this happened upon initial loading of the PNG via the code I showed above.

What is the best way to get in touch with fabricio these days. just email support?
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 08 2013 :  05:57:17  Show Profile  Reply
support@hicomponents.com

William Miller
Go to Top of Page

fab

1310 Posts

Posted - Jan 09 2013 :  01:53:08  Show Profile  Reply
This image hasn't background, but an alpha mask.
In order to make the background white you have to remove alpha mask and replace with white:


  iebmp:=TIEBitmap.Create;
  tmpImageIO:=TImageEnIO.Create(nil);
  tmpImageIO.AttachedIEBitmap:=iebmp;
  tmpImageIO.LoadFromFile(FNAME);
  tmpImageIO.IEBitmap.RemoveAlphaChannel(true, clWhite);    <<<< here
  bmp:=TBitmap.Create;
  ieBmp.CopyToTBitmap(bmp);
  bmp.SaveToFile('c:\tempworks\made_with_imageEn.bmp');
  bmp.Free;
  tmpImageIO.Free;
  ieBmp.Free;



In order to set the PNG background color info, execute instead:

tmpImageIO.IEBitmap.RemoveAlphaChannel(true, TRGB2TColor(tmpImageIO.Params.PNG_Background));

Anyway PNG_Background read from this PNG is black, so you will get a black background.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: