Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    VB.NET Background image su picturebox non funziona correttamente

    O forse sono io che non capace di farla funzionare in maniera ottimale

    Ho una picture box, sulla quale eseguo vari DrawRectangleS.
    Lo sfondo è bianco dato dato da una bella

    Dim gr As Graphics = PictureBox1.CreateGraphics
    gr.Clear(Color.White)

    Ho provato a remmare il clear, inserendo questa riga di testo:

    PictureBox1.BackgroundImage = New Bitmap("indirizzo immagine")

    e anche

    PictureBox1.BackgroundImage = Image.FromFile("indirizzo immagine")


    ma il risultato è sempre lo stesso: la form carica l'immagine di background, ci disegna sopra i Rettangoli, e poi è come se ci caricasse ancora la background image xk i rettangoli scompaiono graficamente (ma ci sono ancora, xk ho impostato un msgbox per ogni click all'interno dei rettangoli, e anche non vedendoli, se per caso ci clicco sopra la msgbox viene fuori, quindi fisicamente ci sono)

    Vi posto il codice:

    codice:
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Drawing.Imaging
    Imports System.Drawing.Text
    
    Public Class gdivb
        Dim list As New List(Of c_ombrellone)
        Dim x As Integer
        Dim y As Integer
        Dim Zoom As Integer = 100
        Dim StepX As Integer = 5
        Dim StepY As Integer = 5
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
            For Each ombrellone As c_ombrellone In list
                ombrellone.zoom = ComboBox1.SelectedItem
                If (ombrellone.xBegin <= x) And (ombrellone.xEnd >= x) And (ombrellone.yBegin <= y) And (ombrellone.yEnd >= y) Then
                    If (ombrellone.xBegin <= x) And (x <= ombrellone.xBegin + ombrellone.w / 3) And (y >= ombrellone.yBegin + ombrellone.h / 2) And (ombrellone.yEnd >= y) Then
                        MsgBox(ombrellone.Testo + " quadrato di sinistra.")
                        Exit For
                    ElseIf (ombrellone.xBegin + ombrellone.w / 3 * 2 <= x) And (x <= ombrellone.xEnd) And (y >= ombrellone.yBegin + ombrellone.h / 2) And (ombrellone.yEnd >= y) Then
                        MsgBox(ombrellone.Testo + " quadrato di destra")
                        Exit For
                    Else
                        MsgBox(ombrellone.Testo)
                        Exit For
                    End If
                End If
            Next
        End Sub
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            x = e.X
            y = e.Y
        End Sub
    
        Private Sub gdivb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add("100")
            ComboBox1.Items.Add("120")
            ComboBox1.Items.Add("150")
            ComboBox1.Items.Add("180")
            ComboBox1.Items.Add("200")
            ComboBox1.SelectedItem = "100"
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, Button1.Click
            Zoom = CInt(ComboBox1.SelectedItem)
            PictureBox1.BackgroundImage = New Bitmap("C:\Users\Luca\Documents\Visual Studio 2010\Projects\Ombrelloni\Ombrelloni\Chrysanthemum.jpg")
            Dim gr As Graphics = PictureBox1.CreateGraphics
            'gr.Clear(Color.White)
            Dim w As Integer = 50
            Dim h As Integer = 30
            Dim f As New Font("Verdana", 8, FontStyle.Bold)
            For i As Integer = 1 To 10
                For j As Integer = 1 To 33
                    Dim o As New c_ombrellone("Ombrellone " + i.ToString + " - " + j.ToString, (j - 1) * (w + StepX), (i - 1) * (h + StepY), w, h, Color.Red, "Verdana", 8, FontStyle.Bold)
                    o.zoom = Zoom
                    list.Add(o)
                    o.ScriviQuadrato(gr, Brushes.Black)
                Next
            Next
        End Sub
    La combobox la uso per zoommare (ma per il problema che vi pongo non è importante, ve lo dico per completezza), e vi faccio notare che il disegno viene creato su pressione del bottone Button1


    Vi posto inoltre la classe c_ombrellone:
    codice:
    Public Class c_ombrellone
        Property IDPrenotazione As Integer
        Property Testo As String
        Property zoom As Integer = 100
        Dim mxBegin As Integer
        ReadOnly Property xBegin As Integer
            Get
                Return CInt(mxBegin * zoom / 100)
            End Get
        End Property
        Dim mybegin As Integer
        ReadOnly Property yBegin As Integer
            Get
                Return CInt(mybegin * zoom / 100)
            End Get
        End Property
        Dim mxend As Integer
        ReadOnly Property xEnd As Integer
            Get
                Return CInt(mxend * zoom / 100)
            End Get
        End Property
        Dim myEnd As Integer
        ReadOnly Property yEnd As Integer
            Get
                Return CInt(myEnd * zoom / 100)
            End Get
        End Property
        ReadOnly Property w As Integer
            Get
                Return xEnd - xBegin
            End Get
        End Property
        ReadOnly Property h As Integer
            Get
                Return yEnd - yBegin
            End Get
        End Property
        Property BackColor As Color
        Property Font As Font
    
        Sub New()
    
        End Sub
    
        Sub New(ByVal n As String, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal backColor As Color, ByVal fontFamily As String, ByVal fontSizeEm As Integer, ByVal fontStile As FontStyle)
            Testo = n
            mxBegin = x
            mybegin = y
            mxend = x + w
            myEnd = y + h
            Me.BackColor = BackColor
            Font = New Font(fontFamily, fontSizeEm, fontStile)
        End Sub
    
        Sub ScriviQuadrato(ByRef gr As Graphics, ByVal Brush As System.Drawing.Brush)
            Dim r As New RectangleF(xBegin, yBegin, w, h)
            Dim l As New RectangleF(xBegin, yBegin + h / 2, w / 3, h / 2)
            Dim s As New RectangleF(xBegin + w / 3 * 2, yBegin + h / 2, w / 3, h / 2)
            gr.DrawRectangles(Pens.Blue, New RectangleF() {r})
            gr.DrawRectangles(Pens.Black, New RectangleF() {l})
            gr.DrawRectangles(Pens.Black, New RectangleF() {s})
            Dim sa = System.Drawing.StringFormat.GenericDefault
            sa.Alignment = StringAlignment.Center
            sa.LineAlignment = StringAlignment.Near
            gr.DrawString(Testo, Font, Brush, r, sa)
        End Sub
    End Class

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463
    Il controllo funziona correttamente.

    Il problema è legato al fatto che i disegni tracciati da te non sono "persistenti", poiché ogni volta che il controllo si aggiorna va a disegnare nella propria area occupata sullo schermo l'immagine di sfondo, e quindi i tuoi disegni vanno perduti poiché non vengono memorizzati dal controllo per essere sovrascritti.

    Credo che le vie più rapide per risolvere siano
    [list=1][*]creare un controllo personalizzato e ridefinire il metodo che si occupa di disegnare gli elementi grafici all'interno dell'area visibile;[*]disegnare direttamente sull'immagine selezionata come sfondo.[/list=1]

    Ciao!
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Ciao, grazie per la risposta

    Puoi darmi una mano per la ridefinizione del metodo? Come pensi che io possa muovermi?

  4. #4
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463
    Crea uno UserControl nel tuo progetto e ridefinisci il metodo OnPaint come indicato negli esempi della documentazione MSDN.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.