是否可以将图像存储到pdf417条形码中?

时间:2014-03-31 17:06:23

标签: c# .net barcode pdf417

是否可以将图像/图片存储(编码)到pdf417条形码中?若有,那么有任何教程或示例代码吗?

条形码不能只保存对数据库中图像的引用。客户还希望能够存储他想要的任何图像。

谢谢。

2 个答案:

答案 0 :(得分:4)

正如ssasa所提到的,您可以将图像存储为byte数组:

public static byte[] GetBytes(Image image)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        // you may want to choose another image format than PNG
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

...或者,如果它必须是string,你可以对它进行base64编码:

public static string GetBase64(Image image)
{
    Image yourImage;

    // using the function from the first example
    var imageBytes = GetBytes(yourImage);   

    var encodedString = Convert.ToBase64String(imageBytes);

    return Encoding.UTF8.GetBytes(encodedString);
}

请记住: PDF417条形码最多可存储2710个字符。虽然这对于您想要编码的大多数结构来说已经足够了,但它对图像的限制相当大。对于小尺寸位图,单色图像和/或高度压缩的JPEG可能就足够了,但是不要指望能够做更多的事情,特别是如果你想能够存储其他数据的话。

如果您的客户希望能够存储他们想要的任何图片,那么您最好在编写任何代码之前尽快降低他们的期望。

如果是一个选项,您可能需要考虑使用QR Codes。并不是说你会创造奇迹,但你可能会喜欢增加的存储容量。

答案 1 :(得分:0)

是的,美国国防部Common Access Cards (CAC)存储持卡人的JPEG图像:

enter image description here