c# - 如何旋转矩形C#中的文本

标签 c# zxing qr-code

<分区>

我制作了一个应用程序,可以在 PNG 图像中生成 QR 码,并打印出 QR 图像中的文本,但现在我需要将该文本旋转 90 度,但我找不到执行此操作的方法。 ..我认为必须旋转矩形,因为文本位于该矩形内。

例子: enter image description here

代码:

namespace QR_Code_with_WFA
{
    public void CreateQRImage(string inputData)
    {
        if (inputData.Trim() == String.Empty)
        {
            System.Windows.Forms.MessageBox.Show("Data must not be empty.");
        }

        BarcodeWriter qrcoder = new ZXing.BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.QrCode.QrCodeEncodingOptions
            {
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
                Height = 250,
                Width = 250
            }
        };

        string tempFileName = System.IO.Path.GetTempPath() + inputData + ".png";

        Image image;
        String data = inputData;
        var result = qrcoder.Write(inputData);
        image = new Bitmap(result);
        image.Save(tempFileName);

        System.Diagnostics.Process.Start(tempFileName);

    var result2 = qrcoder.Write(inputData);

    int textWidth = 200, textHeight = 20;
    // creating new bitmap having imcreased width
    var img = new Bitmap(result2.Width + textWidth, result2.Height);

    using (var g = Graphics.FromImage(img))
    using (var font = new Font(FontFamily.GenericMonospace, 12))
    using (var brush = new SolidBrush(Color.Black))
    using (var bgBrush = new SolidBrush(Color.White))
    using (var format = new StringFormat() { Alignment = StringAlignment.Near })
    {
            // filling background with white color
            g.FillRectangle(bgBrush, 0, 0, img.Width, img.Height);
            // drawing your generated image over new one
            g.DrawImage(result, new Point(0,0));
            // drawing text
            g.DrawString(inputData, font, brush,  result2.Width, (result2.Height - textHeight) / 2, format);
    }

    img.Save(tempFileName);
    }
}

最佳答案

您需要申请 RotateTransform在绘制文本之前在 Graphics 对象上:

// Change alignment to center so you don't have to do the math yourself :)
using (var format = new StringFormat() { Alignment = StringAlignment.Center })
{
   ...
   // Translate to the point where you want the text
   g.TranslateTransform(result2.Width, result2.Height / 2);
   // Rotation happens around that point
   g.RotateTransform(-90);
   // Note that we draw on [0, 0] because we translated our coordinates already
   g.DrawString(inputData, font, brush, 0, 0, format);
   // When done, reset the transform
   g.ResetTransform();
}

关于c# - 如何旋转矩形C#中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30028647/

相关文章:

c# - 在 LINQ to SQL 中查找或创建对象的通用方法?

java - 谷歌的 zxing(斑马线)条形码库的 BitMatrix 不在它应该在的地方

Python - QRCode 错误 "No module named ' 图像'"

c# - 检测二维码然后旋转文档

javascript - 如何在 Three.js 中的网格上显示二维码?

c# - 如何重新设计 ReSharper 8 生成的代码的样式

c# - 具有公共(public)字段的不同类之间的 IEnumerable.Except()

javascript - Ajax 未附加到我的下拉列表(C# MVC)

camera - 如何制作UWP手机二维码阅读器?

ios - 停止 ZxingObjC 中的连续扫描