C# 绘制透明图像不需要';行不通
我有一个PictureBox控件,它的背景色设置为透明。我想在此控件上绘制一幅图像(源代码:),但它的背景色不是透明背景,而是白色(不幸的是,paint不支持alpha通道) 以下是我如何尝试绘制位图:C# 绘制透明图像不需要';行不通,c#,graphics,drawing,transparent,C#,Graphics,Drawing,Transparent,我有一个PictureBox控件,它的背景色设置为透明。我想在此控件上绘制一幅图像(源代码:),但它的背景色不是透明背景,而是白色(不幸的是,paint不支持alpha通道) 以下是我如何尝试绘制位图: private void DrawPictureBox() { pbScreen.Image = Update(); } private Bitmap CreateBackgroundBitmap() { Bitmap bitmap = (Bitmap)Properties.
private void DrawPictureBox()
{
pbScreen.Image = Update();
}
private Bitmap CreateBackgroundBitmap()
{
Bitmap bitmap = (Bitmap)Properties.Resources.ResourceManager.GetObject("empty");
bitmap.MakeTransparent(Color.White);
return bitmap;
}
private ImageAttributes GetImageAttributes()
{
float[][] matrixItems = {
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, Contrast, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
return imageAtt;
}
private void DrawSegment(ref Graphics g, Digit digit, int Position = 0)
{
if (digit == null)
return;
Bitmap buffer = digit.CreateBitmapFromFile(digit.CreateFileNameFromContent());
buffer.MakeTransparent(Color.White);
g.DrawImage(buffer, new Rectangle(0 + Position * 43, 0, 43, 67), 0.0f, 0.0f, 43, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
}
public Bitmap Update()
{
Bitmap buffer = CreateBackgroundBitmap();
Graphics g = Graphics.FromImage(buffer);
g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
if (State == Powerstate.on)
{
// Draw segments
for (int i = 0; i < digits.Count(); i++)
{
DrawSegment(ref g, digits[i], i);
if (digits[i]?.Dot == Dot.dot_on)
{
DrawPoint(ref g, digits[i], i);
}
}
}
return buffer;
}
以前
g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
在Update()方法中,我的图形“g”是黑色的——不管我是保留还是删除
bitmap.MakeTransparent(Color.White);
在“CreateBackgroundBitmap()”中,但在黑色背景上绘制片段的效果与预期一样
有人知道问题在哪里吗?我错过了什么
非常感谢:)好的,我知道了。我想我不明白图形对象是如何与位图交互的,而位图是由图形对象构成的 以下是必须改变的方式:
public Bitmap Update()
{
Bitmap background = new Bitmap(301, 67);
Graphics g = Graphics.FromImage(background);
g.Clear(color: Color.White);
Bitmap buffer = CreateBackgroundBitmap();
g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
if (State == Powerstate.on)
{
for (int i = 0; i < digits.Count(); i++)
{
DrawSegment(ref g, digits[i], i);
if (digits[i]?.Dot == Dot.dot_on)
{
DrawPoint(ref g, digits[i], i);
}
}
}
return background;
}
公共位图更新()
{
位图背景=新位图(301,67);
Graphics g=Graphics.FromImage(背景);
g、 清晰(颜色:彩色。白色);
位图缓冲区=CreateBackgroundBitmap();
g、 DrawImage(缓冲区,新矩形(0,0,301,67),0.0f,0.0f,301,67,GraphicsUnit.Pixel,this.GetImageAttributes());
如果(状态==Powerstate.on)
{
对于(int i=0;i
我明白了。我想我不明白图形对象是如何与位图交互的,而位图是由图形对象构成的
以下是必须改变的方式:
public Bitmap Update()
{
Bitmap background = new Bitmap(301, 67);
Graphics g = Graphics.FromImage(background);
g.Clear(color: Color.White);
Bitmap buffer = CreateBackgroundBitmap();
g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());
if (State == Powerstate.on)
{
for (int i = 0; i < digits.Count(); i++)
{
DrawSegment(ref g, digits[i], i);
if (digits[i]?.Dot == Dot.dot_on)
{
DrawPoint(ref g, digits[i], i);
}
}
}
return background;
}
公共位图更新()
{
位图背景=新位图(301,67);
Graphics g=Graphics.FromImage(背景);
g、 清晰(颜色:彩色。白色);
位图缓冲区=CreateBackgroundBitmap();
g、 DrawImage(缓冲区,新矩形(0,0,301,67),0.0f,0.0f,301,67,GraphicsUnit.Pixel,this.GetImageAttributes());
如果(状态==Powerstate.on)
{
对于(int i=0;i