2016-01-04 475 views
-2

使用這個代碼行波紋管生成QR碼圖像

private void button1_Click(object sender, EventArgs e) 
    { 
     string barcode = textBox1.Text; 
     Bitmap bitmap = new Bitmap(barcode.Length * 40, 150); 

     using (Graphics graphics = Graphics.FromImage(bitmap)) 
     { 
      Font oFont = new System.Drawing.Font("IDAutomationHC39M", 20); 
      PointF point = new PointF(2f, 2f); 
      SolidBrush white = new SolidBrush(Color.White); 
      SolidBrush black = new SolidBrush(Color.Black); 
      graphics.FillRectangle(white,0,0,bitmap.Width,bitmap.Height); 
      graphics.DrawString("*" + barcode + "*", oFont, black, point); 
     } 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      bitmap.Save(ms,ImageFormat.Png); 
      pictureBox1.Image = bitmap; 
      pictureBox1.Height = bitmap.Height; 
      pictureBox1.Width = bitmap.Width; 
     } 
    } 

後我能夠產生該輸出圖像條形碼

enter image description here

我想要生成條形碼輸出像圖像下面我怎麼能達到這個

enter image description here

+0

這是QR碼(https://開頭en.wikipedia.org/wiki/QR_code) – Idos

+0

應該真的被關閉爲「搜索工具/庫「,但是因爲已經存在關於相同搜索的問題(關鍵問題),而不是關閉原因錯誤。 –

+0

首先使用Google,查看您的問題是否已被問及回答! –

回答

1

你可以做這樣的

private System.Drawing.Image GenerateQRCode(string content, int size) 
{ 
    QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H); 
    QrCode qrCode; 
    encoder.TryEncode(content, out qrCode); 

    GraphicsRenderer gRenderer = new GraphicsRenderer(new FixedModuleSize(4, QuietZoneModules.Two), System.Drawing.Brushes.Black, System.Drawing.Brushes.White); 
    //Graphics g = gRenderer.Draw(qrCode.Matrix); 

    MemoryStream ms = new MemoryStream(); 
    gRenderer.WriteToStream(qrCode.Matrix, ImageFormat.Bmp, ms); 

    var imageTemp = new Bitmap(ms); 

    var image = new Bitmap(imageTemp, new System.Drawing.Size(new System.Drawing.Point(size, size))); 

    //image.Save("file.bmp", ImageFormat.Bmp); 

    return (System.Drawing.Image)image; 
} 

Implementaion

string barcode = textBox1.Text; 
codeImage = GenerateQRCode(barcode, 120); 
// you can make a smaller image as per your need 
rect = new System.Drawing.Rectangle(1080, 530, codeImage.Width, codeImage.Height); 
using (Graphics g = Graphics.FromImage(picEdit)) 
{ 
    g.SmoothingMode = SmoothingMode.AntiAlias; 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
    g.DrawImage(codeImage, rect); 
} 

不要忘記添加

using Gma.QrCodeNet.Encoding; 
using Gma.QrCodeNet.Encoding.Windows.Render; 

GitHub: QrCode.Net

+0

QrCode和QrEncoder在哪些命名空間或庫下面? –

+0

在'主'線程中發佈這個答案這個解決方案沒有提到那裏... btw我沒有投票 –