发布网友 发布时间:2023-07-25 23:06
共1个回答
热心网友 时间:2024-12-04 14:56
该代码使用 Graphics 对象和两个半透明画笔 (alpha = 160) 在该位图上绘画。 该代码使用半透明画笔填充红色椭圆和绿色椭圆。 绿色椭圆与红色椭圆重叠,但是绿色并不与红色混合,这是因为 Graphics 对象的复合模式已设置为 SourceCopy。 该代码在屏幕上绘制该位图两次:一次是在白色背景上,一次是在多色背景上。 位图中属于两个椭圆的像素的 alpha 分量的值是 160,因此这些椭圆与屏幕上的背景色相混合。 下面的插图显示代码示例的输出。 请注意,这些椭圆与背景相混合,但椭圆之间不进行混合。 ' Create a blank bitmap. Dim myBitmap As New Bitmap(180, 100) ' Create a Graphics object that we can use to draw on the bitmap. Dim bitmapGraphics As Graphics = Graphics.FromImage(myBitmap) ' Create a red brush and a green brush, each with an alpha value of 160. Dim redBrush As New SolidBrush(Color.FromArgb(160, 255, 0, 0)) Dim greenBrush As New SolidBrush(Color.FromArgb(160, 0, 255, 0)) ' Set the compositing mode so that when we draw overlapping ellipses, ' the colors of the ellipses are not blended. bitmapGraphics.CompositingMode = CompositingMode.SourceCopy ' Fill an ellipse using a red brush that has an alpha value of 160. bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70) ' Fill a second ellipse using a green brush that has an alpha value of ' 160. The green ellipse overlaps the red ellipse, but the green is not ' blended with the red. bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70) 'Set the compositing quality of the form's Graphics object. e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected ' Draw a multicolored background. Dim colorBrush As New SolidBrush(Color.Aqua) e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100) colorBrush.Color = Color.Yellow e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100) colorBrush.Color = Color.Fuchsia e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100) 'Display the bitmap on a white background. e.Graphics.DrawImage(myBitmap, 0, 0) ' Display the bitmap on a multicolored background. e.Graphics.DrawImage(myBitmap, 200, 0) // Create a blank bitmap. Bitmap myBitmap = new Bitmap(180, 100); // Create a Graphics object that we can use to draw on the bitmap. Graphics bitmapGraphics = Graphics.FromImage(myBitmap); // Create a red brush and a green brush, each with an alpha value of 160. SolidBrush redBrush = new SolidBrush(Color.FromArgb(160, 255, 0, 0)); SolidBrush greenBrush = new SolidBrush(Color.FromArgb(160, 0, 255, 0)); // Set the compositing mode so that when we draw overlapping ellipses, // the colors of the ellipses are not blended. bitmapGraphics.CompositingMode = CompositingMode.SourceCopy; // Fill an ellipse using a red brush that has an alpha value of 160. bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70); // Fill a second ellipse using a green brush that has an alpha value of 160. // The green ellipse overlaps the red ellipse, but the green is not // blended with the red. bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70); // Set the compositing quality of the form's Graphics object. e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected; // Draw a multicolored background. SolidBrush colorBrush = new SolidBrush(Color.Aqua); e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100); colorBrush.Color = Color.Yellow; e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100); colorBrush.Color = Color.Fuchsia; e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100); // Display the bitmap on a white background. e.Graphics.DrawImage(myBitmap, 0, 0); // Display the bitmap on a multicolored background. e.Graphics.DrawImage(myBitmap, 200, 0);