halbtransparente Bitmaps (Opacity)

  • VB.NET

Es gibt 19 Antworten in diesem Thema. Der letzte Beitrag () ist von Mad Andy.

    halbtransparente Bitmaps (Opacity)

    Hi,

    ich versuche (verzweifelt) eine Lösung für das Darstellen von Bitmaps die "halb-transparent" dargestellt werden sollen.
    Es geht mir hier nicht um die Transparent einer bestimmten Farbe o.ä. sonder wirklich um die opacity (wie heisst das eigentlich auf deutsch ??? Durchsichtigkeit ???) wie es auch in Formen gibt.

    Irgendjemand eine Idee für mich ?

    Grüße
    Samu
    Hallo Andy,

    Du bist wirklich ein Lichtblick :)

    Ich war schon am verzweifeln aber jetzt weiss ich wie ich die Sache angehen kann. Das war wirklich DER Tipp !

    Ich hoffe, ich kann wieder auf Deine Hilfe zurückgreifen bei meinem nächsten (für Dich Problemchen) Problem.

    Habe 1000 Dank große Kaiser des VB.

    Dein unwürdiger Diener
    Samu
    Hi,

    ich nochmal...

    Habe soweit alles jetzt im Griff, jetzt stehe ich nur auf dem Schlauch, weil ich keine externe Grafik sonder lieber die PictureBox benutzen möchte.

    Kann mir jemand noch sagen wie ich den Code in Zeile 4 ändern muss um auf eine PictureBox (ist dann logischerweise local resource) zugreifen kann ?

    VB.NET-Quellcode

    1. Dim g As Graphics = Me.CreateGraphics()
    2. g.Clear(Me.BackColor)
    3. Dim bitmap As New Bitmap("")
    4. Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0.5F, 0}, New Single() {0, 0, 0, 0, 1}}
    5. Dim clrMatrix As New ColorMatrix(ptsArray)
    6. Dim imgAttributes As New ImageAttributes
    7. imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    8. g.DrawImage(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imgAttributes)
    9. g.Dispose()


    schöne Grüße
    Samu
    Hi!

    Einfach das Bitmap Objekt durch ein Image austauschen (Bitmap erbt von Image) und auf PictureBox1.Image referenzieren lassen:

    VB.NET-Quellcode

    1. Dim bitmap as Image = PictureBox1.Image


    oder hald überall statt bitmap PictureBox1.Image angeben, find ich aber nicht so edel ;)


    mfG Andy
    Hi,

    sooooo ich nochmal...
    Nach 8 Std. rumexperimentieren und googeln gebe ich es einfach auf :(

    Habe ja alles wunderbar schon implementiert und war hell begeistert, daß alles jetzt so perfekt ist. Pustekuchen !

    Erkläre mal mein (doppeltes) Problem :
    In der Form wo die halb transparenten Bitmaps dargestellt werden, habe ich ein Backgroundimage und die Bitmaps sollten skaliert werden je nach Userangabe.

    Tue ich also

    VB.NET-Quellcode

    1. g.Clear(Color.Transparent)

    macht es *kabuuuuuuf* abrakadabra hokus pokus und mein Hintergrundbild verschwindet in Nirvana. Toll !!!

    Habe mir also gedacht (ja ja ich weiß sollte es wohl aufgeben) mache ich es doch so :

    VB.NET-Quellcode

    1. Dim g As Graphics = Me.CreateGraphics()
    2. Dim bitmap As Image = PictureBox1.Image
    3. Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0.5F, 0}, New Single() {0, 0, 0, 0, 1}}
    4. Dim clrMatrix As New ColorMatrix(ptsArray)
    5. Dim imgAttributes As New ImageAttributes
    6. If dummy = 0 Then
    7. imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    8. test = New Rectangle(92, 320 - PictureBox1.Height, bitmap.Width, PictureBox1.Height)
    9. g.DrawImage(bitmap, test, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imgAttributes)
    10. dummy = 1
    11. End If
    12. If dummy = 2 Then
    13. imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    14. test.height = 20
    15. g.DrawImage(bitmap, test, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imgAttributes)
    16. dummy = 1
    17. End If


    doch 'nu bleibt mir immer die "alte" Grafik einfach mal da stehen und will nicht weeech... Böse Grafik...

    In dem Beispiel was Andy mir gezeigt hat, wird die Sache mit

    VB.NET-Quellcode

    1. Me.Invalidate()

    erledigt, bringt aber einen unschönen Effekt mit sich (Flimmern).

    Hilfeeee....

    schöne Grüße
    Samu
    Hi!

    Das ganze müsste eigentlich so gehen:
    Du Löscht das hineingeladene Bild, dann rufst du den Code nochmal auf nur statt

    VB.NET-Quellcode

    1. Dim bitmap As Image = PictureBox1.Image
    verwendest du

    VB.NET-Quellcode

    1. Dim ResultWidth, ResultHeight as Long 'die skalierte Größe
    2. ResultWidth = ....
    3. ResultHeight = ....
    4. Dim bitmap as Image = new Bitmap(ResultWidth, Resultheight)
    5. Dim gfx Graphics = Graphics.FromImage(bitmap)
    6. gfx.DrawImage(picturebox1.Image, New Rectangle (0,0,ResultWidth, ResultHeight), _
    7. 0,0,picturebox1.Image.Width, picturebox1.Image.Height,GraphicsUnit.Pixel, nothing)
    8. 'Der restliche code...


    mfG Andy

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Mad Andy“ ()

    Hi Andy,

    vielen Dank für die Antwort... leider bekomme ich das "Ding" nicht ans laufen :( *omg*

    schaue mal :

    VB.NET-Quellcode

    1. 'Dim g As Graphics = Me.CreateGraphics()
    2. Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0.2F, 0}, New Single() {0, 0, 0, 0, 1}}
    3. Dim clrMatrix As New ColorMatrix(ptsArray)
    4. Dim imgAttributes As New ImageAttributes
    5. imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    6. 'Dim bitmap As Image = PictureBox1.Image
    7. Dim ResultWidth, ResultHeight As Integer 'die skalierte Größe
    8. ResultWidth = PictureBox1.Width
    9. ResultHeight = 50
    10. Dim bitmap As Image = New Bitmap(ResultWidth, ResultHeight)
    11. Dim gfx As Graphics = Graphics.FromImage(bitmap)
    12. gfx.DrawImage(PictureBox1.Image, New Rectangle(0, 0, ResultWidth, ResultHeight), _
    13. 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height, GraphicsUnit.Pixel, imgAttributes)


    so habe ich es angepasst und nun kann man wenigstens gar nichts mehr sehen :(:(:(

    Ich glaube ich gebe es langsam wirklich auf...
    Es seit dem Du hast noch die rettende Idee für mich.

    Grüße
    Samu
    Hi Andy,

    also doch so :

    VB.NET-Quellcode

    1. Dim g As Graphics = Me.CreateGraphics()
    2. 'Dim bitmap As Image = PictureBox1.Image
    3. Dim ResultWidth, ResultHeight As Integer 'die skalierte Größe
    4. ResultWidth = PictureBox1.Width
    5. ResultHeight = PictureBox1.Height
    6. Dim bitmap As Image = New Bitmap(ResultWidth, ResultHeight)
    7. Dim gfx As Graphics = Graphics.FromImage(bitmap)
    8. gfx.DrawImage(PictureBox1.Image, New Rectangle(0, 0, ResultWidth, ResultHeight), _
    9. 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height, GraphicsUnit.Pixel, Nothing)
    10. Dim ptsArray As Single()() = {New Single() {1, 0, 0, 0, 0}, New Single() {0, 1, 0, 0, 0}, New Single() {0, 0, 1, 0, 0}, New Single() {0, 0, 0, 0.2F, 0}, New Single() {0, 0, 0, 0, 1}}
    11. Dim clrMatrix As New ColorMatrix(ptsArray)
    12. Dim imgAttributes As New ImageAttributes
    13. imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    14. g.DrawImage(bitmap, New Rectangle(92, 119, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imgAttributes)


    klappt auch nicht, weil die "alte" Grafik immer noch stehen bleibt...

    Grüße
    Samu
    Hi!

    sry... hab da was falsch bedacht :D
    gfx ist ja "mein" Grafik-Objekt...
    Du musst das, das der Form (auf dsa du ja letztendlich zeichnest) löschen...
    das heißt in deinem Code "g". Also g.Clear(Color.Transperent) bzw. g.Clear(Me.BackColor)

    Du solltest die Namensgebung vielleicht etwas anpassen gScaled und gForm oder so ;)


    mfG Andy
    Hi Andy,

    *lol*... Das Thema hatten wir schon doch...

    Beide Möglichkeiten zaubern mein Hintergrundbild einfach weg...

    Gibt es nicht in VB .NET die Möglichkeit ein Layer zu setzen ? Damit könnte ich es vielleich dann anders lösen...

    schöne Grüße
    Samu
    Was ist denn bitte ein Layer?

    Versuch mal das ganze nicht auf eine Form zeichnen zu lassen, sondern in eine weitere Picturebox.
    Das wär dann Dim g As Graphics = Graphics.FromImage(Picturebox2.Image) statt Dim g As Graphics = Me.CreateGraphics()


    mfG Andy
    Ein Layer ist eine zstl. Ebene das ich dann entsprechend über eine Form legen könnte und damit machen was ich will, ohne das der Hintergrund in Mitleideschaft gezogen wird.

    Die Definition lt. ungefähr so : "ein Layer eine Ebene oder Schicht eines Bildes, die unabhängig von anderen Teilen bearbeitet werden kann"

    Das mit Dim g As Graphics = Graphics.FromImage(Picturebox2.Image) habe ich gerade ausprobiert, klappt überhaupt nicht :( Da wird nichtmal mein halbtransparentes Bitmap dort dargestellt :(


    schöne Grüße
    Samu
    Hi!

    Achso...
    du meinst die Layer / Ebenen, wie man sie aus Photoshop & Co. kennt.

    erm... ne... sry...
    Der oben gepostete Link ist die einzig mir bekannte Möglichkeit.
    Wenn ich mal Zeit hab kann ich ja mal mein Glück versuchen ;)

    Heut wird da aber leider nix draus -.-


    mfG Andy