Author Topic: Drawing White PNG Images: Problem  (Read 1660 times)

mar_je

  • Guest
Drawing White PNG Images: Problem
« on: 12 March 2009, 21:15:51 »
First of all, sorry for my English (I'm foreinger :))

Hi.

I want to draw image (you can download it from: http://communicom.w8w.pl/image_not_working.png) on a TBitmap, but it's some problem with a white parts of image (look at the screen):


The second (black) picture is painted well with the same code:

Code: [Select]
FImage2:=TMultiImage.create;
FImage2.loadmultifromfile('Skin\window.png');
FLayeredWindow.Surface.Width:=Form1.Width;
FLayeredWindow.Surface.Height:=Form1.Height;
ImagingComponents.DisplayImage(FLayeredWindow.Surface.Canvas,Rect(0,0,Form1.Width,Form1.Height),Fimage2);
FLayeredWindow.UpdateLayer;
FImage2.Free;

This code is working well for a white image too.:
Code: [Select]
TestPNG:=TPNGObject.Create;
TestPNG.LoadFormFile('Skin\window.png');
FLayeredWindow.Surface.Width:=Form1.Width;
FLayeredWindow.Surface.Height:=Form1.Height;
FLayeredWindow.Surface.Assign(TestPNG);
FLayeredWindow.UpdateLayer;

I think, you can understand me  ;). Can you help me to fix a problem?

//I'm using "O3LayeredWindow" component to create layered Form. System: Win XP SP2; Delphi 2009

mar_je

  • Guest
Re: Drawing White PNG Images: Problem
« Reply #1 on: 12 March 2009, 21:22:15 »
Sorry, but I have screen for second procedure. ->Look attachment.

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: Drawing White PNG Images: Problem
« Reply #2 on: 13 March 2009, 02:41:48 »
What about if you convert PNG image loaded by Imaging to TBitmap instead of using DisplayImage? DisplayImage doesn't do any alpha blending which I guess is needed here.

You can directly use TImagingBitmap class to load the PNG:
Code: [Select]
TestPNG:=TImagingBitmap.Create;
TestPNG.LoadFormFile('Skinwindow.png');
FLayeredWindow.Surface.Width:=Form1.Width;
FLayeredWindow.Surface.Height:=Form1.Height;
FLayeredWindow.Surface.Assign(TestPNG);
FLayeredWindow.UpdateLayer;

Offline mar_je

  • Imaging User
  • *
  • Posts: 4
    • View Profile
Re: Drawing White PNG Images: Problem
« Reply #3 on: 13 March 2009, 09:43:28 »
Thanks for a fast reply, but the result is the same as my procedure  :(. I create sample apps for You can check it (images, procedure with PNGImage, Your procedure, component for create layeredWindow).

(I uploaded it on my server, beacouse it's larger than maximum individual size in attachment. Download samples with sources from):
Code: [Select]
http://communicom.w8w.pl/SampleApps.rar

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: Drawing White PNG Images: Problem
« Reply #4 on: 13 March 2009, 16:19:15 »
Ok I got it, the bitmap needs to be premultiplied by alpha for layered window to display properly. PNGImage does this by itself when converting to TBitmap.
You can try this code:
Code: [Select]
var
  testPNG:TImagingBitmap;
  X, Y: Integer;
  Dest, Src: PByte;
  Alpha: Byte;
begin
  FLayeredWindow:=TO3LayeredWindow.Create(Form1);
  FLayeredWindow.Parent:=Form1;
  testPNG:=TImagingBitmap.create;
  testPNG.LoadFromFile('..\Images\image_not_working.png');
  FLayeredWindow.Surface.Width:=testPNG.Width;
  FLayeredWindow.Surface.Height:=testPNG.Height;
  FLayeredWindow.Surface.PixelFormat:=pf32Bit;

  with FLayeredWindow.Surface do
    for Y := 0 to Height - 1 do
    begin
      Src := testPNG.Scanline[y];
      Dest := ScanLine[y];
      for X := 0 to Width - 1 do
      begin
        Alpha := Src[X * 4 + 3];

        Dest[X * 4 + 0] := Src[x * 4 + 0] * Alpha div 255;
        Dest[X * 4 + 1] := Src[x * 4 + 1] * Alpha div 255;
        Dest[X * 4 + 2] := Src[x * 4 + 2] * Alpha div 255;
        Dest[X * 4 + 3] := Alpha;
      end;
    end;

  FLayeredWindow.UpdateLayer;
end;

Offline mar_je

  • Imaging User
  • *
  • Posts: 4
    • View Profile
Re: Drawing White PNG Images: Problem
« Reply #5 on: 13 March 2009, 16:34:31 »
Man, you're GREAT  ;D. It's working...

//Awensome support for a libary :)