Copy codeThe Code is as follows: private void button#click (object sender, EventArgs e)
{
If (string. IsNullOrEmpty (this. textBox1.Text. Trim ()))
{
MessageBox. Show ("enter the information to be converted! ");
Return;
}
String content = textBox1.Text;
Hashtable hints = new Hashtable ();
Hints. Add (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. L); // Error Correction level
Hints. Add (EncodeHintType. CHARACTER_SET, "UTF-8"); // encoding format
ByteMatrix byteMatrix = new MultiFormatWriter (). encode (content, BarcodeFormat. QR_CODE, 300,300, hints );
Bitmap bitmap = toBitmap (byteMatrix );
PictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog ();
SFD. Filter = "*. png | *. png ";
SFD. AddExtension = true;
Try
{
If (sFD. ShowDialog () = DialogResult. OK)
{
WriteToFile (byteMatrix, System. Drawing. Imaging. ImageFormat. Png, sFD. FileName );
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
Public static void writeToFile (ByteMatrix, System. Drawing. Imaging. ImageFormat format, string file)
{
System. Drawing. Imaging. EncoderParameters eps = new System. Drawing. Imaging. EncoderParameters ();
Eps. Param [0] = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, 100L );
Bitmap bmap = toBitmap (matrix );
Bmap. Save (file, format );
}
Public static Bitmap toBitmap (ByteMatrix)
{
Int width = matrix. Width;
Int height = matrix. Height;
Bitmap bmap = new Bitmap (width, height, System. Drawing. Imaging. PixelFormat. Format32bppArgb );
For (int x = 0; x <width; x ++)
{
For (int y = 0; y {
Bmap. SetPixel (x, y, matrix. get_Renamed (x, y )! =-1? ColorTranslator. FromHtml ("Purple"): ColorTranslator. FromHtml ("0 xFFFFFFFF"); // you can customize the color and background color.
}
}
Return bmap;
}