2014-11-11 3 views
0

하루 종일 솔루션을 검색했지만 어떻게 든 찾을 수 없습니다. 내가 원했던 것은 데이터베이스 (PDF와 같은 바이너리 파일)에서 MemoryStream으로 데이터를 가져 오는 것이었지만, 문제는 웹 브라우저 컨트롤로 그 파일을 미리보고 싶을 때입니다.render memorystream에서 pdf

그래서 코드가 간다 : 메모리에서

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; 
    } 
} 
} 

PDF render

실제로 웹 브라우저 정말 내용을 출력하지만, 분명 사진에서 텍스트로 렌더링 것보다 ... 어떻게 PDF로 렌더링하는 강제 ?

webBrowser 컨트롤을 사용할 수 없다면 WinForms에서 메모리에서 PDF를 미리보기/렌더링하는 데 사용할 수있는 다른 컨트롤이 있습니까?

답변

1

비동기식 플러 거블 프로토콜을 구현해야합니다 (예 : IClassFactory, IInternetProtocol ... 그런 다음 CoInternetGetSession을 사용하여 프로토콜을 등록합니다. IE가 구현을 호출하면 메모리에서 이미지 데이터를 제공하거나 MIME 유형을 제공 할 수 있습니다.

약간 지루하지만 수행 할 수 있습니다. IInternetProtocol 및 플러그 가능 프로토콜 설명서를 MSDN에보십시오.

+0

답장을 보내 주셔서 감사합니다.하지만 이건 비슷한 스레드 중 하나입니다. 경험이 없으므로 구현 방법을 모르겠습니다. – dovla091

+0

http : // www .codeproject.com/Articles/6120/A-Simple-protocol-to-view-aspx-pages-IIS-i없이 –

+0

내일 ASP 구현과 관련이 있음에도 불구하고 구현하려고합니다. 내가 WinForms를 사용하고 있기 때문에 t 연결을 보았습니다. – dovla091

0

완료, 수정 한 버그가 하나 있습니다. 이것은 전체 코드입니다 :

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 interpreter

에서 고스트 스크립트 인터프리터를 설치하는 데 필요한

또한 사용자에게 감사드립니다. sm.abdullah

실제로 올바른 방향으로 연결됩니다. 건배

+0

모두와 솔루션을 공유해 주셔서 감사합니다. 또한 여기에있는 최신 Ghostscript.NET 라이브러리를 사용하는 것이 좋습니다. http://github.com/jhabjan/Ghostscript.NET – HABJAN

관련 문제