| |
VB.NET - FortgeschritteneTransparente Bilder | | | Autor: dm1485 | Datum: 15.02.18 09:03 |
| Hallo zusammen,
ich komme bei einer Sache nicht weiter. Ich habe folgenden Fall.
Ich habe ein Hintergrundbild. Über dieses Hintergrundbild will ich beliebig viele transparente Bilder, die unterschiedlich sind, zur Laufzeit hinzufügen können. Die transparenten Bilder sollen sich mit der Maus vergrößern und bewegen lassen. Das Ganze auch so, dass es nicht flackert beim Bewegen.
Ich habe das Ganze soweit auch schon umgesetzt, nur habe ich das Problem, wenn ich ein transparentes Bild anklicke, dass sich alle anderen transparenten Bilder dem angeklickten Bild anpassen. Das ist natürlich nicht gewollt.
Das ist mein bisheriger Code:
Bei Klick auf einen Button füge ich ein neues Bild ein
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click
nPanel = New NonFlickerPanel
Dim fstream As FileStream
xPan += 1
nPanel.Parent = PictureBox
nPanel.Name = "pan_" & xPan
nPanel.BackgroundImageLayout = ImageLayout.Zoom
SelectedControl = nPanel
MyControls.Add(nPanel)
fstream = New FileStream("Pfad zu meinem Bild", IO.FileMode.Open, _
IO.FileAccess.Read)
nPanel.BackgroundImage = Image.FromStream(fstream)
Application.DoEvents()
fstream.Close()
fstream.Dispose()
nPanel.Width = nPanel.BackgroundImage.Width * _scale
nPanel.Height = nPanel.BackgroundImage.Height * _scale
nPanel.BringToFront()
AddHandler nPanel.MouseDown, AddressOf control_MouseDown
AddHandler nPanel.MouseEnter, AddressOf control_MouseEnter
AddHandler nPanel.MouseLeave, AddressOf control_MouseLeave
AddHandler nPanel.MouseMove, AddressOf control_MouseMove
AddHandler nPanel.MouseUp, AddressOf control_MouseUp
AddHandler nPanel.Click, AddressOf control_Click
AddHandler nPanel.Paint, AddressOf control_Paint
pubControl = nPanel
End Sub Ich habe eine extra Klasse, um ein transparentes Panel zu erzeugen:
Partial Public Class NonFlickerPanel
Inherits Panel
Public Sub New()
DoubleBuffered = True
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, True)
MyBase.BackColor = System.Drawing.Color.Transparent
End Sub
Protected Overrides ReadOnly Property CreateParams() As _
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT
Return cp
End Get
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim ctrl As Control = Form1.SelectedControl
MyBase.OnPaint(e)
If MyBase.BackgroundImage IsNot Nothing Then
e.Graphics.DrawImage(ctrl.BackgroundImage, 0, 0, ctrl.Width, _
ctrl.Height)
End If
End Sub
End Class Beim MouseMove Event aktualisiere ich die Bilder
Private Sub control_MouseMove(ByVal sender As Object, ByVal e As _
MouseEventArgs)
If e.Button = MouseButtons.Left Then
Dim control As Control = CType(sender, Control)
Dim nextPosition As System.Drawing.Point = New System.Drawing.Point( _
)
nextPosition = control.Parent.PointToClient(MousePosition)
nextPosition.Offset(mouseX, mouseY)
Dim test As String = control.Name
control.Location = nextPosition
control.Invalidate()
control.Cursor = Cursors.Default
PictureBox.Invalidate()
End If
End Sub Und hier das Paint Event um die Bilder zu zeichnen.
Private Sub control_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs)
Dim ctrl As Control = SelectedControl
Dim bmp As Bitmap = New Bitmap(ctrl.BackgroundImage)
e.Graphics.DrawImage(bmp, 0, 0, ctrl.Width, ctrl.Height)
End Sub Beim Paint Event scheint es immer alle Panel zu aktualisieren. Wie kann ich das verhindern???
Gruß
Dominik | |
Re: Transparente Bilder | | | Autor: Manfred X | Datum: 15.02.18 11:52 |
| Hallo!
Ich habe den Code nur mal kurz angeschaut und bin
in dieser Zeile "hängengeblieben" (NonFlickerPanel):
Dim ctrl As Control = Form1.SelectedControl | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 15.02.18 13:27 |
| Hallo Manfred,
SelectedControl ist immer das Bild, das gerade angeklickt ist und z.B. verschoben werden soll.
Über das Form1.SelectedControl wollte ich das Control in die extra Klasse reinholen. Ziel war, dass dann evtl nur das Control aktualisiert wird.
Gruß
Dominik | |
Re: Transparente Bilder | | | Autor: Manfred X | Datum: 15.02.18 18:06 |
| Grundregel:
Übergeordnete Klassen kommunizieren mit untergeordneten Klassen,
indem sie deren öffentliche Eigenschaften setzen/abfragen oder deren
öffentliche Methoden aufrufen.
Untergeordnete Klassen kommunizieren mit übergeordneten Klassen,
indem sie öffentliche Ereignisse auslösen und ggf. Content im
zweiten Eventparameter zur Verfügung stellen.
Richte in "NonFlickerPanel" eine öffentliche Property ein (Typ:Bitmap).
Im Parent-Formular ist diese Eigenschaft der jeweils gewünschten
Instanz von "NonFlickerPanel" mit dem anzuzeigenden (selected) Bild
durch Zuweisung einer Bitmap zu füllen.
In dieser Property (Set-Part) kann nach Zuweisung ein "Invalidate"
ausgeführt werden. | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 16.02.18 10:07 |
| Hallo Manfred,
danke für deine Hilfe.
So ganz habe ich es noch nicht verstanden. Könntest du es bitte nochmal genauer erklären was ich im NonflickerPanel machen soll?
Gruß
Dominik | |
Re: Transparente Bilder | | | Autor: Manfred X | Datum: 16.02.18 11:53 |
| Du gibst nur einige Code-Ausschnitte wieder,
so daß ich nicht beurteilen kann, was Du erreichen willst
bzw. wo mögliche Fehler liegen.
NonFlickerPanel könnte evt. so gestaltet werden ...
Partial Public Class NonFlickerPanel
Inherits Panel
Private _img As Image = New Bitmap(20, 20) 'Default
Public Property Img As Image
Set(value As Image)
If value Is Nothing Then Exit Property
_img = value
Invalidate()
End Set
Get
Return _img
End Get
End Property
Public Sub New()
DoubleBuffered = True
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, True)
MyBase.BackColor = System.Drawing.Color.Transparent
End Sub
Protected Overrides ReadOnly Property CreateParams() As _
System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT
Return cp
End Get
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
'Zoomen und Zentrieren des übergebenen Bildes
Dim zfw As Double = Me.Width / _img.Width
Dim zfh As Double = Me.Height / _img.Height
Dim zf As Double = Math.Min(zfh, zfw)
Dim w As Integer = CInt(zf * _img.Width)
Dim h As Integer = CInt(zf * _img.Height)
Dim l As Integer = CInt((Me.Width - w) / 2)
Dim t As Integer = CInt((Me.Height - h) / 2)
Dim dr As New Rectangle(l, t, w, h)
e.Graphics.DrawImage(_img, dr)
End Sub
End Class Die Eigenschaft "img" des Panel muß beim Ändern des "SelectedControl"
in Form1 das erforderliche Bild dort jeweils zugewiesen bekommen. | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 16.02.18 13:44 |
| Vielen Dank für deine Hilfe. Den letzten Teil habe ich nicht ganz verstanden. Kann ich dir das Ganze mal per Mail zukommen lassen?
Gruß | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 16.02.18 13:54 |
| Es hat mittlerweile funktioniert. Nur habe ich jetzt das Problem, das wenn ich 2 oder mehr Bilder geladen habe, dass alle Bilder wahnsinnig flackern sobald ich eines davon mit der Maus bewege. | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 16.02.18 14:44 |
| komischerweise alle bis auf das Erste das man eingefügt hat. | |
Re: Transparente Bilder | | | Autor: Manfred X | Datum: 16.02.18 15:16 |
| Hallo!
Mein Eindruck ist, daß Du Dich noch nicht hinreichend damit
beschäftigt hast, wie man Programme strukturiert, Klassen
konstruiert und wie man die Ereignisverarbeitung organisiert.
Sobald Du mehr als einfache Tools entwickeln willst,
brauchst Du diesbezüglich einige Kenntnisse - sonst stolperst
Du über "Probleme", wie Du sie schilderst.
Einem fehlerbehafteten Projekt kann man nicht (zuverlässig) entnehmen,
was mit einzelnen Code-Abschnitten oder Klassen erreicht werden soll.
Statt dessen benötigt man ein "Pflichtenheft". | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 16.02.18 15:29 |
| Mit der Klasse will ich lediglich erreichen, dass ein zu Laufzeit erstelltes Control, in dem Fall ein Panel, einen komplett transparenten Hintergrund hat und dabei nicht flackert wenn man es mit der Maus bewegt.
Wie ich schon geschrieben habe, will ich einfach x-beliebig viele Bilder über ein anderes Bild legen, deren Größe ändern können und mit der Maus verschieben können. Mittlerweile habe ich auch alles soweit mehr oder weniger am laufen. Dein Vorschlag hat mir schon weitergeholfen, nur eben, dass es jetzt wieder flackert.
Gruß | |
Re: Transparente Bilder | | | Autor: Kuno60 | Datum: 16.02.18 22:26 |
| Hallo,
Public Sub New()
DoubleBuffered = True
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, True)
MyBase.BackColor = System.Drawing.Color.Transparent
End Sub das flackern kommt daher, weil du in der ersten Zeile den Doppelpuffer einschaltest und in der dritten Zeile wieder ausschaltest. DoubleBuffered und ResizeRedraw, kann sowohl über die Eigenschaften, als auch über SetStyle festgelegt werden. | |
Re: Transparente Bilder | | | Autor: GPM | Datum: 17.02.18 15:43 |
| Warum Panels mit Bildern verwenden?
Alles einfach in die PictureBox zeichnen.
Ein einfaches Beispiel mit verschieben und skalieren(Mausrad)
Public Class Form1
Dim WithEvents Pb As New PictureBox With {.Dock = DockStyle.Fill, .Parent = _
Me}
Dim hgrund As Bitmap = SystemIcons.Shield.ToBitmap
Dim picList As New List(Of MyPicture)
Dim loc, pos As Point, img As Int32, mov As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ClientSize = New Size(800, 800)
For y = 200 To 400 Step 100
picList.Add(New MyPicture(SystemIcons.Warning.ToBitmap, New _
Rectangle(225, y, 64, 64)))
picList.Add(New MyPicture(SystemIcons.Error.ToBitmap, New Rectangle( _
375, y, 64, 64)))
picList.Add(New MyPicture(Me.Icon.ToBitmap, New Rectangle(525, y, _
64, 64)))
Next
End Sub
Private Sub Pb_MouseDown(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseDown
For i = picList.Count - 1 To 0 Step -1
If picList(i).Bounds.Contains(e.Location) Then
loc = e.Location
pos = New Point(picList(i).Bounds.X, picList(i).Bounds.Y)
picList.Add(picList(i)) 'aktuelles Bild =
picList.RemoveAt(i) 'Top-Position
img = picList.Count - 1
mov = True
Exit Sub
End If
Next
mov = False
End Sub
Private Sub Pb_MouseMove(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseMove
If mov AndAlso e.Button = MouseButtons.Left Then
Dim re As Rectangle = picList(img).Bounds
picList(img) = New MyPicture(picList(img).Img, New Rectangle(pos.X _
+ e.X - loc.X, pos.Y + e.Y - loc.Y, re.Width, re.Height))
Pb.Invalidate()
End If
End Sub
Private Sub Pb_Paint(sender As Object, e As PaintEventArgs) Handles Pb.Paint
e.Graphics.InterpolationMode = _
Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.Clear(Color.Green)
e.Graphics.DrawImage(hgrund, 0, 0, Pb.Width, Pb.Height)
For Each pic In picList
e.Graphics.DrawImage(pic.Img, pic.Bounds)
Next
End Sub
Private Sub Pb_MouseWheel(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseWheel
If Not mov Then Return 'kein Bild ausgewählt
Dim r As Rectangle = picList(img).Bounds
If e.Delta > 0 Then
picList(img) = New MyPicture(picList(img).Img, Rectangle.Inflate(r, _
4, 4))
Else
If r.Width <= 20 OrElse r.Height <= 20 Then Exit Sub ' Mindestgrösse!
picList(img) = New MyPicture(picList(img).Img, Rectangle.Inflate(r, _
-4, -4))
End If
Pb.Invalidate()
End Sub
Private Sub PB_MouseEnter(sender As Object, e As EventArgs) Handles _
Pb.MouseEnter
Pb.Focus() ' Für Pb.MouseWheel nötig!
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Pb.Invalidate()
End Sub
End Class
Public Class MyPicture
Private _img As Bitmap
Private _bounds As Rectangle
Public Sub New(Img As Bitmap, Rec As Rectangle)
_img = Img
_bounds = Rec
End Sub
Public Property Img As Bitmap
Get
Return _img
End Get
Set(value As Bitmap)
_img = Img
End Set
End Property
Public Property Bounds As Rectangle
Get
Return _bounds
End Get
Set(value As Rectangle)
_bounds = Bounds
End Set
End Property
End Class MfG GPM | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 19.02.18 12:01 |
| Hi GMP,
das sieht auch sehr gut aus. Nur habe ich noch die Anforderung, dass das Bild einen Rahmen mit Handler drum rum bekommen soll, damit ich das Bild mit der Maus scalieren kann. Wäre es bei der Lösung auch möglich??
Gruß
Dominik | |
Re: Transparente Bilder | | | Autor: GPM | Datum: 19.02.18 23:56 |
| Hier eine Demo mit 4 Eckhandler + Cursor (Ein-Aus: Maustaste rechts)
Imports System.Drawing.Drawing2D
Public Class Form1
Dim Fo As New Font("Arial", 12, FontStyle.Bold)
Dim WithEvents Pb As New PictureBox With {.Dock = DockStyle.Fill, .Parent = _
Me}
Dim hgrund As Bitmap = SystemIcons.Shield.ToBitmap
Dim picList As New List(Of MyPicture) 'Bilderliste
Dim hrList As New List(Of Rectangle) 'Handler-Rechtecke
Dim loc As Point, rec, rec2 As Rectangle
Dim hp As Int32 = 0, handler, movh As Boolean
Dim cur As New List(Of Cursor)({Cursors.SizeAll, Cursors.PanNW, _
Cursors.PanNE, Cursors.PanSW, Cursors.PanSE})
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ClientSize = New Size(800, 800)
For y = 200 To 400 Step 100
picList.Add(New MyPicture With {.Img = _
SystemIcons.Warning.ToBitmap, .Bounds = New Rectangle(225, y, 64, _
64)})
picList.Add(New MyPicture With {.Img = SystemIcons.Error.ToBitmap, _
.Bounds = New Rectangle(375, y, 64, 64)})
picList.Add(New MyPicture With {.Img = Me.Icon.ToBitmap, .Bounds = _
New Rectangle(525, y, 64, 64)})
Next
End Sub
Private Sub Pb_MouseDown(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseDown
For i = picList.Count - 1 To 0 Step -1
rec = picList(i).Bounds
If rec.Contains(e.Location) Then
loc = e.Location
picList.Add(picList(i)) 'Auswahl auf Top setzen
picList.RemoveAt(i)
SetHandler()
Exit For
End If
Next
If handler Then
Pb.Cursor = Cursors.SizeAll
For i = hrList.Count - 1 To 0 Step -1
If hrList(i).Contains(e.Location) Then
loc = e.Location
Pb.Cursor = cur(i)
hp = i
movh = True
rec = picList(picList.Count - 1).Bounds
rec2 = rec
Exit Sub
End If
Next
End If
movh = False
End Sub
Private Sub SetHandler()
Dim re As Rectangle = picList(picList.Count - 1).Bounds
hrList.Clear()
hrList.Add(re)
hrList.Add(New Rectangle(re.X - 15, re.Y - 15, 15, 15))
hrList.Add(New Rectangle(re.Right, re.Y - 15, 15, 15))
hrList.Add(New Rectangle(re.X - 15, re.Bottom, 15, 15))
hrList.Add(New Rectangle(re.Right, re.Bottom, 15, 15))
End Sub
Private Sub Pb_MouseUp(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseUp
If e.Button = MouseButtons.Right Then
handler = Not handler
Pb.Invalidate()
End If
movh = False
Pb.Cursor = Cursors.Default
End Sub
Private Sub Pb_MouseMove(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseMove
If movh AndAlso e.Button = MouseButtons.Left Then
Select Case hp
Case 0 : picList(picList.Count - 1).Bounds = New Rectangle(New _
Point(rec.X + (e.X - loc.X), rec.Y + e.Y - loc.Y), rec.Size)
Case 1
rec.X = rec2.X + (e.X - loc.X) : rec.Width = rec2.Width + ( _
loc.X - e.X)
rec.Y = rec2.Y + (e.Y - loc.Y) : rec.Height = rec2.Height + _
(loc.Y - e.Y)
Case 2
rec.Width = rec2.Width - (loc.X - e.X) : rec.Height = _
rec2.Height + (loc.Y - e.Y)
rec.Y = rec2.Y - (loc.Y - e.Y)
Case 3
rec.X = rec2.X + (e.X - loc.X) : rec.Width = rec2.Width + ( _
loc.X - e.X)
rec.Height = rec2.Height + (e.Y - loc.Y)
Case 4
rec.Width = rec2.Width + (e.X - loc.X)
rec.Height = rec2.Height + (e.Y - loc.Y)
End Select
If hp > 0 Then picList(picList.Count - 1).Bounds = rec
SetHandler()
Pb.Invalidate()
End If
End Sub
Private Sub Pb_Paint(sender As Object, e As PaintEventArgs) Handles Pb.Paint
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(hgrund, 0, 0, Pb.Width, Pb.Height)
For Each pic In picList
e.Graphics.DrawImage(pic.Img, pic.Bounds)
Next
If handler Then
Using p As New Pen(Brushes.White, 3)
For i = 0 To hrList.Count - 1
e.Graphics.DrawRectangle(p, hrList(i))
e.Graphics.DrawRectangle(Pens.Black, hrList(i))
Next
End Using
End If
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Pb.Invalidate()
End Sub
End Class
Public Class MyPicture
Public Img As Bitmap
Public Bounds As Rectangle
End Class MfG GPM | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 20.02.18 09:17 |
| WOW! Vielen Dank. Jetzt hätte ich das Ganze gerne in einen extra Panel, der im Form1 schon vorhanden ist, zeichnen. Geht das? | |
Re: Transparente Bilder | | | Autor: GPM | Datum: 20.02.18 11:08 |
| Du kannst statt in die PictureBox auch in ein Panel zeichnen.
Beim Panel muss man nur DoubleBuffered auf True setzen.
Dafür reicht:
Public Class MyPanel
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class Wenn du aber nur das Bild der PictureBox in ein Panel kopieren willst:
Dim bm As New Bitmap(800, 800)
Pb.DrawToBitmap(bm, New Rectangle(0, 0, 800, 800))
Pnl.BackgroundImage = bm oder die Bitmap im Paint-Event des Panels zeichnen.
MfG GPM | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 20.02.18 11:18 |
| Danke läuft super!
Wenn jetzt noch funktionieren würde, dass die Bilder automatisch die Postition beim vergrößern der Form1 anpassen. Habe es mit einem ScaleFaktor versucht, aber irgendwie will das nicht so richtig.
Beitrag wurde zuletzt am 20.02.18 um 11:21:29 editiert. | |
Re: Transparente Bilder | | | Autor: GPM | Datum: 20.02.18 22:26 |
| Nur mit Positionsanpassung sowie andere Cursors(Doppelpfeil)
Den Anfang bis Form1_Load ersetzen und das
Resize-Event anpassen.
Imports System.Drawing.Drawing2D
Public Class Form1
Dim WithEvents Pb As New PictureBox With {.Dock = DockStyle.Fill, .Parent = _
Me}
Dim hgrund As Bitmap = SystemIcons.Shield.ToBitmap
Dim picList As New List(Of MyPicture) 'Bilderliste
Dim hrList As New List(Of Rectangle) 'Handler-Rechtecke
Dim loc As Point, rec, rec2 As Rectangle
Dim hp As Int32 = 0, handler, movh As Boolean
Dim cur As New List(Of Cursor)({Cursors.SizeAll, Cursors.SizeNWSE, _
Cursors.SizeNESW, Cursors.SizeNESW, Cursors.SizeNWSE})
Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles _
MyBase.Load
Me.ClientSize = New Size(800, 800)
For y = 200 To 400 Step 100
picList.Add(New MyPicture With {.Img = _
SystemIcons.Warning.ToBitmap, .Bounds = New Rectangle(225, y, 64, _
64)})
picList.Add(New MyPicture With {.Img = SystemIcons.Error.ToBitmap, _
.Bounds = New Rectangle(375, y, 64, 64)})
picList.Add(New MyPicture With {.Img = Me.Icon.ToBitmap, .Bounds = _
New Rectangle(525, y, 64, 64)})
Next
For i = 0 To picList.Count - 1
imgloc.Add(New PointF(CSng(picList(i).Bounds.X / Pb.Width), CSng( _
picList(i).Bounds.Y / Pb.Height)))
Next
End Sub Altes Resize-Event löschen und hiermit ersetzen:
Dim imgloc As New List(Of PointF)
Private Sub Form1_ResizeBegin(sender As Object, e As EventArgs) Handles _
Me.ResizeBegin
imgloc.Clear()
For i = 0 To picList.Count - 1
imgloc.Add(New PointF(CSng(picList(i).Bounds.X / Pb.Width), CSng( _
picList(i).Bounds.Y / Pb.Height)))
Next
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
For i = 0 To imgloc.Count - 1
picList(i).Bounds.Location = New Point(CInt(Pb.Width * imgloc( _
i).X), CInt(Pb.Height * imgloc(i).Y))
Next
Pb.Invalidate()
End Sub MfG GPM | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 20.02.18 22:55 |
| Danke. Die Position ändert sich nun. Aber die Größe der Bilder bleibt gleich. Kann man das auch noch anpassen? Oder kann ich dir evtl mal meinen Code zukommen lassen? Ich habe es mittlerweile etwas angepasst damit ich per Button ein neues Bild hinzufügen kann. Zusätzlich zeichne ich jetzt alles in eine Picturebox.
DANKE + Gruß
Beitrag wurde zuletzt am 20.02.18 um 23:09:26 editiert. | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 21.02.18 08:50 |
| Mir ist noch aufgefallen, dass sie dich Bilder zwar mitverschieben, aber nicht proportional zur Größenänderung der Form. | |
Re: Transparente Bilder | | | Autor: GPM | Datum: 21.02.18 14:39 |
| Die Skalierung aus der Startgrösse kannst du einfach
im Paint-Event machen. Siehe Beispiel hier.
Wenn du diese Bilder aber danach bearbeiten willst
passen die Handler nicht mehr.
D.h. unskaliert bearbeiten und skaliert anzeigen.
Andernfalls müssten die Handler nach jeder Skalierung
neu berechnet werden. Das wäre dann aber deine Baustelle.
Imports System.Drawing.Drawing2D
Public Class Form1
Dim WithEvents Pb As New PictureBox With {.Dock = DockStyle.Fill, .Parent = _
Me}
Dim hgrund As Bitmap = SystemIcons.Shield.ToBitmap
Dim picList As New List(Of MyPicture) 'Bilderliste
Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles _
MyBase.Load
Me.ClientSize = New Size(800, 800) ' Startwert = Skalierung 1
For y = 200 To 400 Step 100
picList.Add(New MyPicture With {.Img = _
SystemIcons.Warning.ToBitmap, .Bounds = New Rectangle(220, y, 64, _
64)})
picList.Add(New MyPicture With {.Img = SystemIcons.Error.ToBitmap, _
.Bounds = New Rectangle(370, y, 64, 64)})
picList.Add(New MyPicture With {.Img = Me.Icon.ToBitmap, .Bounds = _
New Rectangle(520, y, 64, 64)})
Next
End Sub
Private Sub Pb_Paint(sender As Object, e As PaintEventArgs) Handles Pb.Paint
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(hgrund, 0, 0, Pb.Width, Pb.Height)
For Each pic In picList
Dim r As Rectangle = pic.Bounds
e.Graphics.DrawImage(pic.Img, New Rectangle(CInt(r.X * scalef.X), _
CInt(r.Y * scalef.Y),
CInt(r.Width * scalef.X), CInt( _
r.Height * scalef.Y)))
Next
End Sub
Dim scalef As New PointF(1.0, 1.0)
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
scalef = New PointF(CSng(Me.ClientSize.Width / 800), CSng( _
Me.ClientSize.Height / 800))
Pb.Invalidate()
End Sub
End Class
Public Class MyPicture
Public Img As Bitmap
Public Bounds As Rectangle
End Class MfG GPM | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 21.02.18 21:18 |
| ich habe es jetzt geschafft, dass wenn nur ein Bild geladen ist, sich der handler mit verschiebt. Aber jetzt lassen sich weitere Bilder nicht mehr bearbeiten... Kannst du mir vll nochmal helfen??
Gruß
Beitrag wurde zuletzt am 21.02.18 um 21:42:52 editiert. | |
Re: Transparente Bilder | | | Autor: dm1485 | Datum: 24.02.18 21:15 |
| Hi GMP,
wäre Klasse wenn du mir vll nochmal helfen könntest. Ich bekomme es einfach nicht hin.
DANKE | |
| Sie sind nicht angemeldet! Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.
Einloggen | Neu registrieren |
|
|
Neu! sevEingabe 3.0
Einfach stark!
Ein einziges Eingabe-Control für alle benötigten Eingabetypen und -formate, inkl. Kalender-, Taschenrechner und Floskelfunktion, mehrspaltige ComboBox mit DB-Anbindung, ImageComboBox u.v.m. Weitere InfosTipp des Monats TOP Entwickler-Paket
TOP-Preis!!
Mit der Developer CD erhalten Sie insgesamt 24 Entwickler- komponenten und Windows-DLLs. Die Einzelkomponenten haben einen Gesamtwert von 1605.50 EUR...
Jetzt nur 599,00 EURWeitere Infos
|