Esempio n. 1
0
 /// <summary>
 /// 在二维码的中添加它的内容的文字
 /// </summary>
 /// <param name="image"></param>
 /// <param name="content"></param>
 public static Image QRCodeAddContent(Image image, string content, RectangleF rectangleF, Font font = null, SolidBrush solidBrush = null, StringFormat stringFormat = null)
 {
     try
     {
         //参数为空时的默认值
         if (font == null)
         {
             font = new Font("黑体", (image.Height / 10) / 2);
         }
         if (solidBrush == null)
         {
             solidBrush = new SolidBrush(Color.Black);
         }
         if (stringFormat == null)
         {
             stringFormat = new StringFormat(StringFormatFlags.FitBlackBox);
         }
         //计算绘制文本所需的大小
         using (Graphics graphics = Graphics.FromImage(image))
         {
             SizeF sizef = graphics.MeasureString(content, font, image.Width, stringFormat);
             rectangleF = new RectangleF()
             {
                 X      = rectangleF.X,
                 Y      = rectangleF.Y,
                 Width  = rectangleF.Width,
                 Height = sizef.Height,
             };
         }
         Size newSize = new Size()
         {
             Width  = image.Size.Width,
             Height = Convert.ToInt32(image.Height + rectangleF.Height),
         };
         Point point = new Point()
         {
             X = 0,
             Y = 0,
         };
         image = QRCodeCommon.ChangImageSize(image, newSize, point);
         using (Graphics graphics = Graphics.FromImage(image))
         {
             graphics.DrawString(content, font, solidBrush, rectangleF, stringFormat);
         }
         return(image);
     }
     catch (Exception)
     {
         throw;
     }
 }
Esempio n. 2
0
 /// <summary>
 /// 把文本保存为条码
 /// </summary>
 /// <param name="path">保存路径</param>
 /// <param name="content">文本内容</param>
 /// <param name="size">图片大小</param>
 /// <param name="barcodeFormat">条码种类</param>
 /// <param name="margin">外边距</param>
 /// <param name="encoding">编码模式</param>
 /// <param name="errorCorrectionLevel">错误修正等级</param>
 /// <param name="imageFormat">图片类型</param>
 public static void SavePicture(
     string path, string content, Size size, BarcodeFormat barcodeFormat, int margin = 0, string encoding = "UTF-8",
     ErrorCorrectionLevel errorCorrectionLevel = null, ImageFormat imageFormat = null)
 {
     try
     {
         using (Stream stream = File.Create(path))
         {
             Image image = QRCodeCommon.TextToBitmap(content, size, barcodeFormat, margin, encoding, errorCorrectionLevel);
             //保存不了Ico文件。。。不知道为什么
             image.Save(stream, imageFormat ?? ImageFormat.Png);
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 把文本转换成Bitmap
 /// </summary>
 /// <param name="content">文本内容</param>
 /// <param name="size">图片大小</param>
 /// <param name="barcodeFormat">条码种类</param>
 /// <param name="margin">外边距</param>
 /// <param name="encoding">编码模式</param>
 /// <param name="errorCorrectionLevel">错误修正等级</param>
 /// <returns></returns>
 public static Bitmap TextToBitmap(
     string content, Size size, BarcodeFormat barcodeFormat, int margin = 0, string encoding = "UTF-8", ErrorCorrectionLevel errorCorrectionLevel = null)
 {
     try
     {
         //在生成图片之前先处理下Size然后在下方加上数据
         BarcodeWriter barcodeWriter = new BarcodeWriter();
         barcodeWriter.Format         = barcodeFormat;
         barcodeWriter.Options.Width  = size.Width;
         barcodeWriter.Options.Height = size.Height;
         barcodeWriter.Options.Margin = margin;
         barcodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, encoding);
         barcodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel ?? ErrorCorrectionLevel.H);
         ///bit矩阵??
         ZXing.Common.BitMatrix bitMatrix = barcodeWriter.Encode(content);
         Bitmap     bitmap     = barcodeWriter.Write(bitMatrix);
         RectangleF rectangleF = new RectangleF()
         {
             X      = 0,
             Y      = bitmap.Size.Height,
             Width  = bitmap.Size.Width,
             Height = (bitmap.Size.Height / 10),
         };
         bitmap = (Bitmap)QRCodeCommon.QRCodeAddContent(bitmap, content, rectangleF);
         if (bitmap == null)
         {
             throw new Exception("生成二维码失败");
         }
         else
         {
             return(bitmap);
         }
     }
     catch (Exception)
     {
         throw;
     }
 }