QRCoder icon indicating copy to clipboard operation
QRCoder copied to clipboard

How to generate qr code with transparent background?

Open saklanmazozgur opened this issue 2 years ago • 1 comments

Hello,

I am generating qrcode as below. Even though I made the background transparent, it casts as black.

public static Bitmap CreateQrCode(string code, int quality)
{
      QRCodeGenerator qrGenerator = new QRCodeGenerator();
      QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
      QRCode qrCode = new QRCode(qrCodeData);
      Bitmap qrCodeImage = qrCode.GetGraphic(quality, Color.Black, Color.Transparent, true);

     return qrCodeImage;
}

saklanmazozgur avatar Aug 10 '23 08:08 saklanmazozgur

I think you shouldn't use transparent in your QR Code image. It's violate the QR Code spec. The white-part has it's purpose.

doggy8088 avatar Aug 29 '23 14:08 doggy8088

Hi @saklanmazozgur ,

Your code produces a perfectly fine transparent image. I think the problem lies in (the not shown) part where you display the code. To keep transparency you have to save the Bitmap in a format that supports transparency, e.g. PNG. If you save the Bitmap as bmp, transparency will get lost. But that's not QRCoders fault, but how BMP works.

If your problem is missing transparency when showing the QRCode in a UI component, be aware that you have to set a transparency key for many of the components. E.g. for PictureBox it's done that way: https://stackoverflow.com/questions/19910172/how-to-make-picturebox-transparent (For other controls it might be different, but can't help you here, without knowing how/where you are displaying the QR code.)

codebude avatar Apr 28 '24 11:04 codebude