Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new in C# and I have create a scanner application by C# winform. Now I'm having a problem that when i use treeview for a folder and click in item inside (all items inside are images and pdf file). All image file format display normal in picture box but when i try to display for pdf file it not working, my app just notice that is not a pdf file but the file i click in totally in a pdf file. Does anyone have some advise


What I have tried:

C#
<pre> private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
 {
     if (e.Node != null && e.Node.Tag != null && File.Exists(e.Node.Tag.ToString()))
     {
         string selectedFile = e.Node.Tag.ToString();
         if (File.Exists(selectedFile))
         {
             if (IsImageFile(selectedFile))
             {
                 try
                 {
                     if (IsPdfFile(selectedFile))
                     {
                         if (selectedFile.ToLower().EndsWith(".pdf"))
                         {
                             using (PdfiumViewer.PdfDocument document = PdfiumViewer.PdfDocument.Load(selectedFile))
                             {
                                 int imageIndex = treeView1.SelectedNode.Index;
                                 using (Bitmap image = (Bitmap)document.Render(imageIndex, 300, 300, true))
                                 {
                                     pictureBox1.Image = new Bitmap(image);
                                 }
                             }
                         }
                     }
                     else
                     {
                         pictureBox1.Image = Image.FromFile(selectedFile);
                     }
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Error loading file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
             else
             {
                 MessageBox.Show("Selected file is not an image or PDF.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
         else
         {
             MessageBox.Show("Selected file does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }

 private bool IsImageFile(string fileName)
 {
     string[] validExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
     string extension = Path.GetExtension(fileName);
     return validExtensions.Contains(extension.ToLower());
 }

 private bool IsPdfFile(string fileName)
 {
     string[] pdfExtensions = { ".pdf" };
     string extension = Path.GetExtension(fileName);
     return pdfExtensions.Contains(extension.ToLower());
 }

 private void PopulateTreeView(string directory, TreeNode parentNode)
 {
     if (parentNode == null)
     {
         treeView1.Nodes.Clear();
     }

     DirectoryInfo directoryInfo = new DirectoryInfo(directory);
     TreeNode node = new TreeNode(directoryInfo.Name);
     node.Tag = directoryInfo.FullName;

     node.ImageIndex = 0;
     node.SelectedImageIndex = 0;

     if (parentNode == null)
     {
         treeView1.Nodes.Add(node);
     }
     else
     {
         parentNode.Nodes.Add(node);
     }

     try
     {
         foreach (var dir in directoryInfo.GetDirectories())
         {
             PopulateTreeView(dir.FullName, node);
         }

         foreach (var file in directoryInfo.GetFiles())
         {
             TreeNode fileNode;
             if (IsImageFile(file.FullName))
             {
                 fileNode = new TreeNode(file.Name);

                 fileNode.ImageIndex = 1;
                 fileNode.SelectedImageIndex = 1;
             }
             else if (IsPdfFile(file.FullName))
             {
                 fileNode = new TreeNode(file.Name);
                 fileNode.ImageIndex = 2;
                 fileNode.SelectedImageIndex = 2;
             }
             else
             {
                 continue;
             }
             fileNode.Tag = file.FullName;
             node.Nodes.Add(fileNode);
         }
     }
     catch (UnauthorizedAccessException)
     {
         node.Nodes.Add("Access denied");
     }
     catch (Exception ex)
     {
         node.Nodes.Add("Error: " + ex.Message);
     }
 }
Posted

1 solution

So, what is it that you want to display as an image? The thumbnail that you would see in Windows Explorer? A detailed, scrollable view of the PDF? I'm going to assume that you want the user to actually see the PDF itself here. If this is the case, at runtime, hide the image viewer and use the techniques in PDF Viewer Control Without Acrobat Reader Installed[^] (you'll have to translate from VB.NET).
 
Share this answer
 
Comments
Maciej Los 10-Apr-24 16:29pm    
5ed!
Pete O'Hanlon 11-Apr-24 2:06am    
Thank you Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900