Winforms 从memorystream呈现pdf
我一整天都在寻找解决办法,但不知怎么的,我找不到。 我想要的是将数据从数据库(如PDF之类的二进制文件)获取到MemoryStream(我成功地做到了这一点),但问题是当我希望将该文件预览到WebBrowser控件中时 代码是这样的:Winforms 从memorystream呈现pdf,winforms,pdf,memorystream,Winforms,Pdf,Memorystream,我一整天都在寻找解决办法,但不知怎么的,我找不到。 我想要的是将数据从数据库(如PDF之类的二进制文件)获取到MemoryStream(我成功地做到了这一点),但问题是当我希望将该文件预览到WebBrowser控件中时 代码是这样的: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.L
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MemoryStream memory = new MemoryStream();
//byte[] temp;
private void Form1_Load(object sender, EventArgs e)
{
memory = GetBlobFile();
}
private MemoryStream GetBlobFile()
{
MemoryStream ms = new MemoryStream();
string SQL = "SELECT [SnapshotPDF] FROM [DBtest].[dbo].[SamplePDF] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=database;Initial Catalog=DBtest; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
byte [] result = (byte[])comm.ExecuteScalar();
conn.Close();
if (result != null)
{
ms.Write(result, 0, result.Length);
ms.Position = 0;
}
return ms;
}
private void button1_Click(object sender, EventArgs e)
{
//webBrowser1.DocumentText = "application/pdf";
webBrowser1.DocumentStream = memory;
}
}
}
实际上webBrowser确实是从内存中输出内容,但显然从上面的图片中,它被渲染为文本。。。
如何强制以PDF格式呈现
如果无法使用webBrowser控件,我是否可以在WinForms中使用其他控件从内存预览/呈现PDF。您必须实现异步可插拔协议,例如IClassFactory、IInternetProtocol。。。然后使用CoInternetGetSession注册协议。当IE调用您的实现时,您可以从内存提供图像数据/提供mime类型
这有点乏味,但可行。请参阅上的IInternetProtocol和可插拔协议文档 完成后,我发现了一个小错误,我纠正了它。这是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Ghostscript.NET;
using Ghostscript.NET.Viewer;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
Stream stream;
byte[] result;
private GhostscriptViewer _viewer;
private GhostscriptVersionInfo _gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion();
private Bitmap _pdfPage = null;
public Form1()
{
InitializeComponent();
pictureBox1.Width = 100;
pictureBox1.Height = 100;
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayUpdate += new GhostscriptViewerViewEventHandler(_viewer_DisplayUpdate);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
}
private void Form1_Load(object sender, EventArgs e)
{
GetBlobFile();
}
private void GetBlobFile()
{
string SQL = "SELECT [SnapshotPDF] FROM [test].[dbo].[InvoiceAdded] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=test;Initial Catalog=test; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
result = (byte[])comm.ExecuteScalar();
conn.Close();
stream = new MemoryStream(result);
}
private void button1_Click(object sender, EventArgs e)
{
ConvertToBitmap();
}
private void ConvertToBitmap()
{
_viewer.Open(stream, _gsVersion, true);
}
//DisplayPage
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
//DisplaySize - dynamically
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
//DisplayUpdate - automatic update picture
void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
请记住,为此,我需要从Ghostscript.NET添加.dll(一个很好的包装器,由我来自克罗地亚的爱国者制作),还需要从安装Ghostscript解释器
还要感谢用户:sm.abdullah
这实际上为我指明了正确的方向。
干杯谢谢你的回答,但这是一个类似线程的副本,但我没有那么丰富的经验,所以我不知道如何实现它…它在这里实现,我明天将尝试实现,尽管我看到这与ASP有关,所以,自从我使用WinForms以来,我还没有看到连接。谢谢你的链接,但我真的不知道该怎么做。。。是否有其他选项可以用来强制webBrowser呈现memoryStream输出?如果您尝试使用此选项会怎么样?感谢您与大家分享您的解决方案。我还建议使用最新的Ghostscript.NET库,可在以下位置找到: