codebude/QRCoder

How to generate qr code with transparent background?

saklanmazozgur opened this issue · 2 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;
}

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.

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.)