Web ページに表示する背景が透明な画像を作成しようとしています。
いくつかのテクニックを試しましたが、背景は常に黒です。
透明な画像を作成し、その上に線を引くにはどうすればよいですか?
33451 次
2 に答える
39
呼び出しGraphics.Clear(Color.Transparent)
て、まあ、画像をクリアします。アルファ チャネルを持つピクセル形式で作成することを忘れないでくださいPixelFormat.Format32bppArgb
。このような:
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);
}
あなたがであるusing
System.Drawing
と仮定しますSystem.Drawing.Imaging
。
編集:実際には必要ないようですClear()
。アルファ チャネルを使用して画像を作成するだけで、空白の (完全に透明な) 画像が作成されます。
于 2009-04-02T09:00:21.570 に答える
0
これは役立つかもしれません(Windowsフォームの背景を透明な画像に設定するために私が一緒に投げたもの:
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;
}
于 2009-04-02T09:12:13.460 に答える