Mehreren Picturenboxen ein Bild Zuweisen

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von -Franky-.

    Mehreren Picturenboxen ein Bild Zuweisen

    Moin Zusammen, ich habe mir mehrere Pictureboxen Dynamisch erstellt und möchte diesen nun ein Bild geben (.dds, mithilfe der WIC .DDS Datei Laden (DevIL))

    Nur weiß ich nicht genau wie ich ein Control auch zuweisen kann.

    Die WIC läuft mithilfe eines Sub´s die mit einem ByVal Parameter übergeben

    Erstellen der Picturebox:

    VB.NET-Quellcode

    1. For i As Integer = 1 To 10 Step 1​
    2. PBI = New PictureBox
    3. PanelI.Controls.Add(PBI)
    4. With PBI
    5. .Name = i & "_Picturebox"
    6. .Dock = DockStyle.Left
    7. .BackColor = Color.Blue
    8. .Width = 160
    9. .BringToFront()
    10. .Image = LoadDDS(PFAD DER DATEI, 'Hier soll die Picturebox genannt werden z.B. 1_PB)
    11. End With
    12. Next


    Image Funktion laden:

    Siehe: .DDS Datei Laden (DevIL)
    Einzige Änderung:

    VB.NET-Quellcode

    1. Public Sub LoadDDS(ByVal DDSPath As String, ByVal PictureboxX As ???) '<- Hier soll die Zuständige Picturebox Zugewiesen werden
    2. Dim strPicture As String = DDSPath


    Und

    VB.NET-Quellcode

    1. PictureboxX = bmp​


    Auf Google hab ich bislang nur Gefunden wie man mehrere Bilder in einer Picturebox Lädt. Aber das benötige ich nicht...

    Danke im Voraus.

    Hi

    Ich würde in der Schleife nicht jedes mal das gleiche Bild neu laden und der PB zuweisen. Lade das Bild einmal in eine Variable und in der Schleife weist dann den PBs die Variable als Image zu.

    Pseudocode
    Using dds As Bitmap = LoadDDS(Pfad)
    For x.....
    PBI.Image = New Bitmap(dds)
    Next
    End Using
    Mfg -Franky-

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „-Franky-“ ()

    Und wie soll ich der WCI Sagen welche Picturebox es ist?

    Hab jetzt das hier Versucht, aber will nicht.


    VB.NET-Quellcode

    1. Public Sub LoadDDS(ByVal DDSPath As String) '<- Hier soll die Zuständige Picturebox Zugewiesen werden
    2. Dim strPicture As String = DDSPath
    3. Dim intWidth As Integer = 0
    4. Dim intHeight As Integer = 0
    5. Dim intBuffSize As Integer = 0
    6. Dim intFrameCount As Integer = 0
    7. Dim tWICRect As WICRect = New WICRect
    8. Dim pIWICLock As IntPtr = IntPtr.Zero
    9. Dim pIWICBitmap As IntPtr = IntPtr.Zero
    10. Dim pIWICDecoder As IntPtr = IntPtr.Zero
    11. Dim pIImagingFactory As IntPtr = IntPtr.Zero
    12. Dim pIWICBitmapFrame As IntPtr = IntPtr.Zero
    13. Dim pIWICFormatConverter As IntPtr = IntPtr.Zero
    14. Dim pData As IntPtr = IntPtr.Zero
    15. ' WIC starten
    16. If CreateImagingFactory(WINCODEC_SDK_VERSION2, pIImagingFactory) = S_OK Then
    17. ' ein Decoder vom Bild erstellen
    18. If CreateDecoderFromFilename(pIImagingFactory, strPicture, IntPtr.Zero,
    19. WICAccessOption.GENERIC_READ,
    20. WICDecodeOptions.WICDecodeMetadataCacheOnLoad,
    21. pIWICDecoder) = S_OK Then
    22. ' Anzahl der Frames ermitteln
    23. If GetFrameCount(pIWICDecoder, intFrameCount) = S_OK Then
    24. ' mindestens ein Frame vorhanden?
    25. If intFrameCount > 0 Then
    26. ' erstes Frame laden
    27. If GetFrame(pIWICDecoder, 0, pIWICBitmapFrame) = S_OK Then
    28. ' ein FormatConverter erstellen
    29. If CreateFormatConverter(pIImagingFactory, pIWICFormatConverter) = S_OK Then
    30. ' FormatConverter initialisieren -> PixelFormat32bppBGRA
    31. If InitializeFormatConverter(pIWICFormatConverter, pIWICBitmapFrame,
    32. GUID_WICPixelFormat32bppBGRA,
    33. WICBitmapDitherType.WICBitmapDitherTypeNone,
    34. IntPtr.Zero, 0,
    35. WICBitmapPaletteType.WICBitmapPaletteTypeMedianCut) = S_OK Then
    36. ' ein WIC-Bitmap vom FormatConverter erstellen
    37. If CreateBitmapFromSource(pIImagingFactory, pIWICFormatConverter,
    38. WICBitmapCreateCacheOption.WICBitmapCacheOnLoad, pIWICBitmap) = S_OK Then
    39. ' Dimensionen der WIC-Bitmap ermitteln
    40. If GetSize(pIWICBitmap, intWidth, intHeight) = S_OK Then
    41. With tWICRect
    42. .X = 0
    43. .Y = 0
    44. .Width = intWidth
    45. .Height = intHeight
    46. End With
    47. ' WIC-Bitmap zum auslesen der Pixeldaten sperren
    48. If WicLockBits(pIWICBitmap, tWICRect, WICBitmapLockFlags.WICBitmapLockRead, pIWICLock) = S_OK Then
    49. ' Pointer auf die Pixeldaten und Größe der Pixeldaten ermitteln
    50. If GetDataPointer(pIWICLock, intBuffSize, pData) = S_OK Then
    51. ' entsprechend ein ByteArray dimensionieren
    52. Dim arrData As Byte() = New Byte(intBuffSize - 1) {}
    53. ' Pixeldaten vom Pointer in das ByteArray kopieren
    54. Marshal.Copy(pData, arrData, 0, intBuffSize)
    55. ' neues Bitmap in gleicher Größe und Pixelformat wie das WIC-Bitmap erstellen
    56. Dim bmp As Bitmap = New Bitmap(intWidth, intHeight, PixelFormat.Format32bppArgb)
    57. ' Bitmap zum schreiben der Pixeldaten sperren
    58. Dim bmpdata As BitmapData = bmp.LockBits(New Rectangle(0, 0, intWidth, intHeight),
    59. ImageLockMode.WriteOnly, bmp.PixelFormat)
    60. ' Pixeldaten aus dem ByteArray in das Bitmap schreiben
    61. Marshal.Copy(arrData, 0, bmpdata.Scan0, intBuffSize)
    62. ' Sperre der Bitmap aufheben
    63. bmp.UnlockBits(bmpdata)
    64. ' Bild ausgeben
    65. 'For Each C As Panel In FlowLayoutPanel1.Controls
    66. ' For Each TB As Control In C.Controls
    67. ' If TB.GetType() Is GetType(PictureBox) Then
    68. ' TB.Name = bmp
    69. ' End If
    70. ' Next
    71. 'Next
    72. For Each C As Panel In FlowLayoutPanel1.Controls
    73. For Each TB As Control In C.Controls
    74. If TB.GetType() Is GetType(PictureBox) Then
    75. TB.BackgroundImage = bmp
    76. End If
    77. Next
    78. Next
    79. ' PictureboxX = bmp
    80. End If
    81. ' Sperre der WIC-Bitmap aufheben
    82. Marshal.Release(pIWICLock)
    83. End If
    84. End If
    85. ' WIC-Bitmap löschen
    86. Marshal.Release(pIWICBitmap)
    87. End If
    88. ' FormatConverter löschen
    89. Marshal.Release(pIWICFormatConverter)
    90. End If
    91. End If
    92. ' geladenes Frame löschen
    93. Marshal.Release(pIWICBitmapFrame)
    94. End If
    95. End If
    96. End If
    97. ' Decoder löschen
    98. Marshal.Release(pIWICDecoder)
    99. End If
    100. ' WIC beenden
    101. Marshal.Release(pIImagingFactory)
    102. End If
    103. ' Next
    104. End Sub

    Hi

    So wie Du Dein Problem beschreibst, wird für jede PB das gleiche Bild verwendet. Dann zunächst aus der Sub eine Funktion machen Die Dir bmp zurück gibt (Code auf das wichtigste eingekürzt).

    VB.NET-Quellcode

    1. Private Function LoadDDS(ByVal DDSPath As String) As Bitmap
    2. Dim intWidth As Integer = 0
    3. Dim intHeight As Integer = 0
    4. ' restliche Dim
    5. Dim bmp As Bitmap = Nothing ' <- neu
    6. ' WIC starten
    7. If CreateImagingFactory(WINCODEC_SDK_VERSION2, pIImagingFactory) = S_OK Then
    8. ' weiterer Code
    9. ' neues Bitmap in gleicher Größe und Pixelformat wie das WIC-Bitmap erstellen
    10. bmp = New Bitmap(intWidth, intHeight, PixelFormat.Format32bppArgb) ' <- ändern
    11. ' weiterer Code
    12. ' Bild ausgeben
    13. ' PictureBox1.Image = bmp ' <- auskommentieren
    14. ' weiterer Code
    15. ' WIC beenden
    16. Marshal.Release(pIImagingFactory)
    17. End If
    18. Return bmp ' <- bmp zurück geben
    19. End Function

    Deinen Code müsstest dann entsprechend anpassen.

    VB.NET-Quellcode

    1. ' Bild einmal laden
    2. Dim dds As Bitmap = LoadDDS(PFAD DER DATEI)
    3. For i As Integer = 1 To 10 Step 1 ' <- Step 1 brauchst Du hier nicht. Nur wenn Step > 1 oder < 0 ist
    4. PBI = New PictureBox
    5. PanelI.Controls.Add(PBI)
    6. With PBI
    7. .Name = i.ToString & "_Picturebox" ' <- i zu einem String konvertieren
    8. .Dock = DockStyle.Left
    9. .BackColor = Color.Blue
    10. .Width = 160
    11. .BringToFront()
    12. .Image = dds ' <- geladenes Bild zuweisen
    13. End With
    14. Next

    Bei CreateDecoderFromFilename kannst auch gleich DDSPath anstelle von strPicture einsetzen. Zu bedenken wäre, wenn DDSPath einen ungültigen Pfad zu einem Bild enthält, liefert Dir die Function LoadDDS Nothing zurück. Evtl. vorher noch den Pfad prüfen ob der Pfad zum Bild auch vorhanden ist. Das aber nur optional.
    Mfg -Franky-