简体   繁体   English

Windows窗体中的Web浏览器控件未显示图像

[英]Web Browser control in windows forms not showing image

I am developing an application that registers a student and then prints out an identity card. 我正在开发一个注册学生的应用程序,然后打印出身份证。

On my main form, I have an image Picture box control in which the user loads the image of the student.This image box stores the image in the hard drive through the following code: 在我的主窗体上,我有一个图像图片框控件,用户可以在其中加载学生图像。此图像框通过以下代码将图像存储在硬盘中:

string result;

        DialogResult res = ofdDialog1.ShowDialog();
        if (res == DialogResult.OK)
        {
            result = ofdDialog1.FileName;
            pbImage.ImageLocation = result;
            pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
            pbImage.Load();
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(pbImage.Image, pbImage.Height, pbImage.Width);
            img.Save(Path.GetFullPath("Images/1.jpg"));
        }

To print the i-card, I am using an web browser control to first preview it to the user. 要打印i卡,我正在使用Web浏览器控件先将其预览给用户。 I have an html code in which I want to show the details of the student with their pictures, and some logos. 我有一个html代码,我想在其中显示学生的详细信息以及他们的图片和一些徽标。 Everything else is working fine, but the student image is not getting displayed in the web browser control. 其他所有工作均正常,但学生图像未显示在Web浏览器控件中。 I am using the following code: 我正在使用以下代码:

contents = contents.Replace("[StudentImage]",Path.GetFullPath("Images/1.jpg"));

contents variable contains the HTML, content变量包含HTML,

Following is the part of the html that should show the image of student: 以下是html中应显示学生图像的部分:

<div style=""float:right; width:100px;height:95px;"">
<img src=""[StudentImage]"" style=""width:85px;height:85px;"" />
</div>

What am i doing wrong when all other images are showing perfectly with this approach except for this? 当除此以外的所有其他图像都完美显示时,我在做什么错?

You can use this method to submit your HTML content to the webbrowser 您可以使用此方法将HTML内容提交到Web浏览器

 private void DisplayHtml(string html)
 {
     webBrowser1.Navigate("about:blank");
     if (webBrowser1.Document != null)
     {
        webBrowser1.Document.Write(string.Empty);
     }
      webBrowser1.DocumentText = html;
  }

The use line 使用线

DisplayHtml(contents);

You might need to use an absolute path for the image. 您可能需要为图像使用绝对路径。 I'm not sure what the WebBrowser's "current directory" would be. 我不确定WebBrowser的“当前目录”是什么。

i found the solution, instead of 我找到了解决方案,而不是

img.Save(Path.GetFullPath("Images/1.jpg"));`

i used 我用了

img.Save(Path.GetFullPath("Images/1.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

The image is now displaying correctly. 图像现在可以正确显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM