以高质量打印

huangapple go评论73阅读模式
英文:

Print in high quality

问题

尝试打印生成的文本和二维码图像,但其质量非常低。

已尝试更改图形设置,但没有成功。在打印后的PDF中附上了文本和图像的外观示例。

private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){
    var xheight = 726;
    var xwidth = 363;
    Bitmap flag = new Bitmap(xwidth, xheight);
    
    Graphics flagGraphics = Graphics.FromImage(flag);
    flag.SetResolution(300, 300);
    
    flagGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    flagGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    flagGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    flagGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

    flagGraphics.Flush();

    System.Drawing.Font font1 = new System.Drawing.Font("Tahoma", 35, FontStyle.Bold, GraphicsUnit.Point);
    System.Drawing.Font fontjob = new System.Drawing.Font("Tahoma", 20, FontStyle.Regular, GraphicsUnit.Point);
 
    Rectangle rectname = new Rectangle(100, 100, xwidth, 100);
   
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    
    flagGraphics.DrawString(user_name, font1, Brushes.Black, rectname, stringFormat) ;
    Rectangle rectnjob = a Rectangle(100, 200, xwidth, 50);

    flagGraphics.DrawString("JOB TITLE", fontjob, Brushes.Black, rectnjob, stringFormat);
    Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
    var qrcodeimg = qrcode.Draw(barcode_token_key, 80);
    flagGraphics.DrawImage(qrcodeimg, 100, 300, 100, 100);

    e.Graphics.DrawImage(flag, 0, 0, xwidth, xheight);
}

使用以下代码进行打印:

var doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();

以高质量打印

英文:

Trying to print generated text and qr code image, but it does in very low quality.

Have tried to change the graphics settings with no chance,
attached an image how the text and the image are appears in PDF after printing.

private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e){
    var xheight = 726;
    var xwidth = 363;
    Bitmap flag = new Bitmap(xwidth, xheight);
    
    Graphics flagGraphics = Graphics.FromImage(flag);
    flag.SetResolution(300, 300);
    
    flagGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    flagGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    flagGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    flagGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

    flagGraphics.Flush();

    System.Drawing.Font font1 = new System.Drawing.Font("Tahoma",35, FontStyle.Bold, GraphicsUnit.Point);
    System.Drawing.Font fontjob = new System.Drawing.Font("Tahoma", 20, FontStyle.Regular, GraphicsUnit.Point);
 
    Rectangle rectname = new Rectangle(100, 100, xwidth, 100);
   
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    
    flagGraphics.DrawString(user_name, font1, Brushes.Black, rectname, stringFormat) ;
    Rectangle rectnjob = new Rectangle(100, 200, xwidth, 50);

    flagGraphics.DrawString("JOB TITLE", fontjob, Brushes.Black, rectnjob , stringFormat);
    Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
    var qrcodeimg = qrcode.Draw(barcode_token_key, 80);
    flagGraphics.DrawImage(qrcodeimg, 100, 300, 100, 100);

    e.Graphics.DrawImage(flag, 0, 0, xwidth, xheight);
}

print with:

var doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();

以高质量打印

答案1

得分: 1

如果我理解你的代码正确的话,似乎存在一些需要插值的调整,这可能导致模糊。特别是,保持QR码的生成大小,要么稍小(91x91)缩放为2,要么稍大(136x136)缩放为3,会使渲染更清晰。

我尝试使用你的代码复制了你的问题,并将其与直接绘制到 e.Graphics 进行比较,几乎不更改图形设置。对我来说,这看起来是一个改进,但最终由你决定。

以高质量打印

private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
    string barcode_token_key = "966819cb72ad75435b87156dc82a9d48";
    SizeF size;
    float x = 100, yCurrent = 130;
    Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
    var qrcodeimg = qrcode.Draw(barcode_token_key, 0, scale: 3);
    using (var font = new Font("Tahoma", 35, FontStyle.Bold, GraphicsUnit.Point))
    {
        size = e.Graphics.MeasureString("Entrepreneur", font);
        e.Graphics.DrawString(
            s: "Entrepreneur",
            font: font,
            brush: Brushes.Black,
            new RectangleF(
                new PointF(x: x, y: yCurrent),
                new SizeF(width: size.Width, size.Height)),
                format: new StringFormat { Alignment = StringAlignment.Center });
        yCurrent += size.Height + 10;
    }
    using (var font = new Font("Tahoma", 20, FontStyle.Regular, GraphicsUnit.Point))
    {
        e.Graphics.DrawString(
            s: "JOB TITLE",
            font: font,
            brush: Brushes.Black,
            new RectangleF(
                new PointF(x: x, y: yCurrent),
                new SizeF(width: size.Width, size.Height)),
                format: new StringFormat { Alignment = StringAlignment.Center });
        yCurrent += size.Height + 10;
    }
    e.Graphics.DrawImage(
        qrcodeimg,
        x: x,
        y: yCurrent,
        width: qrcodeimg.Width,
        height: qrcodeimg.Height);
}

备选

字体1大小 = 10,字体2大小 = 7,高度增量 = 4

以高质量打印

英文:

If I'm understanding your code, there seems to be some resizing going on that requires interpolation and may be contributing to the fuzziness. In particular, allowing the QR code to keep its generated size, either slightly smaller (91x91) at a scale of 2 or slightly larger (136x136) at a scale of 3, makes the rendering clearer.

I tried to reproduce your issue with a print preview using your code and then compared it to drawing straight into e.Graphics while pretty much leaving the graphics settings alone. This looks like an improvement to me but you be the judge.

以高质量打印

private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
    string barcode_token_key = "966819cb72ad75435b87156dc82a9d48";
    SizeF size;
    float x = 100, yCurrent = 130;
    Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
    var qrcodeimg = qrcode.Draw(barcode_token_key, 0, scale: 3);
    using (var font = new Font("Tahoma", 35, FontStyle.Bold, GraphicsUnit.Point))
    {
        size = e.Graphics.MeasureString("Entrepreneur", font);
        e.Graphics.DrawString(
            s: "Entrepreneur",
            font: font,
            brush: Brushes.Black,
            new RectangleF(
                new PointF(x: x, y: yCurrent),
                new SizeF(width: size.Width, size.Height)),
                format: new StringFormat { Alignment = StringAlignment.Center });
        yCurrent += size.Height + 10;
    }
    using (var font = new Font("Tahoma", 20, FontStyle.Regular, GraphicsUnit.Point))
    {
        e.Graphics.DrawString(
            s: "JOB TITLE",
            font: font,
            brush: Brushes.Black,
            new RectangleF(
                new PointF(x: x, y: yCurrent),
                new SizeF(width: size.Width, size.Height)),
                format: new StringFormat { Alignment = StringAlignment.Center });
        yCurrent += size.Height + 10;
    }
    e.Graphics.DrawImage(
        qrcodeimg,
        x: x,
        y: yCurrent,
        width: qrcodeimg.Width,
        height: qrcodeimg.Height);
}

Alternate

Font 1 size = 10, Font 2 size = 7, height incr = 4

以高质量打印

huangapple
  • 本文由 发表于 2023年2月27日 10:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定