dcsimg
Results 1 to 15 of 15
  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Resolved [RESOLVED] inserting text on image on picture box and printing .

    Hi everyone
    I want to develop an app with characteristics below:
    - open a picture in A4 or A5 size in a picturebox
    - insert text on the picture in certain places
    - printing the picture and the text inserted in A4 or A5 size.
    first I want to use a textbox to move from one site to another in picturebox by clicking on it ,and then inserting textbox.text on picture although I don't know How.
    second I want to print that picture along with texts inserted in certain location and I don't know How too.
    now I need a guideline to bring me to a right direction .
    any help appreciated.
    dr.bayat as bayat2000

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    How do you determine where to place the text?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    Here's a simple example...

    Code:
    Public Class Form1
    
        Private WithEvents pd As New Printing.PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Private img As Bitmap
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'declare new OpenFileDialog + set it's initial properties
            Dim ofd As New OpenFileDialog With { _
            .Title = "Select image file", _
            .Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|All Files (*.*)|*.*", _
            .FilterIndex = 0, _
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}
    
            'show the dialog
            If ofd.ShowDialog = DialogResult.OK Then
                img = New Bitmap(ofd.FileName)
                PictureBox1.Image = img
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If Not img Is Nothing Then
                Dim gr As Graphics = Graphics.FromImage(img)
                Dim f As New Font(TextBox1.Font.FontFamily, 20, FontStyle.Bold Or FontStyle.Italic)
                gr.DrawString(TextBox1.Text, f, Brushes.White, 50, 150)
                ppd = New PrintPreviewDialog
                ppd.Document = pd
                ppd.WindowState = FormWindowState.Maximized
                ppd.ShowDialog()
            End If
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            e.Graphics.DrawImage(img, New Point(20, 20))
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    Quote Originally Posted by .paul. View Post
    How do you determine where to place the text?
    i don't know how .
    I want to load ,actually, for example a Job Application Form (in any image format) an fill the blanks with my app and then print it again along with inserted text . this was just an idea although I don't know how.
    any help appreciated.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    The easiest way is to make the textboxes draggable to give you visual control over the locations on the image...

    Code:
    Public Class Form1
    
        Private WithEvents pd As New Printing.PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Private img As Bitmap
    
        Const HT_CAPTION As Integer = &H2
        Const WM_NCLBUTTONDOWN As Integer = &HA1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
                AddHandler tb.MouseMove, AddressOf textboxes_MouseMove
            Next
        End Sub
    
        Private Sub textboxes_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim tb As TextBox = DirectCast(sender, TextBox)
                tb.BringToFront()
                tb.Capture = False
                Me.WndProc(Message.Create(tb.Handle, WM_NCLBUTTONDOWN, CType(HT_CAPTION, IntPtr), IntPtr.Zero))
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'declare new OpenFileDialog + set it's initial properties
            Dim ofd As New OpenFileDialog With { _
            .Title = "Select image file", _
            .Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|All Files (*.*)|*.*", _
            .FilterIndex = 0, _
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}
    
            'show the dialog
            If ofd.ShowDialog = DialogResult.OK Then
                img = New Bitmap(ofd.FileName)
                PictureBox1.Image = img
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If Not img Is Nothing Then
                Dim gr As Graphics = Graphics.FromImage(img)
                For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
                    Dim p As Point = PictureBox1.PointToClient(tb.Location)
                    gr.DrawString(tb.Text, tb.Font, Brushes.Black, p.X, p.Y)
                Next
                ppd = New PrintPreviewDialog
                ppd.Document = pd
                ppd.WindowState = FormWindowState.Maximized
                ppd.ShowDialog()
            End If
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            e.Graphics.DrawImage(img, New Point(20, 20))
        End Sub
    
    End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    This line...

    Dim p As Point = PictureBox1.PointToClient(tb.Location)

    Should actually be...

    Dim p As Point = PictureBox1.PointToClient(Me.PointToScreen(tb.Location))

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    thanks for your reply
    let me try and let me ask you again in case of being problem .
    thank you again for being bear with me.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    Quote Originally Posted by .paul. View Post
    This line...

    Dim p As Point = PictureBox1.PointToClient(tb.Location)

    Should actually be...

    Dim p As Point = PictureBox1.PointToClient(Me.PointToScreen(tb.Location))
    Hi paul
    please explain me in a simple way about pointtoclient and pointtoscreen .I can't get well what these two methods do.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    PointToClient translates screen coordinates to PictureBox1 coordinates. Me.PointToScreen translates the textbox location to screen coordinates.

    Is it working how you expected?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    yes the code you offered was perfect.
    I am trying to understand the concepts. I prefer to click on the Application Form image and one textbox moves there ,I insert text in textbox then press Enter Key, the textbox will disappear and text will insert on picturebox in coordinate that textbox were.and more important to me is printing text in right position or location on paper. hoping I express myself well.if you have any explanation or something that can guide me through right direction ,I need it.
    sincere bayat200

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    by the way I tried your code and it worked well

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    Quote Originally Posted by .paul. View Post
    PointToClient translates screen coordinates to PictureBox1 coordinates. Me.PointToScreen translates the textbox location to screen coordinates.

    Is it working how you expected?
    as far as I got these two methods have opposite function . is that so?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    Not exactly. The textbox location is in form coordinates. It's necessary to get the textbox location in picturebox coordinates. To do that we translate the form coordinates to screen coordinates, then translate the screen coordinates to picturebox coordinates.

    The only way to be sure this is working is to test it. Does it work when you test it or not?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,674

    Re: inserting text on image on picture box and printing .

    Quote Originally Posted by bayat2000 View Post
    by the way I tried your code and it worked well
    ok thanks for answering my question. I missed this answer��

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2015
    Posts
    30

    Re: inserting text on image on picture box and printing .

    Quote Originally Posted by .paul. View Post
    Not exactly. The textbox location is in form coordinates. It's necessary to get the textbox location in picturebox coordinates. To do that we translate the form coordinates to screen coordinates, then translate the screen coordinates to picturebox coordinates.

    The only way to be sure this is working is to test it. Does it work when you test it or not?
    ok I get it just now. (thanks)
    I tested it without printing (just on printpreviw), and it works well.i want to try more and I will print and tell you results.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.