CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2008
    Posts
    142

    text and picture problem

    i m writing a small program to add text on the picture box
    but when i run my program...then this error comes"Object reference not set to an instance of an object." on the following line
    Code:
     Dim tbm As Bitmap = CType(bm.Clone, Bitmap)
    whole paint event is as follow
    Code:
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            Dim tbm As Bitmap = CType(bm.Clone, Bitmap)
            Dim g As Graphics = Graphics.FromImage(tbm)
            Dim mx As Matrix = New Matrix
            Dim gpathText As New GraphicsPath
            Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
                                                  Color.LightCoral))
    
            'Smooth the Text
            g.SmoothingMode = SmoothingMode.AntiAlias
    
            'Make the GraphicsPath for the Text
            Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
            'gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
            ' emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
            ' StringFormat.GenericDefault)
    
            'Draw a copy of the image to the Graphics Object canvas
            g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
    
            'Rotate the Matrix at the center point
            mx.RotateAt(tbarRotate.Value, _
                New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
    
            'Get the points for the rotated text bounds
            mx.TransformPoints(ptsText)
    
            'Transform the Graphics Object with the Matrix
            g.Transform = mx
    
            'Draw the Rotated Text
            g.FillPath(br, gpathText)
            If chkAddOutline.Checked Then
                Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, Color.White), 1)
                    g.DrawPath(pn, gpathText)
                End Using
            End If
    
            'Draw the box if the mouse is over the Text
            If MouseOver Then
                g.ResetTransform()
                g.DrawPolygon(ptsTextPen, ptsText)
            End If
    
            'Draw the whole thing to the form
            e.Graphics.DrawImage(tbm, 10, 10)
    
            tbm.Dispose()
            g.Dispose()
            mx.Dispose()
            br.Dispose()
            gpathText.Dispose()
        End Sub
    i have converted this program from an other program which ads text on the form to a pre-assigned picture on the form
    but i have converted it into a picture box
    i have added a picture box and calling its paint event while in the original program..form pain event is called
    attaching the file
    kindly help me
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: text and picture problem

    What you need here is the Graphics object, and your Bitmap must acctually be a Image type
    Code:
    Dim tbm As Image
    Dim tbmGobj as Graphics
    
    tbm = New Bitmap(bm.Image)
    tbGobj = Graphics.FromImage (bm.Image)
    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Mar 2008
    Posts
    142

    Re: text and picture problem

    i have tried ur code but this is giving errors
    it is giving two errors on the following line
    Code:
    tbm = New Bitmap(bm.image)
    tbmGobj = Graphics.FromImage(bm.image)
    Error is "image' is not a member of 'System.Drawing.Bitmap'.
    now whole code of picture paint event is as follow
    Code:
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    
            Dim tbm As Image
            Dim tbmGobj As Graphics
            tbm = New Bitmap(bm.image)
            tbmGobj = Graphics.FromImage(bm.image)
            Dim g As Graphics = Graphics.FromImage(tbm)
            Dim mx As Matrix = New Matrix
            Dim gpathText As New GraphicsPath
            Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
                                                  Color.LightCoral))
    
            'Smooth the Text
            g.SmoothingMode = SmoothingMode.AntiAlias
    
            'Make the GraphicsPath for the Text
            Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
            'gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
            ' emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
            ' StringFormat.GenericDefault)
    
            'Draw a copy of the image to the Graphics Object canvas
            g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
    
            'Rotate the Matrix at the center point
            mx.RotateAt(tbarRotate.Value, _
                New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
    
            'Get the points for the rotated text bounds
            mx.TransformPoints(ptsText)
    
            'Transform the Graphics Object with the Matrix
            g.Transform = mx
    
            'Draw the Rotated Text
            g.FillPath(br, gpathText)
            If chkAddOutline.Checked Then
                Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, Color.White), 1)
                    g.DrawPath(pn, gpathText)
                End Using
            End If
    
            'Draw the box if the mouse is over the Text
            If MouseOver Then
                g.ResetTransform()
                g.DrawPolygon(ptsTextPen, ptsText)
            End If
    
            'Draw the whole thing to the form
            e.Graphics.DrawImage(tbm, 10, 10)
    
            tbm.Dispose()
            g.Dispose()
            mx.Dispose()
            br.Dispose()
            gpathText.Dispose()
        End Sub
    secondly i m loading picture into the picture box through open dialog as follow
    Code:
    Private Sub OpenPictureToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPictureToolStripMenuItem.Click
            Try
                Dim openFileDlg As New OpenFileDialog
                openFileDlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
                With openFileDlg
                    .CheckFileExists = True
                    .ShowReadOnly = False
                    .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
                    .FilterIndex = 2
                    If .ShowDialog = Windows.Forms.DialogResult.OK Then
                        Me.PictureBox1.Image = Image.FromFile(.FileName)
                        PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
                        'If Auto_Size Then Pic2Pic(pb, Panel1, Enlarge, Shrink)
                    End If
                End With
            Catch ex As Exception
                MessageBox.Show("Can not open file")
            End Try
        End Sub
    i didn't understand this one"your Bitmap must acctually be a Image type"
    kindly guide me more..

  4. #4
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: text and picture problem

    As you've defined bm as a bitmap, this is a valid argument for both New Bitmap() and Graphics.FromImage()
    Therfore:
    Code:
                    tbm = New Bitmap(bm)
                    tbmGobj = Graphics.FromImage(bm)
    Hope that Helps!
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  5. #5
    Join Date
    Mar 2008
    Posts
    142

    Re: text and picture problem

    i have tried this code but error is coming on the following line when i run the application
    Code:
    tbm = New Bitmap(bm)
    what can i do now???
    kindly help me.

  6. #6
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: text and picture problem

    What is the error? If it mentioned a Null Reference, then the problem is that the picture bo gets painted before the variable bm is initialised. the best way round this is to declare a private variable in the form to determine whether a bitmap hads been loaded, or to perform a test on bm. For example, adding:
    Code:
    If bm Is Nothing Then Exit Sub
    to the beginning of the paint routine should sort things out. It's not the neatest way, but the quickest to write.
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  7. #7
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: text and picture problem

    I've just noticed that your program fails to do anything then. In fact, as far as I can see, bm is only used as a reference point for reate other bitmaps. It is never given any dimensions, nor is it ever used directly. Therefore, it's default state is Nothing. Is it meant to represent the original image? In which case just use PictureBox1.Image.
    Also, tbmGobj seems to serve no purpose either...
    Well, If I'm honest, I'm a bit out of my depth in graphics...I'll stick to text and logic any day...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  8. #8
    Join Date
    Mar 2008
    Posts
    142

    Re: text and picture problem

    i m giving some code here
    in this code paint is applied on the form and a picture is paint on the form when program loads and a text is also loaded when first time run.
    but i want make changes as following
    1.i can invoke paint event of picture box
    2.when program loads first time...picture box should nothing
    3.i can load picture from disk through a open dialog box after loading the form
    4.i can then add the text on the picture manually through a text box
    Code:
    Dim pic_font As New Font("Arial Black", 40, FontStyle.Regular, GraphicsUnit.Pixel)
        Dim bm As Bitmap
        Dim strText As String = "Diver Dude"
        Dim szText As New SizeF
        Dim ptText As New Point(125, 125)
        Dim ptsText() As PointF
        Dim MovingOffset As PointF
        Dim ptsTextPen As Pen = New Pen(Color.LightSteelBlue, 1)
        Dim MouseMoving As Boolean
        Dim MouseOver As Boolean
    Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            bm = Image.FromFile(Application.StartupPath & "\DivePic.bmp")
            szText = Me.CreateGraphics.MeasureString(strText, pic_font)
            setptsText()
            ptsTextPen.DashStyle = DashStyle.Dot
        End Sub
    Code:
    Private Sub Form1_Paint(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles MyBase.Paint
    
            Dim tbm As Bitmap = CType(bm.Clone, Bitmap)
            Dim g As Graphics = Graphics.FromImage(tbm)
            Dim mx As Matrix = New Matrix
            Dim gpathText As New GraphicsPath
            Dim br As SolidBrush = New SolidBrush(Color.FromArgb(tbarTrans.Value, _
                                                  Color.LightCoral))
    
            'Smooth the Text
            g.SmoothingMode = SmoothingMode.AntiAlias
    
            'Make the GraphicsPath for the Text
            Dim emsize As Single = Me.CreateGraphics.DpiY * pic_font.SizeInPoints / 72
            gpathText.AddString(strText, pic_font.FontFamily, CInt(pic_font.Style), _
                emsize, New RectangleF(ptText.X, ptText.Y, szText.Width, szText.Height), _
                StringFormat.GenericDefault)
    
            'Draw a copy of the image to the Graphics Object canvas
            g.DrawImage(CType(bm.Clone, Bitmap), 0, 0)
    
            'Rotate the Matrix at the center point
            mx.RotateAt(tbarRotate.Value, _
                New Point(ptText.X + (szText.Width / 2), ptText.Y + (szText.Height / 2)))
    
            'Get the points for the rotated text bounds
            mx.TransformPoints(ptsText)
    
            'Transform the Graphics Object with the Matrix
            g.Transform = mx
    
            'Draw the Rotated Text
            g.FillPath(br, gpathText)
            If chkAddOutline.Checked Then
                Using pn As Pen = New Pen(Color.FromArgb(tbarTrans.Value, Color.White), 1)
                    g.DrawPath(pn, gpathText)
                End Using
            End If
    
            'Draw the box if the mouse is over the Text
            If MouseOver Then
                g.ResetTransform()
                g.DrawPolygon(ptsTextPen, ptsText)
            End If
    
            'Draw the whole thing to the form
            e.Graphics.DrawImage(tbm, 10, 10)
    
            tbm.Dispose()
            g.Dispose()
            mx.Dispose()
            br.Dispose()
            gpathText.Dispose()
        End Sub
    then help me...thanks

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

Featured