我已将二进制编码图像存储到我的SQL数据库中。该二进制编码图像具有另一个名为MemberReportImage1的文件,该文件用于解码二进制图像。我可以在我的webapp页面上显示图像。但是,每当我尝试从数据库中获取图像时,它们将显示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();
}
这是我从MemberReportImage1解码文件中检索解码图像的方法
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按钮,我尝试从MemberReportImage1文件中获取解码图像
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;
}
}
更新
我在答案的帮助下设法在下面的链接中找到了这个 Retrieve binary image data from SQL Server into pdf via iTextSharp ASP.NET
答案 0 :(得分:0)
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
请看上面的链接。
我假设您在sql数据库中保存了图像文件的二进制数据。
如果是,您不需要获得解码图像。您可以使用Image.FromStream()来加载图像数据。