0

Eu estou a fazer um pequeno pograma para fazer memoriais em fotos consiste em uma label e uma picturebox como posso gravar a label junto com a imagem da picture box para uma so imagem? este e o meu codigo

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 Resposta 1

0

Você pode implementar um Panel, e salvar o conteúdo dele:

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

Assim, você pode chamar o método para criar a memória Bitmap CriarMemoria(Image, String) escrever no arquivo, pois a imagem já estará pronta.

5
  • 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 = texto, .Font = New Font("Segoe UI", 14) ' você escolhe sua fonte aqui } Commented 28/07/2017 às 15:42
  • Hello, @HelderDaSilveiraVentura. Here on Stack Overflow, we speak Brazil's Portuguese language. Can you tell more about your error? Commented 28/07/2017 às 18:10
  • well in fact the problem is wen i try your example with a panel well i have insert a panel call moldura imgur.com/wJYcGSr Commented 28/07/2017 às 18:38
  • Strange. lab was declared some lines above the code, try to reload the solution. Commented 28/07/2017 às 19:26
  • 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 so whe can see the result PictureBox1.Invalidate() Commented 28/07/2017 às 20:03

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .