Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am using listview with list of images. By click listview row then getting all selected images and want to print those images. I have tried. After click,

C#
PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(PrintPage);
            System.Windows.Forms.PrintDialog pdi = new System.Windows.Forms.PrintDialog();
            pdi.Document = pd;
            if (pdi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {                    
                pd.Print();
            }
            else
            {
                MessageBox.Show("Print Cancelled");
            }


What I have tried:

After this code,

C#
private void PrintPage(object o, PrintPageEventArgs e)
      {
          List<ListViewItemsData> objListViewItemsData = new List<ListViewItemsData>();
          DMSBusinessLayer service = new DMSBusinessLayer();
          List<DocumentsUser> objTble_Documents = new List<DocumentsUser>();
          int UserId = 0;
          string ImgName = string.Empty;
          foreach (DocumentsUser item in listView1.SelectedItems)
          {
              UserId = Convert.ToInt32(item.UserId);
              ImgName = item.Parent_File_Name;
              objTble_Documents = service.PrintUserDocuments(ImgName, UserId).AsEnumerable().Select(m => new DocumentsUser()
              {
                  Child_File_Name = m.Field<string>("Child_File_Name"),
                  FilePath = m.Field<string>("FilePath"),
                  Parent_File_Name = m.Field<string>("Parent_File_Name")
              }).ToList();
          }

          foreach (var i in objTble_Documents)
          {
              objListViewItemsData.Add(new ListViewItemsData()
              {
                  GridViewColumnName_ImageSource = (Convert.ToString(i.FilePath) + Convert.ToString(i.Parent_File_Name) + "_" + Convert.ToString(i.Child_File_Name)),
              });
          }

          foreach (var ii in objListViewItemsData)
          {
              System.Drawing.Image img = System.Drawing.Image.FromFile(ii.GridViewColumnName_ImageSource.ToString());
              System.Drawing.Rectangle m = e.MarginBounds;

              if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height)
              {
                  m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
              }
              else
              {
                  m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
              }
              e.Graphics.DrawImage(img, m);
          }
      }



'objListViewItemsData' list object contain all images and how can I print each image file separately? Please help me...
Posted
Updated 20-Jan-17 9:08am
Comments
Afzaal Ahmad Zeeshan 4-Jan-17 14:04pm    
Get the image separately, then execute Print functionality separately. Otherwise, create a single document with image on each page. Where is your print document object?
Rajesh Kumar 2013 5-Jan-17 13:02pm    
@Afzaal : ii.GridViewColumnName_ImageSource.ToString() is the print document object. How can I get image separately? Could you please explain with respect to above code? I want to print each images print on selepated file. Please

1 solution

Try this

This shouldn't be in your PrintPage method, but rather on a button click.
 Image imgToPrint;
protected void btnPrint_Click(object sender, EventArgs e){
 foreach (var ii in objListViewItemsData)
            {
                System.Drawing.Image img System.Drawing.Image.FromFile(ii.GridViewColumnName_ImageSource.ToString());
       
      imgToPrint = img;
      PrintDocument pd = new PrintDocument();
      pd.OriginAtMargins = true;
      pd.PrintPage += pd_PrintPage;
      pd.DefaultPageSettings.Landscape = true;
      pd.Print();
            }
}


void pd_PrintPage(object sender, PrintPageEventArgs e)
 {
    Point loc = new Point(100, 100);
 e.Graphics.DrawImage(imgToPrint , loc);
 }


Just set your own dimensions and it will print each image as a new page(instance).
 
Share this answer
 
Comments
Rajesh Kumar 2013 20-Jan-17 21:02pm    
@Asgard25 - Thank you Sir. Its working fine. I am facing another problem, I can not print all content from WPF's listview. Could you please help me by answering by this question. https://www.codeproject.com/Questions/1166691/How-can-I-print-all-contents-of-listview-from-user please help me.

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