GDI+: fondere due immagini rendendole semitrasparenti

image

Per disegnare un'immagine semitrasparente è necessario passare un opportuno ImageAttributes al metodo DrawImage della Classe System.Drawing.Image.

L'ImageAttributes dovrà utilizzare una matrice di colori (ColorMatrix) nella quale dovrà essere specificato il valore di trasparenza desiderato.

 

    ' VB.
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        ' Imposta il livello di trasparenza.
        Dim opacity As Single = CSng(Me.TrackBar1.Value / 10)

        ' Crea una ColorMatrix di 5x5 con il valore di trasparenza impostato.
        Dim colMatrix As New Imaging.ColorMatrix(New 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, opacity, 0}, _
                                                        New Single() {0, 0, 0, 0, 1}})

        ' Crea un oggetto ImageAttributes e gli passa la ColorMatrix.
        Dim imgAttr As New Imaging.ImageAttributes
        imgAttr.SetColorMatrix(colMatrix, Imaging.ColorMatrixFlag.Default, _
                                                        Imaging.ColorAdjustType.Bitmap)

        ' Carica l'immagine di sfondo.
        Dim img1 As Image = Image.FromFile("C:\Dnw.png")

        ' Carica l'immagine in primo piano.
        Dim img2 As New Bitmap("C:\Net.png")

        ' Disegna l'immagine utilizzando l'ImageAttribute specificato.
        Dim gr As Graphics = Me.Panel3.CreateGraphics
        gr.DrawImage(img1, 0, 0)
        gr.DrawImage(img2, New Rectangle(0, 0, Me.Panel3.Width, _
                                                        Me.Panel3.Height), 0, 0, _
                                                        Me.Panel3.Width, Me.Panel3.Height, _
                                                        GraphicsUnit.Pixel, imgAttr)
        gr.Dispose()
    End Sub
// C#. 
private void //TrackBar1_Scroll(object sender, System.EventArgs e) 
{ 
    // Imposta il livello di trasparenza. 
    float opacity = (float)this.TrackBar1.Value / 10; 
    
    // Crea una ColorMatrix di 5x5 con il valore di trasparenza impostato. 
    Imaging.ColorMatrix colMatrix = new Imaging.ColorMatrix(new float[][] { new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, opacity, 0 }, new float[] { 0, 0, 0, 0, 1 } }); 
    
    // Crea un oggetto ImageAttributes e gli passa la ColorMatrix. 
    Imaging.ImageAttributes imgAttr = new Imaging.ImageAttributes(); 
    imgAttr.SetColorMatrix(colMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap); 
    
    // Carica l'immagine di sfondo. 
    Image img1 = Image.FromFile("C:\\Dnw.png"); 
    
    // Carica l'immagine in primo piano. 
    Bitmap img2 = new Bitmap("C:\\Net.png"); 
    
    // Disegna l'immagine utilizzando l'ImageAttribute specificato. 
    Graphics gr = this.Panel3.CreateGraphics; 
    gr.DrawImage(img1, 0, 0); 
    gr.DrawImage(img2, new Rectangle(0, 0, this.Panel3.Width, this.Panel3.Height), 0, 0, this.Panel3.Width, this.Panel3.Height, GraphicsUnit.Pixel, imgAttr); 
    gr.Dispose(); 
} 

 

Nel prossimo post vederemo come rendere trasparente un solo colore di un'immagine.

 

Tag Cloud: ,

Print | posted on domenica 19 ottobre 2008 15.50

Feedback

No comments posted yet.

Your comment:





 
Please add 1 and 7 and type the answer here:

Copyright © Andrea Zingoni

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski