检索解码二进制图像从SQL和插入到PDF通过iTextsharp asp.net c#
本文关键字:通过 PDF iTextsharp asp net 插入 二进制 解码 图像 SQL 检索 | 更新日期: 2023-09-27 18:17:13
我已经将二进制编码图像存储到我的SQL数据库中。这个二进制编码图像有另一个名为memberreporttimage1的文件,用于解码二进制图像。我能够在我的网页上显示出图像。然而,每当我试图从数据库中获取图像时,它们将显示system.byte(),因为它直接从数据库中检索并且尚未解码。因此,我想知道是否有任何方法来检索解码图像。
我试过这个方法,但最终没有成功
table.AddCell(Image1.Attributes["src"] = MemberReportImage1.aspx?);
这是我的解码文件
protected void Page_Load(object sender, EventArgs e)
{
string strQuery = "select image1 from MemberReport where memberreportid='" + Session["memberreportid"] + "'";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
if (dt != null)
{
download(dt);
}
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
private void download(DataTable dt)
{
Byte[] bytes = (Byte[])dt.Rows[0]["image1"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
//Response.ContentType = dt.Rows[0]["contenttype"].ToString();
//Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
这是我如何从memberreporttimage1解码文件中检索解码图像
if (!DDLCase.SelectedValue.Equals("-1"))
{
Session["memberreportid"] = DDLCase.SelectedValue;
Image1.Attributes["src"] = "MemberReportImage1.aspx?";
Image1.Attributes["height"] = "300";
Image1.Attributes["width"] = "300";
Image2.Attributes["src"] = "MemberReportImage2.aspx?";
Image2.Attributes["height"] = "300";
Image2.Attributes["width"] = "300";
Image3.Attributes["src"] = "MemberReportImage3.aspx?";
Image3.Attributes["height"] = "300";
Image3.Attributes["width"] = "300";
Image4.Attributes["src"] = "MemberReportImage4.aspx?";
Image4.Attributes["height"] = "300";
Image4.Attributes["width"] = "300";
Image5.Attributes["src"] = "MemberReportImage5.aspx?";
Image5.Attributes["height"] = "300";
Image5.Attributes["width"] = "300";
}
这是我的PDF按钮,我试图从memberreporttimage1文件
获得解码图像 protected void btnPDF_Click(object sender, EventArgs e)
{
var doc1 = new Document();
var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
var output = new FileStream(Path.Combine("C:''Users''apr12mpsip''Desktop''New folder", filename), FileMode.Create);
PdfWriter.GetInstance(doc1, output);
doc1.Open();
PdfPTable table = new PdfPTable(2);
table.TotalWidth = 585f;
table.LockedWidth = true;
var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
doc1.Add(logo);
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
doc1.Add(new Paragraph("Official Report. Member Report ID : " + DDLCase.SelectedValue, titleFont));
SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");
SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid, pr.prdatetime, pr.policereport, mr.citizenreport, aor.officialreport from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro, AdminOfficialReport aor where mr.memberreportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and pr.policereportid=aor.policereportid", con);
con.Open();
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
table.AddCell("Full Name:'r'r" + dr[0].ToString());
table.AddCell("Contact :'r'r " + dr[1].ToString());
table.AddCell("Type Of Crime :'r'r " + dr[2].ToString());
table.AddCell("Location :'r'r " + dr[3].ToString());
table.AddCell(Image1.Attributes["src"] = MemberReportImage1.aspx?);
table.AddCell("PoliceID :'r'r " + dr[5].ToString());
table.AddCell("Police Report Date Time :'r'r " + dr[6].ToString());
table.AddCell("Police Report :'r'r " + dr[7].ToString());
table.AddCell("Citizen Report :'r'r" + dr[8].ToString());
table.AddCell("Admin Official Report :'r'r" + dr[9].ToString());
}
dr.Close();
doc1.Add(table);
doc1.Close();
btnPDF.Enabled = false;
}
}
更新
在答案的帮助下,我在下面的链接中设法弄清楚了这个问题检索二进制图像数据从SQL Server到pdf通过iTextSharp ASP。净http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
请看上面的链接
我假设你保存二进制数据的图像文件在你的sql数据库。
如果是,您不需要解码图像。你可以使用image . fromstream()来加载图像数据