我正在使用C#处理Windows窗体应用程序,而我正在通过Windows窗体应用程序运行PDF。早些时候,我正在使用MS SQL Server(它工作正常)但现在从XML文件中获取数据并将文件从字节转换为PDF文件(pdf格式)。这是我抛出错误的方法:
public static byte[] GetUnCompressedData(byte[] value)
{
try
{
if (value != null)
using (var zipInputStream = new ZipInputStream(new MemoryStream(value)))
{
while ((zipInputStream.GetNextEntry()) != null)
{
using (var zippedInMemoryStream = new MemoryStream())
{
var data = new byte[2048];
while (true)
{
var size = zipInputStream.Read(data, 0, data.Length);
if (size <= 0)
break;
zippedInMemoryStream.Write(data, 0, size);
/// new code added
///
zippedInMemoryStream.Flush();
}
//zippedInMemoryStream.Close();
GC.WaitForPendingFinalizers();
GC.Collect();
return zippedInMemoryStream.ToArray();
}
}
}
return null;
}
catch (Exception)
{
throw;
}
}
这是抛出错误的上述方法的一行:
using (var zippedInMemoryStream = new MemoryStream())
我在Windows 8/7中遇到以下错误:
file does not begin with '%pdf%-'
在调试方法时,我还得到一个错误:
Wrong Local header signature: 0x41430A02
工作环境:Visual Studio 2013,.Net Framework 4.5,Acrobat PDF组件,XML 第三方控制:使用csharp代码使用http://icsharpcode.github.io/SharpZipLib/解压缩/压缩数据
我还提供了使用以下方法压缩数据的功能:
public static byte[] GetCompressedData(string fileName, byte[] value)
{
try
{
// Code for zip file.
if (value != null && !string.IsNullOrEmpty(fileName))
using (var zippedMemoryStream = new MemoryStream())
{
// A ZIP stream
using (var zipOutputStream = new ZipOutputStream(zippedMemoryStream))
{
// Highest compression rating 0 - 9.
zipOutputStream.SetLevel(9);
var entry = new ZipEntry(fileName) { DateTime = DateTime.Now };
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(value, 0, ConvertToInt32(value.Length));
zipOutputStream.Finish();
zipOutputStream.Close();
return zippedMemoryStream.ToArray();
}
}
}
catch (Exception)
{
throw;
}
return null;
}
答案 0 :(得分:0)
我最后这个问题只是将字节 FromBase64String 转换为以下内容。
byte[] _imgdata = System.Text.Encoding.UTF8.GetBytes(_obj.FileData);
_imgdata = Convert.FromBase64String(_obj.FileData);
_pic = clsCommon.ByteToImage(_imgdata);
添加此方法也可以从字节转换为图像
public static Bitmap ByteToImage(byte[] blob)
{
try
{
/// Write memory stream
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
_Height = Convert.ToInt32(bm.Height);
_Width = Convert.ToInt32(bm.Width);
mStream.Dispose();
return bm;
}
catch
{
throw;
}
}
两天的头痛结束了以上两行。