2017-10-12 9 views
0

WebブラウザでPDFと画像を表示するためにWindowsフォームアプリケーションを作成しようとしています。画像は、以下のコードでWebブラウザの書式に合わせて拡大/縮小されます。しかし、私がpdfを読み込もうとすると、エラーが発生します。WebBrowserで画像を拡大してPDFのエラーを表示するC#

"AddMetadataToDocuments.exeで 'System.NullReferenceException'型の例外が発生しましたが、ユーザコード" "で処理されませんでした。

コード:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    webBrowser1.Document.Body.SetAttribute("scroll", "no"); 
    var img = webBrowser1.Document.GetElementsByTagName("img") 
         .Cast<HtmlElement>().FirstOrDefault(); 
    var w = img.ClientRectangle.Width; 
    var h = img.ClientRectangle.Height; 
    img.Style = string.Format("{0}: 100%", w > h ? "Width" : "Height"); 
} 

は誰でも役立つことを願って!

+0

可能な複製:他の実行スケーリング方法、(PDF)は何もしません[NullReferenceExceptionとは何ですか?どうすれば修正できますか?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – haindl

答えて

0

ファイルはJPGまたはPNGで終わるかどうかを確認する声明、そうならばならば、私はシンプルを使用して、それを修正しました:の

if (allFiles[index].EndsWith("JPG") || allFiles[index].EndsWith("PNG")) 
{ 
    webBrowser1.Document.Body.SetAttribute("scroll", "no"); 
    var img = webBrowser1.Document.GetElementsByTagName("img") 
         .Cast<HtmlElement>().FirstOrDefault(); 
    var w = img.ClientRectangle.Width; 
    var h = img.ClientRectangle.Height; 
    img.Style = string.Format("{0}: 100%", w > h ? "Width" : "Height"); 
}  
関連する問題