Extracting PNG images from Delphi 2009 imagelist - delphi

The TImageList of Delphi 2009 has support for PNG images by adding them in the imagelist editor. Is there any way to extract a TPngImage from a TImagelist and preserving the alpha channel?
What I want to do is actually to extract the images from one TImageList, make a disabled version of them and then add them to another TImageList. During this operation I would of course like to preserve the alpha channel of the PNG images.

I did something like this with Delphi 2006.
TImageList contains a protected method GetImages. It can be accessed using the "protected bug"
type
TGetImageImageList = class (TImageList) // Please use a better name!
end;
You can cast the imagelist to the TGetImageImageList to get to the GetImages.
begin
TGetImageList(ImageList).GetImages(index, bitmap, mask);
end;
Bitmap contains the bitmap and mask is a black and white bitmap that determines the transparant sections.
You now can change the bitmap and store it using:
function Add(Image, Mask: TBitmap): Integer;
I hope this gives you enough pointers to explore further.

Related

TJpegImage: Internal bitmap not updated after applying JPEG compression

I want to convert a BMP to JPG, compress that JPG and the put back the compressed JPG into the original BMP.
However, it won't assign the compressed image to the BMP. I always get the orignal image into the BMP.
The code is below. To see the compression I set CompressionQuality = 1. This will literary ruin the image.
function CompressBmp2RAM(InOutBMP: TBitmap): Integer;
VAR
Stream: TMemoryStream;
Jpg: TJPEGImage;
begin
Jpg:= TJPEGImage.Create;
Stream:= TMemoryStream.Create;
TRY
Jpg.Assign(InOutBMP);
Jpg.CompressionQuality:= 1; // highest compression, lowest quality
Jpg.Compress;
Jpg.SaveToStream(Stream);
//Stream.SaveToFile('c:\out.jpg'); <---- this gives the correct (heavily compressed) image
Result:= tmpQStream.Size;
InOutBMP.Assign(Jpg);
//InOutBMP.SaveToFile('c:\out.bmp'); <---- this gives the uncompressed image
FINALLY
FreeAndNil(Stream);
FreeAndNil(Jpg);
END;
end;
I have found an work around, but I still want to know why InOutBMP.Assign(Jpg) in the code above won't work.
...
Stream.Position:= 0;
Jpg.LoadFromStream(Stream);
InOutBMP.Assign(Jpg);
...
To me it seems to be a bug. The JPG is not aware that the data was recompressed, so the internal bitmap is never updated. There should be some kind of internal "DirtyData" (or "HasChanged") flag.
So, what is the official solution for this?
Having the JPG to reload ITS OWN DATA from an external data source (stream) seems rather a hack/temporary bug fix.
PS: Jpg.DIBNeeded won't help.
I just checked the code of TJpegImage, and the hypothesis I posted in the comments seems correct.
TJpegImage keeps an internal TBitmap for the representation. When you call DIBNeeded, this bitmap is created based on the Jpeg image data.
GetBitmap (the private function that does the legwork for DIBNeeded) will first check if the bitmap is already assigned, and won't repeat the process if it is. So just calling DIBNeeded will not work in your case, since you're basically guaranteed to have this cached bitmap already.
The FreeBitmap method will free the internal bitmap, after which calling DIBNeeded will create a new one again. So I think the sequence you need is:
Jpg.Compress; // Make sure the Jpeg compressed image data is up to date
Jpg.FreeBitmap; // Clear the internal cached bitmap
Jpg.DIBNeeded; // Optional, get a new bitmap. Will happen when you assign to TBitmap.
I also mentioned JpegNeeded before, but that will do a similar thing as DIBNeeded: check if there is data, if not, call Compress. So you need to call Compress, like you did, to force this compression.
PS: TBitmap (and file formats similar to bmp), don't really know this kind of compression, so by assigning it back to the bitmap, you will have reduced image quality, but not image size. Some bitmap formats, including PNG, do compress by using (amongst others) run length encoding (RLE), which means something like spending just four bytes for saying "And now, 54 times a pixel of this color!". That kind of compression won't work really well on images with lots of jpeg artifacts (the grainy/blurry side effect of the compression), so a PNG version of the compressed Jpg might be larger than a PNG version of the original, even though the quality of the original is better as well. This is especially true for images with large areas of the same color, like screenshots and certain artwork.
The internal TJPEGIMage.GetBitmap function only creates an internal bitmap based on the current jpeg image if no internal bitmap has been previously created.
Assign Image A. Do not use Canvas.
Assign Image B. Use Canvas and see Image B.
Assign Image C. Use Canvas and see Image B.
Assign Image D. Use Canvas and see Image B.
etc.
This is definitely a bug in TJPEGImage. I'm using XE7, so maybe this has been fixed by now.
My workaround to always get the correct JPEGImage.Canvas bitmap is to clear any existing internal bitmap before the assignment.
TJPEGImage = class(Vcl.Imaging.jpeg.TJPEGImage)
public
procedure Assign(Source: TPersistent); override;
end;
procedure TJPEGImage.Assign(Source: TPersistent);
begin
FreeBitmap;
inherited;
end;
The code above is helpful when using one TJPEGImage to handle a lot of different images. For only a few images, creating a new TJPEGImage for each image works.

Delphi: GR32 draw a PNG with alpha onto a TBitmap32 without clearing its content

After going through several other related questions I couldn't come up with a working code for this, so please spare the "duplicate question" tags.
Given a PNG image with either per-pixel alpha channel or single-color transparency, I need code to draw it onto a TBitmap32 which already contains an image (some drawing goes on before the PNG part). So let's say my TBitmap32 is 200x200, I do some drawing on it, then I want to ~insert a smaller transparent PNG image on top of its current content, transparently according to the PNG's alpha channel data or single-color alpha.
Uses pngimage, GR32;
procedure TForm1.Button1Click(Sender: TObject);
Var b: TBitmap;
b32: TBitmap32;
p: TPngImage;
begin
b := TBitmap.Create;
b32 := TBitmap32.Create;
p := TPngImage.Create;
// 50x50 PNG
p.LoadFromFile('z:\test2.png');
b.Width := 200;
b.Height := 200;
b32.Width := 200;
b32.Height := 200;
// some drawing happens on the b32~
// insert code here to draw the png onto the b32, on top of
// what's already drawn, and at specific coordinates i.e 10,10
/////////////////////////////
b32.DrawTo(b.Canvas.Handle,0,0);
Canvas.Draw(0,0,b);
p.Free;
b32.Free;
b.Free;
end;
Original PNG:
Results so far:
There are two ways of working with transparent PNG files:
Load them into intermediary TBitmap32 bitmaps and then manipulate these TBitmap32 bitmaps.
Use TPngImage.Draw (implemented in Vcl.Imaging.pngimage with Delphi XE2 and later) directly on target Canvas, as you have pointed out.
The second way is preferable when it comes to transparency, because the code that you may find to load a PNG into TBitmap32 may work incorrectly. Here are the two examples of the incorrect code that is used most frequently:
(1) “LoadPNGintoBitmap32” from http://graphics32.org/wiki/FAQ/ImageFormatRelated
- it applies the transparency twice, so the images with alpha values other than 0 or 255 will look differently than in other software (most noticeable on translucent images with glass effects). This code will first apply alpha to RGB and then sets alpha, so when you pain, alpha will be applied again. You can find more information on this issue here: Delphi, GR32 + PngObject: converting to Bitmap32 doesn't work as expected
. Besides that, it doesn't convert correctly transparency from paletted images into the alpha layer of TBitmap32, for example, all white pixels become transparent.
(2) “LoadBitmap32FromPNG” from gr32ex library: https://code.google.com/archive/p/gr32ex/
- a slightly different implementation of the same algorithm as (1), and has the same issues as (1).
If you still prefer using TBitmap32, make the following sequence of steps:
Make sure your code correctly converts PNG to TBitmap32.
Do not use TBitmap32 with transparent images to draw directly on HDC, Canvas or TBitmap. Use dmBlend and DrawTo or BlockTransfer() to draw on another TBitmap32. For example, to draw transparently on a TBitmap, create an intermediary cache TBitmap32:
Copy the image from TBitmap to the cache TBitmap32;
Apply your transparent image to the cache TBitmap32 using DrawTo or BlockTransfer(), avoid using Canvas or HDC to mix two images because they lose alpha layer information;
Copy the image back from the cache TBitmap32 to your TBitmap.

brcc32 invalid bitmap format

i created a delphi component and i want to add an icon to it, i know the procedure to follow, but something does not work for me, so here is what I did:
I Created a bitmap file.
I Created an rc file (MyComponent.rc) using notepad and added this to it: TMyComponent BITMAP "MyComponent.bmp" , as my component name is:TMyComponent.
I tried to get the res file using delphi ressource compiler : brcc32 Mycomponent.rc, but I'm getting error 1 33:invalid bitmap format.
I tried to use the other alternative which is image editor, but there is no option to make a res file, may be I'm using the inappropriate software.
Why do I get this error? or could you just give me a link to get the right image editor? Thanks for your help.
Make sure your .bmp file is 8-bit (256 colors) and is 24x24 pixels in size.
Also, the resource name needs to be the component class type in all caps:
TMYCOMPONENT BITMAP "MyComponent.bmp"
Also, the IDE supports 16x16, 24x24, and 32x32 component icons, so you should include 16x16 and 32x32 bitmaps in your resource as well (otherwise the IDE will resize the 24x24 bitmap when needed, which might not look good when shrunk/stretched):
TMYCOMPONENT BITMAP "MyComponent24x24.bmp"
TMYCOMPONENT16 BITMAP "MyComponent16x16.bmp"
TMYCOMPONENT32 BITMAP "MyComponent32x32.bmp"`

Delphi, GR32 + PngObject: converting to Bitmap32 doesn't work as expected

I'm using GR32 for drawing multiple semi-transparent PNG images.
So far I've been using the following method:
png:= TPNGObject.Create;
png.LoadFromFile(...);
PaintBox321.Buffer.Canvas.Draw(120, 20, png);
however I wanted to switch to the method proposed on GR32 website (http://graphics32.org/wiki/FAQ/ImageFormatRelated) :
tmp:= TBitmap32.Create;
LoadPNGintoBitmap32(tmp, ..., foo);
tmp.DrawMode:= dmBlend;
PaintBox321.Buffer.Draw(Rect(20, 20, 20+ tmp.Width, 20+tmp.Height),
tmp.ClipRect, tmp);
While the first method works perfectly fine, the second - which should give the same result - causes very strange problem with alpha channel, see the image (which also shows comparison to the same image "arranged" in Paint.NET - both background and icon were opened on the layers of the editor). The image depicts that the Bitmap32 is loaded or drawn inproperly. Any tips?
-- added 22 Nov
I've found out that it is not about drawing, it's about loading PNG to BMP32. Saving back from BMP32 to PNG generates the incorrect, "whitened" (the one on the left) PNG image.
The reason seems to be that the transparency is applied two times to the image when loaded with LoadPNGintoBitmap32, giving it a more transparent and greyish look (more on this later).
First the transparency:
This is code from the original LoadPNGintoBitmap32, the critical parts are marked with comments:
PNGObject := TPngObject.Create;
PNGObject.LoadFromStream(srcStream);
destBitmap.Assign(PNGObject); // <--- paint to destBitmap's canvas with transparency (!)
destBitmap.ResetAlpha;
case PNGObject.TransparencyMode of // <--- the following code sets the transparency again for the TBitmap32
{ ... }
The destBitmap.Assign internally does the same as you in your previous approach: it let's the PNG image paint itself to its canvas. This operation respects the alpha channel of the PNG. But this is not necessary, since the alpha channel is assigned to TBitmap32's pixels in a second step!
Now change the code as follows, critical parts are again marked with comments:
PNGObject := TPngObject.Create;
PNGObject.LoadFromStream(srcStream);
PNGObject.RemoveTransparency; // <--- paint PNG without any transparency...
destBitmap.Assign(PNGObject); // <--- ...here
destBitmap.ResetAlpha;
srcStream.Position:=0;
PNGObject.LoadFromStream(srcStream); // <--- read the image again to get the alpha channel back
case PNGObject.TransparencyMode of // <--- this is ok now, the alpha channel now only exists in the TBitmap32
{ ... }
The above solution is inefficient because it reads the image twice. But it shows why your second approach produces a more transparent image.
And for the greyishness: There is one more problem in the original code: destBitmap.Assign first fills the background with clWhite32, then paints the image transparently onto it. And then LoadPNGintoBitmap32 comes and adds another layer of transparency on top of it.
The problem may be that the PNG has incorrectly converted to TBitmap32, losing the transparency information in transit. It is a common case with paletted PNG images. Otherwise, you would not had to use „Bitmap.DrawMode := dmTransparent” and the „OuterColor”. If the transparencry information from PNG would have correctly transferred to TBitmpa32, DrawMode := dmBlend would have worked, without the need to set the OuterColor.
What matters most is how did you load a PNG into the TBitmap32. The TPngImage from Vcl.Imaging.pngimage unit (implemented in Delphi XE2 and later) can draw transparently on bitmaps, preserving what was on that bitmaps, combining colors using the PNG alpha layer, etc, but it doesn’t allow to easily convert various formats of PNG transparency (including paletted) into the alpha component of each pixel of TBitmap32. Once TPngImage have drawn an image, you get the combined RGB for each pixel, but the alpha component is not transferred to the target bitmap.
There are helper routines available that try to load a PNG into a TBitmap32 with transparency, but they have drawbacks:
(1) “LoadPNGintoBitmap32” from http://graphics32.org/wiki/FAQ/ImageFormatRelated
- it applies the transparency twice, so the images with alpha values other than 0 or 255 will look differently than in other software (most noticeable on translucent images with glass effects). This code will first apply alpha to RGB and then sets alpha as a separate layer, so when you paint, alpha will be applied again. You can find more information on this issue here: Delphi, GR32 + PngObject: converting to Bitmap32 doesn't work as expected
. Besides that, it doesn't convert correctly transparency from paletted images into the alpha layer of TBitmap32. They manually set alpha transparency for the pixels of a certain color of the output bitmap (rendered to RGB) rather doing that before rendering to RGB, so the actual transparency is lost as on your sample image when all white pixels are transparent.
(2) “LoadBitmap32FromPNG” from gr32ex library: https://code.google.com/archive/p/gr32ex/
- a slightly different implementation of the same algorithm as (1), and has the same issues as (1).
So, the solutions are:
Do not use TBitmap32; use Vcl.Imaging.pngimage.TPngImage do draw directly on target bitmap (screen, etc.) – this is the most compatible way that deals correctly with various PNG formats.
Use a helper routing to transfer transparency information from Vcl.Imaging.pngimage.TPngImage to TBitmap32.
Use the GR32 PNG library that can natively load a PNG into TBitmap32 https://sourceforge.net/projects/gr32pnglibrary/
Since you now have all the information on this issue, you may get the right solution for you.
How to load the alpha layer in one pass
Heinrich Ulbricht made a nice suggestion to remove the transparency layer before paining and then to read the image again. To avoid loading the image twice, you can save the alpha layer before calling PNGObject.RemoveTransparency. Here is the code that correctly applies the alpha layer and loads the image only once. Unfortunately, it does not work with paletted images. If you know how to correctly fill the alpha layer of TBitmap32 from any paletted image, without the effects described at Transparent Png to TBitmap32 please let me know.
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; SrcStream: TStream; out AlphaChannelUsed: Boolean);
var
PNGObject: TPngImage;
PixelPtr: PColor32;
AlphaPtr: PByte;
SaveAlpha: PByte;
I, AlphaSize: Integer;
begin
AlphaChannelUsed := False;
PNGObject := TPngImage.Create;
try
PNGObject.LoadFromStream(SrcStream);
AlphaPtr := PByte(PNGObject.AlphaScanline[0]);
if Assigned(AlphaPtr) then
begin
AlphaSize := PNGObject.Width * PNGObject.Height;
if AlphaSize <= 0 then raise Exception.Create('PNG files with zero dimensions are not supported to be loaded to TBitmap32');
GetMem(SaveAlpha, AlphaSize);
try
Move(AlphaPtr^, SaveAlpha^, AlphaSize);
PNGObject.RemoveTransparency;
DstBitmap.Assign(PNGObject);
DstBitmap.ResetAlpha;
PixelPtr := PColor32(#DstBitmap.Bits[0]);
AlphaPtr := SaveAlpha;
for I := 0 to AlphaSize-1 do
begin
PixelPtr^ := (PixelPtr^ and $00FFFFFF) or (TColor32(AlphaPtr^) shl 24);
Inc(PixelPtr);
Inc(AlphaPtr);
end;
finally
FreeMem(SaveAlpha, AlphaSize);
end;
AlphaChannelUsed := True;
end else
if PNGObject.TransparencyMode = ptmNone then
begin
DstBitmap.Assign(PNGObject);
end else
begin
raise Exception.Create('Paletted PNG images are not supported in LoadPNGintoBitmap32, transparency cannot be stored to TBitmap32');
end;
finally
FreeAndNil(PNGObject);
end;
end;

PNGs in Delphi 2009 Imagelists and Images

D2009 introduces PNG support for Images and Imagelists.
However...
I have an imagelist containing png images with alpha. I want to place one of these on a form using a TImage. How do I do this and get the image nicely composited?
As an example of the problem I'm facing the code below fails to work correctly, and produces the effect shown:
ImageList.GetBitmap(index, Image1.Picture.Bitmap);
(source: clip2net.com)
To explain a bit more:
Drop a Timage on a form, and at design time, load a PNG file with alpha using the Picture property. Note how it is correctly composited with full transparency onto the form.
Now, at design time, add a second empty Timage, add a TImagelist, and add the same PNG to the imagelist. How can I assign the PNG in the TImageList to the second TImage, and have it look identical to the first one?
From my research I found that TImageList stores the images as TBitmaps, so the alpha information is lost on storage, and you can't achieve what you're looking for with the current implementation of TImageList.
Update:
A little more experiments and with the code below i could make transparency work with the code below.
ImageList1.ColorDepth := cd32Bit;
Image2.Transparent := True;
Image2.Canvas.Pen.Style := psClear;
Image2.Canvas.Rectangle(0, 0, Image2.Width+1, Image2.Height+1);
ImageList1.Draw(Image2.Canvas, 0,0,0);
But it didn't look as pretty as a loaded png.
Check the Enable Runtime Themes in the tab at
Project -> Options -> Application tab
This solved my problem for me in RAD Studio 2010.
I just tried a simple test. TImageList contains a PNG image with transparency. I render the image on the second TImage using:
imlImageList.Draw(img2.Canvas, 0, 0, 0);
What made the difference for me was setting img2.Transparent := true (I used the designer, not code).
I stumbled over this discussion-thread:
Tranparent PNGs in D2009 TImageList
#Pekka Nyyssonen: Setting ColorDepth to cd32Bit and DrawingStyle to dsTransparent
worked for me.
I don't have access to delphi 2009 my self so I havn't tried it out, though...
There are several ways to add transparent images to an image list.
With AddMasked or InsertMasked, you add an image and tags a color to be the transparent color:
procedure InsertMasked(Index: Integer; Image: TBitmap; MaskColor: TColor);
function AddMasked(Image: TBitmap; MaskColor: TColor): Integer;
With Insert or Add, you add an image and a mask. The mask if a 2 color (black/white) image where only the white pixels from the image are used, the others are transparent.
function Add(Image, Mask: TBitmap): Integer;
procedure Insert(Index: Integer; Image, Mask: TBitmap);
To the best of my knowledge, this cannot be acheived. None of the suggestions given result in a properly alpha-blended image, which is the primary requirement.
Maybe by defining a class derived from TImageList, which can then access protected methods, something could be got to work. My solution for now is to use a third-party custom ImageList component specifically for this.

Resources