Results 1 to 3 of 3

Thread: [RESOLVED] Printing an image

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Resolved [RESOLVED] Printing an image

    I have a form with many controls and in the top left I have an image. I have added a printdialog and print document. I have the code below in my PrintPage. The textbox's print fine but the images do not get printed and I get no error. You will see several attempts at different ways to print an image. Feel free to correct me on any of them.

    Code:
    private void mDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
    
                float xPos = e.MarginBounds.Left;
                float yPos = e.MarginBounds.Top;
                float lineHeight = mFont.GetHeight(e.Graphics);
                if (varDef == 0)
                {
                    {
                        //Bitmap bitmap = new Bitmap("M:\\Apps\\DocCtrl\\Graphics\\DNDFlag.gif");
                        //e.Graphics.DrawImage(bitmap, 20, 30);
                        e.Graphics.DrawString(txtEng.Text, mFont2, Brushes.Black, xPos, yPos);
                        Image imageFile = Image.FromFile("M:\\Apps\\DocCtrl\\Graphics\\DNDFlag2.gif");
                        Graphics newgraphics = Graphics.FromImage(imageFile);
                        e.Graphics.DrawImage(imageFile, new Point(Convert.ToInt16(xPos), Convert.ToInt16(yPos)));
                        newgraphics.Dispose();
                        e.Graphics.DrawImage(Image.FromFile("M:\\Apps\\Resource Management\\loanflag3.jpg"), xPos, yPos);
    
                        yPos += lineHeight;
                        yPos += lineHeight;
                        e.Graphics.DrawString(textBox2.Text, mFont2, Brushes.Black, xPos, yPos);
                        yPos += lineHeight;
                        yPos += lineHeight;
    
                    }
    Problem solved, I had 2 methods and the one I was edditting was the wrong one. DUH./
    Last edited by Beast777; Aug 14th, 2007 at 12:45 PM.

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Printing an image

    The e.Graphics.DrawImage method seems like a correct way to do it.

    Code:
    private void mDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    
        float xPos = e.MarginBounds.Left;
        float yPos = e.MarginBounds.Top;
        float lineHeight = mFont.GetHeight(e.Graphics);
        if (varDef == 0)
        {
            e.Graphics.DrawString(txtEng.Text, mFont2, Brushes.Black, xPos, yPos);
            e.Graphics.DrawImage(Image.FromFile("M:\\Apps\\Resource Management\\loanflag3.jpg"), xPos, yPos);
    
            yPos += lineHeight;
            yPos += lineHeight;
            e.Graphics.DrawString(textBox2.Text, mFont2, Brushes.Black, xPos, yPos);
            yPos += lineHeight;
            yPos += lineHeight;
        }
    }

    What happens if you 'print preview' instead of actually printing, is the page displayed correctly then?

    Code:
    PrintPreviewDialog p_prv = new PrintPreviewDialog();
    p_prv.Document = this.mDoc; // Set this to your print document.  I'm guessing it is called mDoc.
    p_prv.ShowDialog();
    p_prv.Dispose();
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [RESOLVED] Printing an image

    Hang on a minute, you are not supposed to solve it before I have finished typing a reply!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width