我嘗試使用BufferedGraphics進行雙緩衝。當我使用BufferedGraphics.Render方法將我的圖像背景變爲黑色時。這裏的簡單代碼,說明我的問題Bufferedgraphics將背景更改爲黑色c#
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e) {
Paint += new PaintEventHandler(Form1_Paint);
}
private void print(Bitmap image, PaintEventArgs e) {
Graphics graphicsObj = e.Graphics;
graphicsObj.DrawImage(image, 60, 10);
graphicsObj.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Rectangle rect = Screen.PrimaryScreen.Bounds;
PixelFormat pf;
pf = PixelFormat.Format32bppArgb;
Bitmap image = new Bitmap(rect.Width, rect.Height, pf);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Orange);
BufferedGraphicsContext context = new BufferedGraphicsContext();
BufferedGraphics buffer = context.Allocate(g, new Rectangle(0, 0, rect.Width + 20, rect.Height + 20));
buffer.Render(g);
print(image, e);
}
}
我希望看到我的屏幕上的橙色矩形,但它是黑色的。我不明白爲什麼會發生這種情況。請幫助我:)
我可以更改緩衝區內容的背景嗎?在我的工作項目中,我使用緩衝區中的透明背景添加圖像,並且當我將其渲染到圖形背景時更改爲黑色 – Alex
謝謝您的回答。這是幫助 – Alex