How to put white/transparent background on png image with black background

Asked

Viewed 596 times

2

I’m using this code in Azarus but at the time of saving it shows a black part like the image, I wanted it to be white/transparent. I tried some manners and it didn’t work.

  procedure TformMain.btSalvarImagemClick(Sender: TObject);
  var
  bmp: TBitmap;
  R: TRect;
  png : TPortableNetworkGraphic;
  begin
  // bmp, png
  bmp := TBitmap.Create;
  png := TPortableNetworkGraphic.Create;
  try
  // bmp
  if SavePictureDialog1.Execute then
  begin
  R := Rect(500, 500, BarcodeQR1.Width, BarcodeQR1.Height);
  bmp.SetSize(600, 600);
  bmp.Canvas.Brush.Color := clWhite;
  bmp.Canvas.FillRect®;
  BarcodeQR1.PaintOnCanvas(bmp.Canvas, R);
  png.Assign(bmp);
  png.SaveToFile(SavepictureDialog1.Filename);
  end;
  finally
  bmp.Free;
  png.Free;
  end;
  end;`

QrCode

3 answers

4


I got

procedure TformMain.btSalvarImagem1Click(Sender: TObject); var bmp: TBitmap; R: TRect; png : TPortableNetworkGraphic; begin // bmp, png bmp := TBitmap.Create; png := TPortableNetworkGraphic.Create; try // bmp if SavePictureDialog1.Execute then begin BarcodeQR2.Height := 600; BarcodeQR2.Width := 600; R := Rect(0, 0, BarcodeQR2.Width, BarcodeQR2.Height); bmp.SetSize(BarcodeQR2.Width, BarcodeQR2.Height); bmp.Canvas.Brush.Color := clWhite; bmp.Canvas.FillRect(R); BarcodeQR2.PaintOnCanvas(bmp.Canvas, R); png.Assign(bmp); png.SaveToFile(SavepictureDialog1.Filename); BarcodeQR2.Height := 120; BarcodeQR2.Width := 105; end; finally bmp.Free; png.Free; end; end;

QRCode

1

I found that answer, try to put this in the code:

 bmp.TransparentColor := bmp.Canvas.Pixels[0,0];

Then place the insto

 bmp.Transparent := True

1

Replace the excerpt from:

  png.Assign(bmp);
  png.SaveToFile(SavepictureDialog1.Filename);

for:

  png.PixelFormat := pf32bit;
  png.Transparent := True;
  png.TransparentColor := clWhite;
  png.Assign(bmp);
  png.SaveToFile(SavepictureDialog1.Filename);
  • The strange that if I open the site after saving the image and give a Ctrl + v the image appears straight, but saving normal no

  • @Jacksonfelipemagnabosco, try to open the saved image with other software than the windows viewer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.