Question Putting multiple texts into an image.

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
What I have is a couple hundred patterns that I want to paste or insert or what have you onto a picturebox at a particular spot. I can get the pattern to show up where I click with no problem. What I can't do though is get more than one pattern to show up in the picturebox. Every time I click on the picturebox again, it erases the pattern I have put on previously and puts the new pattern on.

I'm sure I'm missing something basic, but it must be in a blind spot or something.

The picturebox has a grid drawn on it, the patterns come from a custom font, and there are about 202 of them.

Can someone please put me out of my misery? Here's the code:

VB.NET:
Imports System.IO
Public Class frmMain
    Private Sub lblKnot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblKnotAA.Click, lblKnotAB.Click, lblKnotAC.Click, lblKnotAD.Click, lblKnotAE.Click, lblKnotAF.Click, lblKnotAG.Click, lblKnotAH.Click, lblKnotAI.Click, lblKnotAJ.Click, lblKnotAK.Click, lblKnotAL.Click, lblKnotAM.Click,
        lblKnotBA.Click, lblKnotBB.Click, lblKnotBC.Click, lblKnotBD.Click, lblKnotBE.Click, lblKnotBF.Click, lblKnotBG.Click, lblKnotBH.Click, lblKnotBI.Click, lblKnotBJ.Click, lblKnotBK.Click, lblKnotBL.Click, lblKnotBM.Click, lblKnotCA.Click, lblKnotCB.Click, lblKnotCC.Click, lblKnotCD.Click, lblKnotCE.Click, lblKnotCF.Click, lblKnotCG.Click, lblKnotCH.Click, lblKnotCI.Click, lblKnotCJ.Click, lblKnotCK.Click, lblKnotCL.Click, lblKnotCM.Click,
        lblKnotDA.Click, lblKnotDB.Click, lblKnotDC.Click, lblKnotDD.Click, lblKnotDE.Click, lblKnotDF.Click, lblKnotDG.Click, lblKnotDH.Click, lblKnotDI.Click, lblKnotDJ.Click, lblKnotDK.Click, lblKnotDL.Click, lblKnotDM.Click, lblKnotEA.Click, lblKnotEB.Click, lblKnotEC.Click, lblKnotED.Click, lblKnotEE.Click, lblKnotEF.Click, lblKnotEG.Click, lblKnotEH.Click, lblKnotEI.Click, lblKnotEJ.Click, lblKnotEK.Click, lblKnotEL.Click, lblKnotEM.Click,
        lblKnotFA.Click, lblKnotFB.Click, lblKnotFC.Click, lblKnotFD.Click, lblKnotFE.Click, lblKnotFF.Click, lblKnotFG.Click, lblKnotFH.Click, lblKnotFI.Click, lblKnotFJ.Click, lblKnotFK.Click, lblKnotFL.Click, lblKnotFM.Click, lblKnotGA.Click, lblKnotGB.Click, lblKnotGC.Click, lblKnotGD.Click, lblKnotGE.Click, lblKnotGF.Click, lblKnotGG.Click, lblKnotGH.Click, lblKnotGI.Click, lblKnotGJ.Click, lblKnotGK.Click, lblKnotGL.Click, lblKnotGM.Click,
        lblKnotHA.Click, lblKnotHB.Click, lblKnotHC.Click, lblKnotHD.Click, lblKnotHE.Click, lblKnotHF.Click, lblKnotHG.Click, lblKnotHH.Click, lblKnotHI.Click, lblKnotHJ.Click, lblKnotHK.Click, lblKnotHL.Click, lblKnotHM.Click, lblKnotIA.Click, lblKnotIB.Click, lblKnotIC.Click, lblKnotID.Click, lblKnotIE.Click, lblKnotIF.Click, lblKnotIG.Click, lblKnotIH.Click, lblKnotII.Click, lblKnotIJ.Click, lblKnotIK.Click, lblKnotIL.Click, lblKnotIM.Click,
        lblKnotJA.Click, lblKnotJB.Click, lblKnotJC.Click, lblKnotJD.Click, lblKnotJE.Click, lblKnotJF.Click, lblKnotJG.Click, lblKnotJH.Click, lblKnotJI.Click, lblKnotJJ.Click, lblKnotJK.Click, lblKnotJL.Click, lblKnotJM.Click, lblKnotKA.Click, lblKnotKC.Click, lblKnotKD.Click, lblKnotKE.Click, lblKnotKF.Click, lblKnotKG.Click, lblKnotKH.Click, lblKnotKI.Click, lblKnotKJ.Click, lblKnotKK.Click, lblKnotKM.Click, lblKnotLA.Click, lblKnotLB.Click,
        lblKnotLC.Click, lblKnotLD.Click, lblKnotLE.Click, lblKnotLF.Click, lblKnotLG.Click, lblKnotLH.Click, lblKnotLI.Click, lblKnotLJ.Click, lblKnotLK.Click, lblKnotLL.Click, lblKnotLM.Click, lblKnotMB.Click, lblKnotMC.Click, lblKnotMD.Click, lblKnotME.Click, lblKnotMF.Click, lblKnotMG.Click, lblKnotMH.Click, lblKnotMI.Click, lblKnotMJ.Click, lblKnotMK.Click, lblKnotML.Click, lblKnotNA.Click, lblKnotNC.Click, lblKnotND.Click, lblKnotNE.Click,
        lblKnotNF.Click, lblKnotNG.Click, lblKnotNH.Click, lblKnotNI.Click, lblKnotNJ.Click, lblKnotNK.Click, lblKnotNM.Click, lblKnotOA.Click, lblKnotOB.Click, lblKnotOC.Click, lblKnotOD.Click, lblKnotOE.Click, lblKnotOF.Click, lblKnotOG.Click, lblKnotOH.Click, lblKnotOI.Click, lblKnotOJ.Click, lblKnotOK.Click, lblKnotOL.Click, lblKnotOM.Click, lblKnotPB.Click, lblKnotPC.Click, lblKnotPD.Click, lblKnotPE.Click, lblKnotPF.Click, lblKnotPG.Click,
        lblKnotPH.Click, lblKnotPI.Click, lblKnotPJ.Click, lblKnotPK.Click, lblKnotPL.Click, lblKnotPM.Click, lblKnotQM.Click

        lblKnotDisp.Text = sender.Text
        sender.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        numRows.Value = 25
        numColumns.Value = 30
        numSize.Value = 30

    End Sub

    Private Sub numRows_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numRows.ValueChanged
        picGrid.Refresh()
    End Sub

    Private Sub numColumns_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numColumns.ValueChanged
        picGrid.Refresh()
    End Sub

    Private Sub frmMain_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        picGrid.Refresh()
    End Sub

    Private Sub picGrid_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picGrid.MouseDown
        Dim img As New Bitmap(picGrid.Width, picGrid.Height)

        Using g = Graphics.FromImage(img)
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            g.DrawString(lblKnotDisp.Text, lblKnotDisp.Font, Brushes.Black, New PointF(e.X, e.Y))
        End Using
        If picGrid.Image IsNot Nothing Then
            picGrid.Image.Dispose()
        End If
        picGrid.Image = img

    End Sub

    Private Sub picGrid_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picGrid.Paint
        Dim objPen As Pen
        Dim objPicGraphics As Graphics = e.Graphics
        Dim picSizeX As Integer = numColumns.Value * numSize.Value
        Dim picSizeY As Integer = numRows.Value * numSize.Value
        picGrid.Size = New Size(picSizeX, picSizeY)

        objPen = New Pen(Drawing.Color.Sienna, 1)
        objPen.DashStyle = Drawing2D.DashStyle.Solid

        For ridx As Integer = 0 To picSizeY Step numSize.Value
            objPicGraphics.DrawLine(objPen, 0, ridx, picSizeX, ridx)
        Next

        For cidx As Integer = 0 To picSizeX Step numSize.Value
            objPicGraphics.DrawLine(objPen, cidx, 0, cidx, picSizeX)
        Next

    End Sub

    Private Sub numSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles numSize.ValueChanged
        picGrid.Refresh()
        picTemp.Size = New Size(numSize.Value, numSize.Value)
    End Sub
End Class

Thank you for your time and attention to this problem.
 
Last edited:
That's because you are creating a new Bitmap object every time in the MouseDown event handler. If you expect to add something new to the existing image then you need to use the existing image as a string point, not a new image that will obviously be blank by default. Just create one Bitmap and load it into the PictureBox at the start, then draw on that Bitmap every time.
 
D'Oh! See, I told you it was something basic. Must be the lack of sleep.

I changed the line in the Mousedown event from:
VB.NET:
 Dim img As New Bitmap(picGrid.Width, picGrid.Height)

To:
VB.NET:
Dim img As New Bitmap(picGrid.Image, picGrid.Width, picGrid.Height)

Thanks.
 
You're still creating a new Image object each time. The new code simply uses the existing Image as a basis for that new Image. Why not just use the existing Image?
 
I'm rather incredulous that you need an example but here goes:
Dim img As Image = picGrid.Image
 

Latest posts

Back
Top