This project is read-only.

Amount of data that can be encoded

Feb 2, 2012 at 7:24 PM


I would like to embed the control in a fixed area of a already densly populated winform but have no clue how to check if the text written to the control can actually be encoded.

So the Question: Is it possible to add a property that returns the number of characters (or bytes) that can be encoded given a size and eror correction? An alternative might be to have a method returns false if the text won't fit (without enlarging the control off-course).

I noticed that there is a call in the code like:

m_Encoder.TryEncode(this.Text, out m_QrCode);

that returns false if the text won't fit (so it could be a start). But the the QREncoder property is private and read-only and I do not want to render text yet (when testing).

Furthermore, it would be an enhancement (for me) if the QRCode could be aligned within the control (currently it's topleft but for example center|center would be more easy on the eye). So could this be added too (align the QRcode to left|middle|right and Top|middle|bottom)?



Feb 2, 2012 at 10:19 PM

For length of text that can be encode by encoder, you can check ISO/IEC 18004:2006 Page 33, Table 7. (Document you can download from front page of this project)

If all your character is English and upper case, it can fit 4296 characters inside, else will be around 2953 characters(For version 40). It really depends what character contain inside your input string.

As you can see on that table, QrCode has numbers of different encode method, It can not tell you exactly how many characters it can fit. Only during process. TryEncode method is the one catch exception and tells developer either input string is empty, null or string is too large. While for method "Encode" you have to try-catch on your own. 

InputOutOfBoundaryException << This is what encoder use for report string empty, null or can not fit. 

It is possible to add method that return how many codewords you can fit to specific version of QrCode, but not how many characters as I have explained at start. One codeword = 1 byte = 8 bits. To determine how many char it can fit we have to see which char encode we will be using and how many codeword we have after encode, also check if it need ECI header and normal header's size depend on version. It's very complicate process. 

For QrCode control. Which one do you use? There are two different kind of control. If you use QrCodeImgControl, that one just act like Winform's PictureBox. It can stretch image and make it always at middle of that control. Just remember to set up proper module size, so it won't consume too much memory. 

I'm not sure what left|middle|right means.

Do you mean QrCode hug left side or right side without QuietZone? Technically we can not create any QrCode without QuietZone, that way we can make sure every code can be proper decoded. As ISO/IEC state, we need 4 modules wide for QuietZone, but as in reality 2 modules wide normally is enough. That's why we have provide property to change QuietZone between 4 modules and 2 modules. (QuietZone is present by white brush, same as 0 module inside QrCode). 

As our default control doesn't have background, so it will be either expand code from position (0, 0), or act like PictureBox, stretch image. Size of control always exactly what QrCode needs or try to fit your config by stretch image. Thus there is no need for position properties. 

If you want paint it on some background, we do provide full support for offset position rendering inside Renderer class which locate with control. 


Sorry for un-detailed documentation. I might put some work on it later. 




Feb 3, 2012 at 9:54 AM
Edited Feb 3, 2012 at 9:55 AM


Thanks for the quick response!

I'm using the QrCodeControl now but will switch the QrCodeImgControl if that has centering (thats what i'm looking for). The currrent one btw has a quitzone allright but it's topleft aligned in it's control area (and test as readable just fine).

As for TryEncode, if you use the controls, it's QrEncoder is private (and thus you cannot calls it;s TryEncode) so you have to create a new one. I would prefer a simple test method on the controls  that creates a copy of m_encoder inclding the settings and tries the TryEncode on it. That way you could test without disturbing the current QrCode on screen.

So what i would like in the controls, is a method like:

public Boolean TryEncode(string content) {
  //make a copy including all settings using a copy constructor.
  QrCode tmp = new QrCode(m_QrCode);
  return m_encoder.TryEncode(content, out tmp);

And hoping that it does not disturb m_encoder and m_renderer in the process offc-course. With such method i could test user input and restrict it to encodeable length easily (QrCode.NET seems fast enougth for this).

As for codeblocks and characters, yep it's complex (so i'm quite happy that you have code it ;-)).


Feb 3, 2012 at 10:50 AM
Edited Feb 3, 2012 at 10:55 AM

Hi. I think I got what you want now. 

At your project using following namespace. 

using Gma.QrCodeNet.Encoding;

Then you can create QrCodeEncoder anywhere within the class that imported that namespace. 
For renderer, that class is under "Gma.QrCodeNet.Encoding.Windows.Controls" or "Gma.QrCodeNet.Encoding.Windows.WPF".
The one under Controls is written base on drawing project on "Graphics". While WPF one drawing content on "DrawingContext" or "WriteableBitMap".
So use the one that fit's for your project will do.

The problem with QrCodeControl is that Control's display will be constant module size, thus with not resizeable setting, it will eventually run out of space, I could actually code it that expand to different direction.
As currently it will expand towards bottom right, it should be able to expand towards other direction. Only problem will be if people really need it or not.

For QrCodeImgControl, WinForm control's auto resize is under property SizeMode, where WPF version of ImgControl's auto resize is just name as AutoSize. So as you want constant size make sure those option are off.
And stretch option is set up to Fill with equal width and height.

Hope that helps. Let me know if you have further questions.


Feb 4, 2012 at 8:07 PM


Thanks, the QrCodeImgControl works like charm and has indeed the center option I was looking for! I completly overlooked it.

Btw Any comments on my TryEncode question (adding a wrapper around it that uses a copy of QrCode so the OnScreen QRCode remains untouched)?


Feb 4, 2012 at 10:56 PM
Edited Feb 4, 2012 at 11:43 PM

Hi wvd_vegt. I'm glade to see you like QrCodeImgControl. 

For TryEncode, you can create QrCodeEncoder without actual QrCodeImgControl. Make sure your class have "using Gma.QrCodeNet.Encoding" imported inside. WIthin that class you can create encoder anywhere you want. Here is some walk through. 

At start:

using Gma.QrCodeNet.Encoding;

Then inside any function you can type:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = new QrCode();
qrEncoder.TryEncode(helloWorld, out qrCode); //helloworld is string variable that stored some input string.

So that way, anything you type in won't affect actual display Control. 

Hope that helps. 



My reason not implement public property for TryEncode is that output of variable which is QrCode is also under that namespace assembly.
If you have to import that assembly might as well create QrCodeEncoder instead of using the one inside. QrCodeEncoder it self doesn't contain any variables, it's just memory free calculation class. Maybe I should just make it to static later. :P