How can i Print .jpg file by adding text at bottom

Sushil Agarwal 381 Reputation points
2021-04-22T12:12:47.57+00:00

hello experts
datagridview having columns for picture file path and picture related to a number called as bill_key also shown in column
i want to merge at either top or bottom of image bill_key so after print user knows that printed picture belongs to which bill_key

i tried following , but cant see the bill_key any where
can somebody help to get this done.

Thanks in advance.

 foreach (DataGridViewRow dgvrPdiTc in dgvPdiTc.SelectedRows)
            {
                if (dgvrPdiTc.Cells["report"].Value.ToString().Trim().Equals("tc"))
                {
                    //print image from file

                    string billnumber = dgvrPdiTc.Cells["bill_key"].Value.ToString();
                    //string testName = "Microsoft C# Deveoper Practice Test";
                    //string testDate = "Thursday, November 10, 2016";
                    //string score = "Score 33 / 33";
                    Bitmap bitmap = (Bitmap)Image.FromFile(dgvrPdiTc.Cells["tc_path"].Value.ToString());
                    //const int dotsPerInch = 75; // define the quality in DPI
                    //http://stackoverflow.com/questions/11699219/save-an-image-as-a-bitmap-without-losing-quality
                    //bitmap.SetResolution(dotsPerInch, dotsPerInch);
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        //graphics.SmoothingMode = SmoothingMode.AntiAlias; //line A
                        //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //line B
                        //graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; //line C
                        graphics.DrawString(billnumber, new Font("Times-Roman", 0.1f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 350f, 350f);
                        //graphics.DrawString(testName, new Font("Times-Roman", 0.06f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 180f, 420f);
                        //graphics.DrawString(testDate, new Font("Times-Roman", 0.05f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 350f, 490f);
                        //graphics.DrawString(score, new Font("Times-Roman", 0.06f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 350f, 550f);

                        //string fileName = "result3600.PNG";
                        //bitmap.Save(imageBaseFilePath + fileName);
                        using (var pd = new PrintDocument())
                        {
                            pd.PrintPage += (_, e1) =>
                            {
                                e1.Graphics.DrawImage(bitmap, e1.PageBounds);
                            };
                            pd.Print();
                        }

                    }
                    //string file = dgvrPdiTc.Cells["tc_path"].Value.ToString();
                    //using (var pd = new PrintDocument())
                    //{
                    //    pd.PrintPage += (_, e) =>
                    //    {
                    //        var img = Image.FromFile(file);
                    //        e.Graphics.DrawImage(img, e.PageBounds);
                    //    };
                    //    pd.Print();
                    //}
                    //Process p = new Process();
                    //p.StartInfo.FileName = dgvrPdiTc.Cells["tc_path"].Value.ToString();
                    //p.StartInfo.Verb = "Print";
                    //p.Start();
                }
            }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,825 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,806 Reputation points
    2021-04-22T14:35:55.55+00:00

    Create a helper method to do the watermarking. This will make it easier to see what is going on.

    private static Image WatermarkImage ( Image image, string watermark, Font font, Brush brush, Point position )
    {
    var newImage = image.Clone() as Image;
    using (var gfx = Graphics.FromImage(newImage))
    {
    gfx.DrawString(watermark, font, brush, position);

        return newImage;
    };
    

    }

    //Usage
    var newImage = WatermarkImage(oldImage, "Test", font, brush, position)

    Note that I believe the issue you're currently seeing might be related to your position value. You're setting it to 350, 350 which may be too large for the image. Try starting at 0,0. It could also be the font family (Times Romans has no dash). If you want to place the watermark at the bottom of the image then you'll need to calculate the "bottom" by getting the size of the image and then stepping back based upon the size of the watermark text (which means you need to use the measure of the graphics).

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sushil Agarwal 381 Reputation points
    2021-04-23T01:40:55.35+00:00

    Thanks cooldadtx

    Your suggetion helped me to do work neatly with little modification.

    private static Image WatermarkImage(Image image, string watermark, Font font, Brush brush, Point position)
            {
                var newImage = image.Clone() as Image;
                using (var gfx = Graphics.FromImage(newImage))
                {
                    gfx.DrawString(watermark, font, brush, position);
    
                    return newImage;
                };
            }
            private void btnPrintPdi_Tc_Click(object sender, EventArgs e)
            {
                foreach (DataGridViewRow dgvrPdiTc in dgvPdiTc.SelectedRows)
                {
                    if (dgvrPdiTc.Cells["report"].Value.ToString().Trim().Equals("tc"))
                    {
                        //print image from file
    
                        string billnumber = dgvrPdiTc.Cells["bill_key"].Value.ToString();
                        Image img = Image.FromFile(dgvrPdiTc.Cells["tc_path"].Value.ToString());
    
                        //
                        // Create font and brush.
                        Font drawFont = new Font("Arial", 32);
                        SolidBrush drawBrush = new SolidBrush(Color.Black);
    
                        // Create point for upper-left corner of drawing.
                        Point wmloc = new Point(10, 10);
    
                        // Set format of string.
                        StringFormat drawFormat = new StringFormat();
                        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    
                        img = WatermarkImage(img, billnumber, drawFont, drawBrush, wmloc);
                        using (var pd = new PrintDocument())
                            {
                                pd.PrintPage += (_, e1) =>
                                {
                                    e1.Graphics.DrawImage(img, e1.PageBounds);
                                };
                                pd.Print();
                            }
                        //Process p = new Process();
                        //p.StartInfo.FileName = dgvrPdiTc.Cells["tc_path"].Value.ToString();
                        //p.StartInfo.Verb = "Print";
                        //p.Start();
                    }
                }
            }
    
    0 comments No comments