Skip to content
Loading
Select image from db ms sql c#
0
  • Jenith Thakkar
    Hi Goran Bibic Still you facing that issue?
    0
  • Jenith Thakkar
    You can use base64 in this way https://stackoverflow.com/questions/19389999/can-itextsharp-xmlworker-render-embedded-images
    0
  • Rajeesh Menoth
    Hi,
     
    Try the following logic ( Answer by tga) :
    1. connection.Open();  
    2.         SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@param", connection);  
    3.         SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30);  
    4.         myparam.Value = txtimgname.Text;  
    5.         byte[] img = (byte[])command1.ExecuteScalar();  
    6.         MemoryStream str = new MemoryStream();  
    7.         str.Write(img, 0, img.Length);  
    8.         Bitmap bit = new Bitmap(str);  
    9.         connection.Close();  
    Reference :
     
    https://www.youtube.com/watch?v=Ie8lnubw8kk
     
    https://stackoverflow.com/questions/2175209/retrieve-images-from-sql-server-database 
    +2
  • i need directly from db
     
    This work directly from file
     
    1. iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:\\memorandum.jpg");  
    2.   
    3.   
    4. logo.ScaleAbsolute(550, 110);  
    5. logo.SetAbsolutePosition(20, 710);  
    6. pdfDoc.Add(logo); 
     
    I have problem with this
     
    1. string memorandum = "";  
    2. using (SqlConnection openCon = new SqlConnection(con))  
    3. {  
    4. SqlDataReader reader;  
    5. SqlCommand cmd = new SqlCommand("SELECT memorandum from dbo.podaci_o_korisniku");  
    6. cmd.Connection = openCon;  
    7. openCon.Open();  
    8. reader = cmd.ExecuteReader();  
    9. while (reader.Read())  
    10. {  
    11. memorandum = reader["memorandum"].ToString();  
    12. }  
    13. openCon.Close();  
    14. }   
    15.   
    16.  string folderPath = "C:\\PDFs\\";  
    17.  if (!Directory.Exists(folderPath))  
    18. {  
    19. Directory.CreateDirectory(folderPath);  
    20. }  
    21. using (FileStream stream = new FileStream(folderPath + "Podaci o korisniku.pdf", FileMode.Create))  
    22. {  
    23. iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 14);  
    24. iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 10);  
    25. Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);  
    26. PdfWriter.GetInstance(pdfDoc, stream);  
    27. pdfDoc.Open();  
    28. pdfDoc.Add(new ListItem(memorandum));  

    29.  }  
    30.   
    31. }  
    32.   
    33.  } 
     
    0
  • Jenith Thakkar

     
    Need to select image from db and show on pdf document itextsharp
    0
  • Jenith Thakkar
    Show me output and tell me what exactly do you want
    +1
  • Jenith Thakkar

     
    Same result  after update code
     
    System.Byte[]
    0
  • Jenith Thakkar
    if you are storing base64 in a database then no need to convert it in byte 
    string memorandum = "";
    using (SqlConnection openCon = new SqlConnection(con))
    {
    SqlDataReader reader;
    SqlCommand cmd = new SqlCommand("SELECT memorandum from dbo.podaci_o_korisniku");
    cmd.Connection = openCon;
    openCon.Open();
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    memorandum = reader["memorandum"].ToString();
    }
    openCon.Close();
    }
     
    +1