views:

566

answers:

0

Duplicate of (amongst others)

Please help to generate thumbnail image for pdf in c#.

  //Treeview1_AfterSelect
    private void Treeview1_AfterSelect(System.Object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
        string strNode;
        strNode = ((TreeView)sender).SelectedNode.FullPath;
        ImgPath = strNode;         
        //pictureBox4.Image = null;
        string strnode = e.Node.FullPath.ToString();
        int idx = strnode.IndexOf("\\");



        if (idx > 1)
        {
            // show list of favorites in this folder
            if (Treeview1.SelectedNode == null)
            {
                //m_newFolderCommand.Enabled = False
            }
            else
            {
                //m_newFolderCommand.Enabled = True
                strfinalpath = ((TreeView)sender).SelectedNode.FullPath;
                // Need to check to see if this folder needs subfolder reading...
                if (Treeview1.SelectedNode.Nodes.Count == 1)
                {
                    try
                    {
                        // It does, so remove the placeholder folder.
                        Treeview1.SelectedNode.Nodes.Clear();
                        // Now populate the nodes with the subfolders.
                        AddFolders(Treeview1.SelectedNode, Treeview1.SelectedNode.Tag.ToString());
                    }
                    catch (Exception ex)
                    {
                    }
                }


                if ((Treeview1.SelectedNode.Text != "Loading..."))
                {
                    if (Treeview1.SelectedNode.Text == frmSettings.strDirPath.ToString())
                    {
                        folder = strfinalpath;
                        TreeListViewDragDrop.Search.strfilepath = null;
                        TreeListViewDragDrop.Search.intDeciCnt = 0;

                    }
                    else
                    {
                        if ((Treeview1.SelectedNode.Text != strAppPath))
                        {
                            folder = Treeview1.SelectedNode.Tag.ToString();
                            TreeListViewDragDrop.Search.strfilepath = null;
                            TreeListViewDragDrop.Search.intDeciCnt = 0;

                        }
                    }
                }

                if (ListView1.Items.Count > 0)
                {
                    ListView1.Items.Clear();
                }
                ImageList imageList = new ImageList();
                if (folder != null && System.IO.Directory.Exists(folder))
                {
                    try
                    {
                        string[] arrImageName=new string[1000];
                        int Count = 0;
                        string CutName;
                        DirectoryInfo dir = new DirectoryInfo(@folder);
                        foreach (FileInfo file in dir.GetFiles())
                        {
                            try
                            {
                                string strSplit;
                                strSplit = file.FullName;
                                string[] ClickImg = strSplit.Split('.');
                                string strPDFpath = "C:\\Documents and Settings\\Lenin\\My Documents\\pdf.jpg";

                                if (ClickImg[1] == "pdf") //if (ClickImg[1] == "pptx")
                                {

                                    ShellThumbnail shellThumbnail = new ShellThumbnail();
                                    Bitmap bmp = shellThumbnail.GetThumbnail(@file.FullName);
                                    bmp.Save(@folder +"\\"+ "Test.bmp");

                                    ////imageList.ImageSize = new Size(140, 140);
                                    ////imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    ////Image image = (Image)bit;
                                    //////Image img1 = Image.FromFile(strPDFpath);
                                    ////CutName = file.FullName;
                                    ////CutName = CutName.Replace(folder, "");
                                    ////CutName = CutName.Replace("\\", "");
                                    ////arrImageName[Count] = CutName;
                                    ////imageList.Images.Add(FormatImage(imageList.ImageSize.Width, image));
                                    //////imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    ////Count = Count + 1;




                                    //imageList.ImageSize = new Size(140, 140);
                                    //imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    //Image img1 = Image.FromFile(strPDFpath);
                                    //CutName = file.FullName;
                                    //CutName = CutName.Replace(folder, "");
                                    //CutName = CutName.Replace("\\", "");
                                    //arrImageName[Count] = CutName;
                                    //imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    //Count = Count + 1;                                       

                                }
                                else
                                {
                                    imageList.ImageSize = new Size(140, 140);
                                    imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    Image img1 = Image.FromFile(file.FullName);                                       
                                    CutName = file.FullName;
                                    CutName = CutName.Replace(folder, "");
                                    CutName = CutName.Replace("\\", "");
                                    arrImageName[Count] = CutName;
                                    imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    Count = Count + 1;
                                }

                            }
                            catch
                            {
                               Console.WriteLine("This is not an image or pdf file.");

                            }     
                        }


                        for (int j = 0; j < imageList.Images.Count; j++)
                        {
                            this.ListView1.Items.Add((j + 1) + "/" + imageList.Images.Count + " " + "\r\n" + arrImageName[j]);                                
                            this.ListView1.Items[j].ImageIndex = j;
                        }

                        this.ListView1.View = View.LargeIcon;
                        this.ListView1.LargeImageList = imageList;
                        //import(folder);
                    }
                    catch (Exception ex)
                    {

                    }
                }                
            }
        }
    }

Im able to generate thumbnail for image as below

    private Image FormatImage(int width, Image img)
    {
        Image thumb = new Bitmap(width, width);
        Image tmp = null;            
        if (img.Width < width && img.Height < width)
        {
            using (Graphics g = Graphics.FromImage(thumb))
            {

                int xoffset = (int)((width - img.Width) / 2);
                int yoffset = (int)((width - img.Height) / 2);
                g.DrawImage(img, xoffset, yoffset, img.Width, img.Height);
            }
        }
        else
        {
            Image.GetThumbnailImageAbort myCallback = new
                Image.GetThumbnailImageAbort(ThumbnailCallback);

            if (img.Width == img.Height)
            {
                thumb = img.GetThumbnailImage(
                         width, width,
                         myCallback, IntPtr.Zero);
            }
            else
            {
                int k = 0;
                int xoffset = 0;
                int yoffset = 0;

                if (img.Width < img.Height)
                {
                    k = (int)(width * img.Width / img.Height);
                    tmp = img.GetThumbnailImage(k, width, myCallback, IntPtr.Zero);
                    xoffset = (int)((width - k) / 2);

                }

                if (img.Width > img.Height)
                {
                    k = (int)(width * img.Height / img.Width);
                    tmp = img.GetThumbnailImage(width, k, myCallback, IntPtr.Zero);
                    yoffset = (int)((width - k) / 2);
                }

                using (Graphics g = Graphics.FromImage(thumb))
                {
                    g.DrawImage(tmp, xoffset, yoffset, tmp.Width, tmp.Height);
                   // Pen p1 = new Pen(Color.Red);
                    //g.DrawLine(p1, 0, 30, 40, 50);
                }

            }
        }

        using (Graphics g = Graphics.FromImage(thumb))
        {
            g.DrawRectangle(Pens.Black, 0, 0, thumb.Width - 1, thumb.Height - 1);
        }

        InitializeListView();
        return thumb;
    }

    private void InitializeListView()
    {
        // Set the view to show details.
        ListView1.View = View.Details;

        // Allow the user to edit item text.
        ListView1.LabelEdit = true;

        // Allow the user to rearrange columns.
        ListView1.AllowColumnReorder = true;

        // Select the item and subitems when selection is made.
        ListView1.FullRowSelect = true;
        // Display grid lines.
        ListView1.GridLines= true;
        ListView1.BorderStyle=BorderStyle.FixedSingle; 
        // Sort the items in the list in ascending order.
       // ListView1.Sorting = SortOrder.Ascending;

        // Attach Subitems to the ListView
        ListView1.Columns.Add("Title", 300, HorizontalAlignment.Left);
        ListView1.Columns.Add("ID", 70, HorizontalAlignment.Left);
        ListView1.Columns.Add("Price", 70, HorizontalAlignment.Left);
        ListView1.Columns.Add("Publi-Date", 100, HorizontalAlignment.Left);

        // The ListViewItemSorter property allows you to specify the
        // object that performs the sorting of items in the ListView.
        // You can use the ListViewItemSorter property in combination
        // with the Sort method to perform custom sorting.
        //_lvwItemComparer = new ListViewItemComparer();
        //this.listView1.ListViewItemSorter = _lvwItemComparer;
    } 

    public bool ThumbnailCallback()
    {
        return true;
    }

But im struck up at a point where i have to read the first page of pdf and generate thumbnail image.

 if (ClickImg[1] == "pdf") //if (ClickImg[1] == "pptx")
                                {

                                    ShellThumbnail shellThumbnail = new ShellThumbnail();
                                    Bitmap bmp = shellThumbnail.GetThumbnail(@file.FullName);
                                    bmp.Save(@folder +"\\"+ "Test.bmp");

                                    ////imageList.ImageSize = new Size(140, 140);
                                    ////imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    ////Image image = (Image)bit;
                                    //////Image img1 = Image.FromFile(strPDFpath);
                                    ////CutName = file.FullName;
                                    ////CutName = CutName.Replace(folder, "");
                                    ////CutName = CutName.Replace("\\", "");
                                    ////arrImageName[Count] = CutName;
                                    ////imageList.Images.Add(FormatImage(imageList.ImageSize.Width, image));
                                    //////imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    ////Count = Count + 1;




                                    //imageList.ImageSize = new Size(140, 140);
                                    //imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    //Image img1 = Image.FromFile(strPDFpath);
                                    //CutName = file.FullName;
                                    //CutName = CutName.Replace(folder, "");
                                    //CutName = CutName.Replace("\\", "");
                                    //arrImageName[Count] = CutName;
                                    //imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    //Count = Count + 1;                                       

                                }
                                else
                                {
                                    imageList.ImageSize = new Size(140, 140);
                                    imageList.ColorDepth = ColorDepth.Depth32Bit;
                                    Image img1 = Image.FromFile(file.FullName);                                       
                                    CutName = file.FullName;
                                    CutName = CutName.Replace(folder, "");
                                    CutName = CutName.Replace("\\", "");
                                    arrImageName[Count] = CutName;
                                    imageList.Images.Add(FormatImage(imageList.ImageSize.Width, img1));
                                    Count = Count + 1;
                                }

Any help.Thanks in advance.