2009-04-02 15 views

Odpowiedz

37

Zadzwoń pod Graphics.Clear(Color.Transparent), aby dobrze wyczyścić obraz. Nie zapomnij utworzyć go w formacie pikseli, który ma kanał alfa, np. PixelFormat.Format32bppArgb. Tak:

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); 
} 

Zakłada jesteś usingSystem.Drawing i System.Drawing.Imaging.

Edytuj: Wygląda na to, że nie potrzebujesz Clear(). Po prostu tworzenie obrazu z kanałem alfa tworzy pusty (całkowicie przezroczysty) obraz.

+0

Chyba brakowało przeciążenie konstruktora Bitmap Niestety, nie mają Kod dostępny już teraz, spróbuję tego wieczoru ... –

+0

Zaufaj mi, działa, próbowałem;) – OregonGhost

+0

Było trochę więcej niż to, co powiedziałeś, ale zrobiłem trochę badań i dostałem go do pracy . Dzięki. –

0

To może pomóc (coś wrzuciłem razem który ustawia tło formularza systemu Windows do przejrzystego obrazu.

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

To zbyt skomplikowane i nie trzeba tego robić w ten sposób :) – nXqd

Powiązane problemy