使用Windows Form中的插件生成的模糊和大图像条形码 [英] Blurry and large image barcode being generated using a plugin in Windows Form

查看:128
本文介绍了使用Windows Form中的插件生成的模糊和大图像条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照上述步骤进行操作此处 并使用IDAutomationCode39,我获得了条形码图像,但是它们非常模糊,只能扫描更大尺寸的图像.我的条形码ID最长为30个字符,这导致条形码图像非常宽.问题出在哪里?是IDAutomationCode39还是我下面的按钮单击事件中的设置?

private void button1_Click(object sender, EventArgs e)
    {
        string abbre = GenerateProdCodeFromProductName();
        txt2.Text = abbre;

        string barcode = txt1.Text;

        Bitmap bitm = new Bitmap(barcode.Length * 45, 160);
        bitm.SetResolution(240, 240);
        using (Graphics graphic = Graphics.FromImage(bitm))
        {
            Font newfont = new Font("IDAutomationHC39M", 6);
            PointF point = new PointF(5f, 5f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
            graphic.DrawString("*" + barcode + "*", newfont, black, point);
        }

        using (MemoryStream Mmst = new MemoryStream())
        {
            bitm.Save("ms", ImageFormat.Jpeg);
            pictureBox1.Image = bitm;
            pictureBox1.Width = bitm.Width;
            pictureBox1.Height = bitm.Height;
        }
    }

谢谢.

解决方案

我在您的代码中看不到任何插件引用,您只是使用Code39 ASCII字体在位图上打印条形码.

我看到的问题是生成的位图的大小未缩放.
我的意思是,您让条形码的大小决定最终图形图像的大小.
通常是相反的.由于布局限制,您为位图定义了一些尺寸:例如,您必须将条形码打印到标签上.
如果生成的条形码比其容器宽,则必须对其进行归一化(缩放以适合它).

这是我的建议.位图的大小(如果固定)(300、150).生成条形码时,其大小将缩放以适合位图大小.
在缩小的情况下,使用双三次插值法可以保留图像的质量.生成的图形也经过了抗锯齿处理.
(Anti-Alias具有良好的视觉效果.可能不是打印的最佳选择.它还取决于打印机.)

然后,将生成的位图传递到PictureBox以进行可视化,并以PNG图像(由于其无损压缩的方式而保存到磁盘)的形式保存到磁盘.

结果是:


string Barcode = "*8457QK3P9*";

using (Bitmap bitmap = new Bitmap(300, 150))
{
    bitmap.SetResolution(240, 240);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Font font = new Font("IDAutomationSHC39M", 10, FontStyle.Regular, GraphicsUnit.Point);

        graphics.Clear(Color.White);
        StringFormat stringformat = new StringFormat(StringFormatFlags.NoWrap);
        graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        graphics.TextContrast = 10;

        PointF TextPosition = new PointF(10F, 10F);
        SizeF TextSize = graphics.MeasureString(Barcode, font, TextPosition, stringformat);

        if (TextSize.Width > bitmap.Width)
        {
            float ScaleFactor = (bitmap.Width - (TextPosition.X / 2)) / TextSize.Width;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.ScaleTransform(ScaleFactor, ScaleFactor);
        }

        graphics.DrawString(Barcode, font, new SolidBrush(Color.Black), TextPosition, StringFormat.GenericTypographic);

        bitmap.Save(@"[SomePath]\[SomeName].png", ImageFormat.Png);
        this.pictureBox1.Image = (Bitmap)bitmap.Clone();
        font.Dispose();
    }
}

Going through the steps mentioned here and using IDAutomationCode39, I am getting the barcode image, however they are very blurr and only scans bigger size images. My barcode id will be upto 30 characters long, which is causing a very wide barcode image. Where could the problem lie? Is it the IDAutomationCode39 or my setting in my button click event below?

private void button1_Click(object sender, EventArgs e)
    {
        string abbre = GenerateProdCodeFromProductName();
        txt2.Text = abbre;

        string barcode = txt1.Text;

        Bitmap bitm = new Bitmap(barcode.Length * 45, 160);
        bitm.SetResolution(240, 240);
        using (Graphics graphic = Graphics.FromImage(bitm))
        {
            Font newfont = new Font("IDAutomationHC39M", 6);
            PointF point = new PointF(5f, 5f);
            SolidBrush black = new SolidBrush(Color.Black);
            SolidBrush white = new SolidBrush(Color.White);
            graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
            graphic.DrawString("*" + barcode + "*", newfont, black, point);
        }

        using (MemoryStream Mmst = new MemoryStream())
        {
            bitm.Save("ms", ImageFormat.Jpeg);
            pictureBox1.Image = bitm;
            pictureBox1.Width = bitm.Width;
            pictureBox1.Height = bitm.Height;
        }
    }

Thank you.

解决方案

I don't see any PlugIn reference in your code, you are just using a Code39 ASCII font to print a Barcode on a Bitmap.

The problem I see is that the size of the resulting Bitmap is unscaled.
What I mean is, you let the size of the Barcode determine the size of the final graphic image.
It is usually the opposite. You have some defined dimensions for a Bitmap, because of layout constraints: you have to print the barcode to a label, for example.
If the generated Barcode is wider than its container, you have to normalize it (scale it to fit).

Here is what I propose. The size of the Bitmap if fixed (300, 150). When the Barcode is generated, its size is scaled to fit the Bitmap size.
The quality of the image is preserved, using an Bicubic Interpolation in case of down scaling. The resulting graphics is also Anti-Aliased.
(Anti-Alias has a good visual render. May not be the best choice for printing. It also depends on the printer.)

The generated Bitmap is then passed to a PictureBox for visualization and saved to disk as a PNG image (this format because of its loss-less compression).

Here is the result:


string Barcode = "*8457QK3P9*";

using (Bitmap bitmap = new Bitmap(300, 150))
{
    bitmap.SetResolution(240, 240);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Font font = new Font("IDAutomationSHC39M", 10, FontStyle.Regular, GraphicsUnit.Point);

        graphics.Clear(Color.White);
        StringFormat stringformat = new StringFormat(StringFormatFlags.NoWrap);
        graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        graphics.TextContrast = 10;

        PointF TextPosition = new PointF(10F, 10F);
        SizeF TextSize = graphics.MeasureString(Barcode, font, TextPosition, stringformat);

        if (TextSize.Width > bitmap.Width)
        {
            float ScaleFactor = (bitmap.Width - (TextPosition.X / 2)) / TextSize.Width;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.ScaleTransform(ScaleFactor, ScaleFactor);
        }

        graphics.DrawString(Barcode, font, new SolidBrush(Color.Black), TextPosition, StringFormat.GenericTypographic);

        bitmap.Save(@"[SomePath]\[SomeName].png", ImageFormat.Png);
        this.pictureBox1.Image = (Bitmap)bitmap.Clone();
        font.Dispose();
    }
}

这篇关于使用Windows Form中的插件生成的模糊和大图像条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆