This project has moved. For the latest updates, please go here.

How to add icon inside and change the drawing color?

Jul 26, 2013 at 2:51 AM
Thanks for any help, and a code sample is greatly appreciated!
Jul 29, 2013 at 9:49 AM
Anyone can help?
Coordinator
Jul 29, 2013 at 9:51 PM
Perhaps you should give some more information like what target platform you are using (Windows Phone, WinForms, etc.) and what you have tried so far.

If you want to change the color of the generated code you can use the following code snippet:
var content = "some text which should be encoded";
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Renderer = new BitmapRenderer
      {
         Foreground = Color.Blue
      }
};
var image = writer.Write(content);
If you want an icon within your qr code image you have to draw it yourself.
For a win forms application you can draw an icon in the middle of the image with the following code snippet:
var content = "some text which should be encoded";
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Renderer = new BitmapRenderer
      {
         Foreground = Color.Blue
      }
};
var image = writer.Write(content);
var icon = System.Drawing.Icon.ExtractAssociatedIcon(GetType().Assembly.Location);
using (var graphics = System.Drawing.Graphics.FromImage(image))
{
   graphics.DrawIcon(icon, (image.Width - icon.Width)/2, (image.Height - icon.Height)/2);
}
Replace "var icon = ..." with you icon loading routine.
Jul 31, 2013 at 7:56 AM
Thanks so much, it can work perfectly.

But why I can't set the Hints including Error_Correction, Margin properties? Is there a workaround here or they're not supported for now?
Jul 31, 2013 at 7:59 AM
Options = new EncodingOptions
            {
                Height = 160,
                Width = 160,
                Margin = 6
            }
I set the margin property here, but it doesn't take effect in the generated bar picture. Please help to check.
Coordinator
Jul 31, 2013 at 8:05 AM
Try this:
var content = "some text which should be encoded";
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Renderer = new BitmapRenderer
      {
         Foreground = Color.Blue
      },
   Options = new ZXing.QrCode.QrCodeEncodingOptions
      {
         Height = 400,
         Width = 400,
         Margin = 10,
         ErrorCorrection = ErrorCorrectionLevel.M
      }
};
var image = writer.Write(content);
var icon = System.Drawing.Icon.ExtractAssociatedIcon(GetType().Assembly.Location);
using (var graphics = System.Drawing.Graphics.FromImage(image))
{
   graphics.DrawIcon(icon, (image.Width - icon.Width)/2, (image.Height - icon.Height)/2);
}
Coordinator
Jul 31, 2013 at 8:04 PM
Which kind of barcode did you try to generate?
Aug 1, 2013 at 2:35 AM
Edited Aug 1, 2013 at 2:37 AM
Thanks for the code snippet, but it still can't take effect. I'm generating QR Code. Please see this picture for the Margin property which is obviously more than 6 pixels(I set background color as Silver):

Image
Coordinator
Aug 2, 2013 at 10:01 PM
That happens because of the stretching of the images to the given width and height. The margin is stretched, too.
Try encoding with the following options:
   Options = new ZXing.QrCode.QrCodeEncodingOptions
      {
         Height = 1,
         Width = 1,
         Margin = 10,
...
      }
That should give a QR code with a margin of 10 pixels.
Aug 3, 2013 at 5:26 AM
Thanks, but it still doesn't work. The margin between the QR Code and background doesn't change. Please check it.
Coordinator
Aug 4, 2013 at 7:37 PM
For me it works. Please post the code which you are currently using, the resulting image and a modified image which shows the result which you are expecting.