Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
Dim x As Integer
Dim y As Integer
If tolerance > 1 Or tolerance < -1 Then
Throw New ArgumentOutOfRangeException
Exit Function
End If
For x = 0 To image.Width - 1 Step 1
For y = 0 To image.Height - 1 Step 1
Dim clr As Color = image.GetPixel(x, y)
If Mode = BWMode.By_RGB_Value Then
If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
image.SetPixel(x, y, Color.White)
Else
image.SetPixel(x, y, Color.Black)
End If
Else
If clr.GetBrightness > 0.5 - (tolerance / 2) Then
image.SetPixel(x, y, Color.White)
Else
image.SetPixel(x, y, Color.Black)
End If
End If
Next
Next
Return image
End Function
Enum BWMode
By_Lightness
By_RGB_Value
End Enum
Code (VB.NET)
Dim MinX As Integer = W : Dim MaxX As Integer = 0
Dim MinY As Integer = H : Dim MaxY As Integer = 0
Dim White As Integer = Color.White.ToArgb()
For x As Integer = 0 To W - 1
For y As Integer = 0 To H - 1
If Not Output.GetPixel(x, y).ToArgb() = White Then
MinX = Math.Min(MinX, x)
MaxX = Math.Max(MaxX, x)
MinY = Math.Min(MinY, y)
MaxY = Math.Max(MaxY, y)
End If
Next
Next
Code (VB.NET)
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
String encoding = cboEncoding.Text ;
if (encoding == "Byte") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
} else if (encoding == "AlphaNumeric") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
} else if (encoding == "Numeric") {
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
}
try {
int scale = Convert.ToInt16(txtSize.Text);
qrCodeEncoder.QRCodeScale = scale;
} catch (Exception ex) {
MessageBox.Show("Invalid size!");
return;
}
try {
int version = Convert.ToInt16(cboVersion.Text) ;
qrCodeEncoder.QRCodeVersion = version;
} catch (Exception ex) {
MessageBox.Show("Invalid version !");
}
string errorCorrect = cboCorrectionLevel.Text;
if (errorCorrect == "L")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
else if (errorCorrect == "M")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
else if (errorCorrect == "Q")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
else if (errorCorrect == "H")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
Image image;
String data = txtEncodeData.Text;
image = qrCodeEncoder.Encode(data);
picEncode.Image = image;