Support
SiteLinks |
FAQ /
ImageFormatRelated<< Alpha transparency | Frequently asked questions | Resampling >> 1. I can not load JPEG images at run-time, but at design-time this does work. What's wrong?Include JPEG unit into the uses clause in your main application unit. 2. Is there a PNG library for GR32 which supports loading the image and its alpha channel at the same time?Yes, you might want to take a look at GraphicEx? (http://www.soft-gems.net/Graphics.php#GraphicEx). This supports loading PNGs? with alpha channel out of the box. However, PNG support in GraphicEx? is very basic and there is no way to save back to PNG. For a more complete support of the PNG image format you might therefore want to consider TPNGImage? (http://pngdelphi.sourceforge.net). However, there is one catch here: This one does not include support for Graphics32 out of the box. For that purpose you can use the following code:
unit GR32_PNG;
interface
uses
SysUtils, Windows, Classes, GR32, Graphics;
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; Filename: String; out AlphaChannelUsed: Boolean); overload;
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; SrcStream: TStream; out AlphaChannelUsed: Boolean); overload;
implementation
uses PNGImage;
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; SrcStream: TStream; out AlphaChannelUsed: Boolean);
var
PNGObject: TPNGObject;
TransparentColor: TColor32;
PixelPtr: PColor32;
AlphaPtr: PByte;
X, Y: Integer;
begin
PNGObject := nil;
try
PNGObject := TPngObject.Create;
PNGObject.LoadFromStream(SrcStream);
DstBitmap.Assign(PNGObject);
DstBitmap.ResetAlpha;
case PNGObject.TransparencyMode of
ptmPartial:
begin
if (PNGObject.Header.ColorType = COLOR_GRAYSCALEALPHA) or
(PNGObject.Header.ColorType = COLOR_RGBALPHA) then
begin
PixelPtr := PColor32(@DstBitmap.Bits[0]);
for Y := 0 to DstBitmap.Height - 1 do
begin
AlphaPtr := PByte(PNGObject.AlphaScanline[Y]);
for X := 0 to DstBitmap.Width - 1 do
begin
PixelPtr^ := (PixelPtr^ and $00FFFFFF) or (TColor32(AlphaPtr^) shl 24);
Inc(PixelPtr);
Inc(AlphaPtr);
end;
end;
AlphaChannelUsed := True;
end;
end;
ptmBit:
begin
TransparentColor := Color32(PNGObject.TransparentColor);
PixelPtr := PColor32(@DstBitmap.Bits[0]);
for X := 0 to DstBitmap.Height * DstBitmap.Width - 1 do
begin
if PixelPtr^ = TransparentColor then
PixelPtr^ := PixelPtr^ and $00FFFFFF;
Inc(PixelPtr);
end;
AlphaChannelUsed := True;
end;
ptmNone:
AlphaChannelUsed := False;
end;
finally
if Assigned(PNGObject) then PNGObject.Free;
end;
end;
procedure LoadPNGintoBitmap32(DstBitmap: TBitmap32; Filename: String; out AlphaChannelUsed: Boolean);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(Filename, fmOpenRead);
try
LoadPNGintoBitmap32(DstBitmap, FileStream, AlphaChannelUsed);
finally
FileStream.Free;
end;
end;
end.
Code usage example:
var
AlphaChannelUsed: Boolean
begin
LoadPNGintoBitmap32(MyBitmap, 'someimage.png', AlphaChannelUsed);
if AlphaChannelUsed then
// add anything else that should be done if we have
// an alpha channel with our PNG image...
MyBitmap.DrawMode := dmBlend
else
MyBitmap.DrawMode := dmOpaque;
end;
|