5

I am using Barcode Studio 2011 to paint a QR Code into a Graphics32 - TImage32 Component and I want to save it in png format but with the white colour as transparent this I have specified in the OuterColor of Graphics32.

OnFormCreate I have just

procedure TForm1.FormCreate(Sender: TObject);
begin
  psBarcodeComponent1.BarCode := 'some text here...';
end;

and for the moment I have the painting assigned to a Button Click Event

procedure TForm1.Button8Click(Sender: TObject); // Paint the barcode
var
  bmp: TBitmap32;
  Coords: TRect;
begin
 bmp := TBitmap32.Create;
 bmp.SetSize(image.Width, image.Height);
 bmp.Canvas.Brush.Color := color;
 bmp.Canvas.Rectangle(-1, -1, image.Width+2, image.Height+2);

 bmp.DrawMode := dmTransparent;
 bmp.OuterColor := clWhite;

 // make Coords the size of image
 Coords := Rect(0,0,image.Width,image.Height);
 psBarcodeComponent1.PaintBarCode(bmp.Canvas, Coords);
 image.Bitmap.Assign(bmp);
end;

I am using the Vampyre Imaging Library to convert the Bitmap into PNG Format but I will gladly use any library, function, and advice - I have been trying to do this now for nearly a week! I have read through and re-read the documentation of graphics32 and also of the Vampyre Imaging Library but nothing I try will convert the white to a transparent colour. I have tried clWhite, clWhite32 and also setting the drawMode to dmBlend and applying the ChromaKey Function all to no avail but plenty frustration, coffee and a little beer also ;)

This is how I am saving it...

procedure TForm1.Button7Click(Sender: TObject); // Save with Vampyre Imaging Lib
{ Try to save in PNG format with transparancy }
var
  FImage: TSingleImage;
begin
  FImage := TSingleImage.Create;
  ConvertBitmap32ToImage(image.Bitmap, FImage);
  FImage.SaveToFile('VampyreLibIMG.png');
end;  

This results in a Black coloured thumbnail and when viewed in Windows Photo Viewer it is completely transparent.

I hope that I have provided enough information and that someone is able to help me.

Chris

Shambhala
  • 1,159
  • 3
  • 13
  • 31
  • what happens when you set bmp.transparentColor := clWhite; in stead of bmp.outerColor := clWhite? –  Aug 04 '11 at 23:03
  • There is no member for that in TBitmap32 - I have also tried picking the OuterColor from a Pixel but it has no effect. bmp.OuterColor := bmp.Pixel[0,1]; – Shambhala Aug 04 '11 at 23:32

2 Answers2

6

You haven't specified the Delphi version, but if your delphi version has "PngImage"(I believe it comes with D2009+) the code bellow works perfectly(loaded in Gimp and Windows Photo Viewer, it draws a frame and some text with transparent background, feel free to play with it:

uses
  PngImage;

procedure TForm1.OnBtnClick(Sender: TObject);
var
  bmp: TBitmap;
  png: TPngImage;
begin
  bmp := TBitmap.Create;
  bmp.Width := 200;
  bmp.Height := 200;

  bmp.Canvas.Brush.Color := clBlack;
  bmp.Canvas.Rectangle( 20, 20, 160, 160 );

  bmp.Canvas.Brush.Style := bsClear;
  bmp.Canvas.Rectangle(1, 1, 199, 199);

  bmp.Canvas.Brush.Color := clWhite;
  bmp.Canvas.Pen.Color := clRed;
  bmp.Canvas.TextOut( 35, 20, 'Hello transparent world');

  bmp.TransparentColor := clWhite;
  bmp.Transparent := True;

  png := TPngImage.Create;
  png.Assign( bmp );
  png.SaveToFile( 'C:\test.png' );

  bmp.Free;
  png.Free;
end;
  • Thank you for that, I will switch to a standard TImage and try your code adapted. I am using Delphi 2010 – Shambhala Aug 05 '11 at 01:56
  • Delphi 2010 will handle this perfectly, don't forget that TImage also has some transpiracy options and to accept it as answer if it's helpful. (: –  Aug 05 '11 at 02:26
4

This approach works for me:

uses GR32, GR32_PNG, GR32_PortableNetworkGraphic;

var
  Y: Integer;
  X: Integer;
  Png: TPortableNetworkGraphic32;

  function IsWhite(Color32: TColor32): Boolean;
  begin
    Result:= (TColor32Entry(Color32).B = 255) and
             (TColor32Entry(Color32).G = 255) and
             (TColor32Entry(Color32).R = 255);
  end;

begin
  with Image321 do
  begin
    Bitmap.ResetAlpha;
    for Y := 0 to Bitmap.Height-1 do
      for X := 0 to Bitmap.Width-1 do
      begin
        if IsWhite(Bitmap.Pixel[X, Y]) then
          Bitmap.Pixel[X,Y]:=Color32(255,255,255,0);
      end;
    Png:= TPortableNetworkGraphic32.Create;
    Png.Assign(Bitmap);
    Png.SaveToFile('C:\Temp\NowTransparent.png');
    Png.Free;
  end;
end;

This uses the GR32 PNG library. It's a pretty direct way, setting all white pixels to transparent.

PS: Image321 is a TImage32 component, containing my TBitmap32.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • this example got me an error. in "procedure TCustomBitmap32.Assign(Source: TPersistent)" in unit GR32.pas; any help? – XBasic3000 Sep 10 '12 at 07:18