简体   繁体   English

使用Alpha通道绘制位图

[英]Draw Bitmap with alpha channel

I have a Format32bppArgb backbuffer, where I draw some lines: 我有一个Format32bppArgb缓冲区,在其中绘制了一些线:

var g = Graphics.FromImage(bitmap);
g.Clear(Color.FromArgb(0));
var rnd = new Random();
for (int i = 0; i < 5000; i++) {
    int x1 = rnd.Next(ClientRectangle.Left, ClientRectangle.Right);
    int y1 = rnd.Next(ClientRectangle.Top, ClientRectangle.Bottom);
    int x2 = rnd.Next(ClientRectangle.Left, ClientRectangle.Right);
    int y2 = rnd.Next(ClientRectangle.Top, ClientRectangle.Bottom);
    Color color = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));

    g.DrawLine(new Pen(color), x1, y1, x2, y2);
}

Now I want to copy bitmap in Paint event. 现在我想在Paint事件中复制bitmap I do it like this: 我这样做是这样的:

void Form1Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
}

Hovewer, the DrawImageUnscaled copies pixels and applies the alpha channel, thus pixels with alpha == 0 won't have any effect. 但是, DrawImageUnscaled复制像素并应用Alpha通道,因此Alpha == 0的像素将没有任何效果。 But I need raw byte copy, so pixels with alpha == 0 are also copied. 但是我需要原始字节复制,因此还复制了alpha == 0的像素。 So the result of these operations should be that e.Graphics contains exact byte-copy of the bitmap . 因此,这些操作的结果应该是e.Graphics包含bitmap精确字节副本。 How to do that? 怎么做?

Summary: When drawing a bitmap, I don't want to apply the alpha channel, I merely want to copy the pixels. 摘要:绘制位图时,我不想应用alpha通道,我只想复制像素。

在绘制图像之前,将Graphics.CompositingMode设置为CompositingMode.SourceCopy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM