【问题标题】:C# How Do I Load an image into a code-behind generated table?C#如何将图像加载到代码隐藏生成的表中?
【发布时间】:2017-05-03 14:17:45
【问题描述】:

我正在使用 asp.net/c# 创建一个将被下载的 PDF 文件。这一切都是在后面的代码中通过创建一系列加载到 div 创建的 HTMLGenericControl 中的 HTMLTable 来完成的。然后将此 div 转换为字符串并传递给生成 PDF 文件的函数。下面是一个代码示例:

//This dataset contains the student's name, age and grade, want it for all sections
List<studentExportPersonal> lsep = sp.getExportPersonal(exportStudents);

//Page Header containg Grade, Student Name, and Age
HtmlGenericControl div = new HtmlGenericControl("div");

foreach (studentExportPersonal sep in lsep)
{
    foreach (string sec in secs)
    {
        HtmlTable identityTable = new HtmlTable();
        div.Controls.Add(identityTable);

        identityTable.Width = "100%";
        identityTable.Border = 0;
        identityTable.Style.Add("margin-bottom", "20px");

        HtmlTableRow identityTR = new HtmlTableRow();
        identityTable.Rows.Add(identityTR);

        HtmlTableCell tdGrade = new HtmlTableCell();
        identityTR.Cells.Add(tdGrade);
        tdGrade.Style.Add("font-size", "30px");
        tdGrade.Align = "center";
        tdGrade.RowSpan = 2;
        tdGrade.InnerText = sep.si.studentGrade;

        HtmlTableCell tdStudentName = new HtmlTableCell();
        identityTR.Cells.Add(tdStudentName);
        tdStudentName.Style.Add("font-size", "30px");
        tdStudentName.Align = "center";
        tdStudentName.InnerText = sep.si.studentName;

        HtmlTableCell tdStudentAge = new HtmlTableCell();
        identityTR.Cells.Add(tdStudentAge);
        tdStudentAge.Style.Add("font-size", "30px");
        tdStudentAge.Align = "center";
        tdStudentAge.RowSpan = 2;
        tdStudentAge.InnerText = sep.si.studentAge + " Years Old";

        HtmlTableRow identityTR1 = new HtmlTableRow();
        identityTable.Rows.Add(identityTR1);

        HtmlTableCell tdPic = new HtmlTableCell();
        identityTR1.Cells.Add(tdPic);

        Bitmap studPic;

        if (HttpContext.Current.Request.IsLocal)
            studPic = (Bitmap)System.Drawing.Image.FromFile(@"C:\studentPics\" + sep.si.studentPicID, true);
        else
            studPic = (Bitmap)System.Drawing.Image.FromFile(@"E:\Website\Cascade\studentPics\" + sep.si.studentPicID, true);
    }
}

export dmc = new export();

var sb = new StringBuilder();
div.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
string s = sb.ToString();

dmc.exportPDF("Student Profiles", "Portrait", s);

在 foreach 循环的底部你可以看到我正在创建一个位图变量,我的问题是,如何将它加载到 HTMLTableCell 变量中?

【问题讨论】:

  • 为什么不使用 CSS 呢?另外,我注意到您正在引用本地文件的硬编码字符串路径,部署应用程序时图像不会出现。
  • 谢谢你...我想这就是我想要弄清楚的东西...但这也是我不使用 CSS 的原因...因为一切都发生在后面的代码,在点击 .aspx 之前处理表格,因此 css 永远没有机会发挥作用......
  • 是每个单元格的唯一图像,还是常用的?如果它是每个单元格的唯一图像,我们可以仔细看看你的逻辑。如果很常见,那么 CSS 非常适合它

标签: c# asp.net image


【解决方案1】:

您可以将图像添加到单元格,就像将单元格添加到行一样。

HtmlImage image = new HtmlImage();
image.Src ="/studentPics/" + sep.si.studentPicID;
tdPic.Controls.Add(image);

但是,当我查看您的代码时,我认为 GridView 会是更好的选择。 您可以将 List 直接绑定到 GridView。

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

if (!Page.IsPostBack)
{
    GridView1.DataSource = lsep;
    GridView1.DataBind();
}

【讨论】:

  • HtmlImage -- 完美!!只是为了提供更多细节,我在一个学校系统工作,这是为了让老师可以给他们班上的学生拍照并打印出他们的个人资料。学生的照片 ID 存储在数据库中,因此在他们选择学生后,我从数据库中读取 ID,因此它是 ID 的动态列表。但是 HtmlImage 工作得很好。再次感谢!!
  • 是图片存放在数据库中,还是图片存放在数据库中的路径,如果是第一个则需要先将图片转base64后才能显示在表格中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2011-04-01
相关资源
最近更新 更多