how to save the result of an image and a label to a single file?

Asked

Viewed 65 times

0

I’m doing a little pograma to memorialize photos consists of a label and a picturebox how can I record the label together with the picture box image to a single image? this is my code

Imports System.Diagnostics
Imports System.Drawing

Public Class Form1

Dim PPoint As Point
Dim image As Image
Dim graphics As Graphics
Dim fontx As Font
Dim point As PointF
Dim color
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    txtValueX.Text = e.x.tostring
    txtValueY.Text = e.Y.ToString
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.InitialDirectory = "C:\"
    OpenFileDialog1.FileName = "Open A File..."
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim sName As String = OpenFileDialog1.SafeFileName
        Dim img As String = OpenFileDialog1.FileName
        PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If
    ' Load image
    image = Drawing.Image.FromFile(OpenFileDialog1.FileName)
    graphics = Drawing.Graphics.FromImage(image)
    ' Create font
    fontx = New Font("Times New Roman", 42.0F)
    ' Create text position
    point = New PointF(txtValueX.Text, txtValueY.Text)
    ' Draw text
    graphics.DrawString(TextBox2.Text, fontx, Brushes.Red, point)



End Sub
Private Sub mouseMove_Capture(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
MyBase.MouseMove
    Dim mousePos As Point = Control.MousePosition
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.InitialDirectory = "C:\"
    OpenFileDialog1.FileName = "Open A File..."
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim sName As String = OpenFileDialog1.SafeFileName
        Dim img As String = OpenFileDialog1.FileName
        PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If


End Sub


Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label5.Parent = PictureBox1

End Sub


Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    '// Save The image in PictureBox in its Original size.
    If Me.PictureBox1.Image IsNot Nothing Then
        Me.PictureBox1.Image.Save(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "TestFile.jpg"))
    End If

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Process.Start("1.jpg")
End Sub


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick





End Sub




Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    PPoint = New Point(e.X, e.Y)
    Label5.Location = New Point(e.X, e.Y)



End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
    Label5.Text = TextBox2.Text
    Label5.TextAlign = ContentAlignment.MiddleLeft

    Label5.Font = New Font("Comic Sans MS", 12, _
                FontStyle.Bold Or FontStyle.Underline)
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
    Label5.Font = New Font("Comic Sans MS", TrackBar1.Value, _
                FontStyle.Bold Or FontStyle.Underline)
End Sub

Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    FontDialog1.ShowDialog()
    Label5.Font = FontDialog1.Font
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    ColorDialog1.ShowDialog()
    Label5.ForeColor = ColorDialog1.Color

End Sub
End Class

1 answer

0

You can implement a Panel, and save its contents:

Imports System.Drawing
...
Public Function CriarMemoria(ByVal foto As Image, texto As String, alinharNoCentro as Boolean) As Bitmap
    ' Cria um painel que será nossa imagem
    Dim moldura As New Panel With {
        .Size = foto.Size,
        .Location = New Point(0, 0)
    }
    ' Coloca o fundo no painel
    moldura.BackgroundImage = foto
    ' Ajusta o layout para preencher tudo
    moldura.BackgroundImageLayout = ImageLayout.Stretch
    ' Cria o tal label com a moldura
    Dim lab As New Label With {
        .Text = texto,
        .Font = New Font("Segoe UI", 14) ' você escolhe sua fonte aqui
    }
    ' Adiciona o label para a moldura
    moldura.Controls.Add(label)
    ' Ajusta a posição do label
    If(alinharNoCentro) Then
         lab.AutoSize = False
         lab.Dock = DockStyle.Top
         label.TextAlign = ContentAlignment.TopCenter
         label.Height = 32 ' ajusta a altura do label de acordo com a fonte
    Else
         lab.AutoSize = True
         lab.Dock = DockStyle.Top
    End If
    ' Cria uma imagem à partir desse painel criado
    Dim output As New Bitmap(moldura.Width, moldura.Height)
    Dim rect As New Rectangle(new Point(0, 0), moldura.Size)
    ' Escreve no bitmap a imagem
    moldura.DrawToBitmap(output, rect)
    ' Retorna o criado
    Return output
End Function

So you can call the method to create memory Bitmap CriarMemoria(Image, String) write to the file, because the image will already be ready.

  • i have one Question its Give me error on lab its not declared if i understand moldura its the panel but it s Give me an error on this line Dim lab As New Label With { . Text = text, . Font = New Font("Segoe UI", 14) ' you choose your font here }

  • Hello, @Helderdasilveiraventura. Here on Stack Overflow, we Speak Brazil’s Portuguese language. Can you Tell more about your error?

  • well in Fact the problem is Wen i Try your example with a panel well i have Insert a panel call moldura http://imgur.com/wJYcGSr

  • Strange. lab was declared some Lines above the code, Try to Reload the Solution.

  • 1

    well in Fact i have Try and i Stay with same problem but i have Solved Temporary the problem with this Lines Dim mystring As String = Label5.Text Dim myFont As Font = New Font("Arial", 13, Fontstyle.Italic, Graphicsunit.Pixel) Dim gr As Graphics = Graphics.Fromimage(Picturebox1.Image) 'Drawing takes place here: gr.Drawstring(mystring, myFont, Brushes.White, txtValueX.Text, txtValueY.Text) 'force a repaint result so whe can see the Picturebox1.Invalidate()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.