2009-04-02 32 views

Respuesta

37

Llame Graphics.Clear(Color.Transparent) a, bien, borre la imagen. No olvides crearlo con un formato de píxel que tenga un canal alfa, p. PixelFormat.Format32bppArgb. De esta manera:

var image = new Bitmap(135, 135, PixelFormat.Format32bppArgb); 
using (var g = Graphics.FromImage(image)) { 
    g.Clear(Color.Transparent); 
    g.DrawLine(Pens.Red, 0, 0, 135, 135); 
} 

supone que usted esta usingSystem.Drawing y System.Drawing.Imaging.

Editar: Parece que en realidad no necesita el Clear(). Solo crear la imagen con un canal alfa crea una imagen en blanco (completamente transparente).

+0

supongo que perdí la sobrecarga en el constructor de mapa de bits Por desgracia, no tengo la código disponible en este momento, lo intentaré esta noche ... –

+0

Créeme, funciona, lo probé;) – OregonGhost

+0

Había un poco más de lo que le decías, pero investigé un poco y lo hice funcionar . Gracias. –

0

Esto podría ayudar a (algo me tiró juntos, lo que produce el fondo de un formulario de Windows a una imagen transparente:.

private void TestBackGround() 
    { 
     // Create a red and black bitmap to demonstrate transparency.    
     Bitmap tempBMP = new Bitmap(this.Width, this.Height); 
     Graphics g = Graphics.FromImage(tempBMP); 
     g.FillEllipse(new SolidBrush(Color.Red), 0, 0, tempBMP.Width, tempBMP.Width); 
     g.DrawLine(new Pen(Color.Black), 0, 0, tempBMP.Width, tempBMP.Width); 
     g.DrawLine(new Pen(Color.Black), tempBMP.Width, 0, 0, tempBMP.Width); 
     g.Dispose(); 


     // Set the transparancy key attributes,at current it is set to the 
     // color of the pixel in top left corner(0,0) 
     ImageAttributes attr = new ImageAttributes(); 
     attr.SetColorKey(tempBMP.GetPixel(0, 0), tempBMP.GetPixel(0, 0)); 

     // Draw the image to your output using the transparancy key attributes 
     Bitmap outputImage = new Bitmap(this.Width,this.Height); 
     g = Graphics.FromImage(outputImage); 
     Rectangle destRect = new Rectangle(0, 0, tempBMP.Width, tempBMP.Height); 
     g.DrawImage(tempBMP, destRect, 0, 0, tempBMP.Width, tempBMP.Height,GraphicsUnit.Pixel, attr); 


     g.Dispose(); 
     tempBMP.Dispose(); 
     this.BackgroundImage = outputImage; 

    } 
+0

Es demasiado complejo y no es necesario hacerlo de esta manera :) – nXqd

Cuestiones relacionadas