问题
I have store a binary coded image into my SQL database. This binary coded image has another file named MemberReportImage1 which is there to decode the binary image. I'm able to display out the image on my webapp page. However, whenever i attempt to get the image from the database they will display system.byte() as it is directly retrieve from the database and has not been decoded. Hence i'm wondering if there is any way to retrieve the decoded image.
I have tried this method which eventually didnt work out
table.AddCell(Image1.Attributes["src"] = MemberReportImage1.aspx?);
This is my decoding file
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();
}
This is how i retrieve the decoded image from the MemberReportImage1 decoding file
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";
}
This is my PDF button where i attempt to get the decoded image from the MemberReportImage1 file
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;
}
}
UPDATE
I managed to figure this out in the link below with the help of the answers Retrieve binary image data from SQL Server into pdf via iTextSharp ASP.NET
回答1:
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
Please look at above link.
I assume you save binary data of image files in your sql database.
if yes, You don't need to get decoded image. You can use Image.FromStream() to load image data.
来源:https://stackoverflow.com/questions/16952561/retrieve-decoded-binary-image-from-sql-and-insert-into-pdf-via-itextsharp-asp-ne