我要求从亚马逊运输标签,他们发回一个字符串是一个压缩的“GZIP”文件,我必须将其保存为“.gz”扩展名然后我可以对其进行解压缩(或解压缩),有时是“PNG”格式的字符串,有些是“PDF”格式! 但我希望每次都以“PNG”格式打印,但当他们发回“PDF”格式时,我无法将解压缩(GZIPSTREAM)文件转换为“PNG”! 有没有安装第三方库的帮助?将“PDF”解压缩的GZipStream转换为“PNG”C#
这里是我的代码
public async Task PrintLabel(string imageLabelString)
{
var byteStream = Convert.FromBase64String(imageLabelString);
GZipStream gzip = null;
Image image = null;
using (MemoryStream memoryStream = new MemoryStream(byteStream))
{
image = null;
//File.WriteAllBytes("temp.gz", byteStream);
using (gzip = new GZipStream(memoryStream, CompressionMode.Decompress))
{
image = Image.FromStream(gzip);
};
memoryStream.Position = 0;
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
PrintDocument pd = new PrintDocument();
pd.PrintPage += (object o, PrintPageEventArgs e) =>
{
Rectangle rectangle = new Rectangle(0, 0, 500, 750);
e.Graphics.DrawImage(image, rectangle);
};
pd.PrinterSettings.PrinterName = "PrinterName";
pd.Print();
}
this.Close();
}
这是非平凡的没有第三方库。 * c#pdf有很多匹配图像* –
您可以通过检查前4个字节来检测解码流是否为PNG。在PNG中,它们将包含ASCII中的字符串“PNG”。 –