Print two images on a single print process

HARISH JHAMNANI 21 Reputation points
2021-05-12T07:47:41.417+00:00

Hello ,
I have two image files and I want to print them on same page.

so far I had only one image so this code works for me

        Process p = new Process();  
        p.StartInfo.FileName = pictureBox1.ImageLocation;  
        p.StartInfo.Verb = "Print";  
        p.Start();    

On trying the same for two image files it shows me two separate print windows.
but I need images in one print window.
like this 95927-image.jpg
using this code for two images

            FileInfo f1 = new FileInfo(textBox_ImgPath_1.Text);  
            FileInfo f2 = new FileInfo(textBox_ImgPath_2.Text);  
            FileInfo[] Files_2 = new FileInfo[] { f1, f2 };          
             foreach (FileInfo file in Files_2)  
             {  
                 var p = new Process();  
                 p.StartInfo.FileName = file.FullName;  
                 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  
                 p.StartInfo.CreateNoWindow = true;  
                 p.StartInfo.Verb = "Print";  
                 p.Start();  
             }   
Developer technologies Windows Forms
Windows for business Windows Server User experience Print jobs
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-05-12T09:54:01.227+00:00

    If there are no other solutions, you can combine the images, making one. Then print the made image; something like this:

    const int Space = 30;
    
    string path = Path.Combine( Path.GetTempPath( ), "MyImage.png" );
    
    using( Image img1 = Image.FromFile( f1.FullName ), img2 = Image.FromFile( f2.FullName ) )
    {
        using( Bitmap b = new Bitmap( Math.Max( img1.Width, img2.Width ), img1.Height + Space + img2.Height, PixelFormat.Format32bppArgb ) )
        {
            using( Graphics g = Graphics.FromImage( b ) )
            {
                g.Clear( Color.Transparent );
    
                g.DrawImage( img1, new Rectangle( new Point( 0, 0 ), img1.Size ) );
                g.DrawImage( img2, new Rectangle( new Point( 0, img1.Height + Space ), img2.Size ) );
            }
    
            b.Save( path, ImageFormat.Png );
        }
    }
    
    var p = new Process( );
    p.StartInfo.FileName = path;
    . . .
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-05-14T02:29:39.867+00:00

    Hi HARISHJHAMNANI-4000,
    You can draw two images in one call to PrintPageEventArgs event.
    Please refer to the following code.

    List<Image> imageList = new List<Image>();  
    private void Form1_Load(object sender, EventArgs e)  
    {  
        var imageOne = Image.FromFile(@"C:\Users\Pictures\Saved Pictures\football.jpg");  
        var imagetwo = Image.FromFile(@"C:\Users\Pictures\Saved Pictures\motorcycle.jpg");  
        imageList.Add(imageOne);  
        imageList.Add(imagetwo);  
    }  
    int currentPage;  
    public void PrintGraph()  
    {  
        PrintDocument pd = new PrintDocument();  
      
        pd.PrintPage += new PrintPageEventHandler(this.PrintImageHandler);  
      
        PrintDialog MyPrintDialog = new PrintDialog();  
      
        if (MyPrintDialog.ShowDialog() == DialogResult.OK)  
        {  
            currentPage = 0;  
            pd.Print();  
        }  
        pd.Dispose();  
    }  
    private void PrintImageHandler(object sender, PrintPageEventArgs ppeArgs)  
    {  
        Graphics g = ppeArgs.Graphics;  
        Image objimage = imageList[currentPage];  
        g.DrawImage(objimage, 0, 0, objimage.Width, objimage.Height);  
        currentPage++;  
        if (currentPage < imageList.Count)  
        {  
            objimage = imageList[currentPage];  
            g.DrawImage(objimage, 0, 600, objimage.Width, objimage.Height);  
            currentPage++;  
        }  
        ppeArgs.HasMorePages = currentPage < imageList.Count;  
    }  
             
    private void button1_Click(object sender, EventArgs e)  
    {  
      
        PrintGraph();  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. cheong00 3,486 Reputation points Volunteer Moderator
    2021-05-14T03:10:25.627+00:00

    In UWP, you can get that option in the print preview window with StandardPrintTaskOptions.NUp Property, however it will only be shown if the printer selected supports it.

    Also took some time to look how to print NUp with SerPrinter() API. It seems even if the printer supports it, you'll have to convert the pages to be printed as EMF and submit to the printer. It'll be easier to use the above approaches to merge the images yourself and directly print it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.