gpt4 book ai didi

VB.Net BufferedGraphics透明背景

转载 作者:行者123 更新时间:2023-12-02 01:35:02 25 4
gpt4 key购买 nike

我正在使用 BufferedGraphics 绘制一些内容,然后将其绘制到用户控件上。这是代码:

Private context As BufferedGraphicsContext
Private grafx As BufferedGraphics

Private Sub PaintDoc_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.UpdateStyles()
context = BufferedGraphicsManager.Current
context.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
grafx = context.Allocate(Me.CreateGraphics, New Rectangle(0, 0, Me.Width, Me.Height))
grafx.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
grafx.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
grafx.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
grafx.Graphics.Clear(Color.Transparent)
End Sub

Private Sub PaintDoc_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
grafx.Graphics.FillEllipse(New SolidBrush(DrawColorFinal), CInt(e.x - (BrushWidth / 2)), CInt(e.y - (BrushWidth / 2)), BrushWidth, BrushWidth)
Me.Invalidate()
End If
End Sub

Private Sub PaintDoc_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
grafx.Render(e.Graphics)
End Sub

问题是grafx没有应有的透明背景,而是黑色背景。有什么想法吗?

更新:

感谢 Hans Passant 解决了问题,下面是支持快速多层图像绘制的最终代码:

Public Class PaintDoc
Public backBuffer(1) As Bitmap
Public bufferGraphics(1) As Graphics
Public layerIndex As Integer = 0

Private Sub PaintDoc_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()
For i As Integer = 0 To backBuffer.Length - 1
backBuffer(i) = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
bufferGraphics(i) = Graphics.FromImage(backBuffer(i))
bufferGraphics(i).InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
bufferGraphics(i).PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
bufferGraphics(i).SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Next
End Sub

Private Sub PaintDoc_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
bufferGraphics(layerIndex).FillEllipse(New SolidBrush(DrawColorFinal), CInt(e.X - (BrushWidth / 2)), CInt(e.Y - (BrushWidth / 2)), BrushWidth, BrushWidth)
Me.Invalidate()
End If
End Sub

Private Sub PaintDoc_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.Clear(Color.Transparent)
For i As Integer = 0 To backBuffer.Length - 1
e.Graphics.DrawImage(backBuffer(i), Point.Empty)
Next
End Sub

Private Sub PaintDoc_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
For i As Integer = 0 To backBuffer.Length - 1
Dim newBuffer As Bitmap = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
If backBuffer(i) IsNot Nothing Then
Using gr As Graphics = Graphics.FromImage(newBuffer)
gr.DrawImage(backBuffer(i), Point.Empty)
End Using
backBuffer(i) = Nothing
End If
backBuffer(i) = newBuffer
Next
End Sub
End Class

最佳答案

从 BufferedGraphics 获得的缓冲区没有用,它的像素格式错误。您想要创建自己的缓冲区,一个可以支持透明度的缓冲区。这需要 32bpp 格式。将其设为 Format32bppPArgb 以使其尽可能快。重写此代码,添加一些额外的支持来调整表单大小并消除所有闪烁:

Public Class Form1

Private backBuffer As Bitmap

Public Sub New()
InitializeComponent()
Me.DoubleBuffered = True
Me.ResizeRedraw = True
End Sub

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
Dim newBuffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
If backBuffer IsNot Nothing Then
Using gr As Graphics = Graphics.FromImage(newBuffer)
gr.DrawImage(backBuffer, Point.Empty)
End Using
backBuffer.Dispose()
End If
backBuffer = newBuffer
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If e.Button = Windows.Forms.MouseButtons.Left Then
Using gr As Graphics = Graphics.FromImage(backBuffer)
gr.FillEllipse(New SolidBrush(Color.Blue), CInt(e.X - (100 / 2)), CInt(e.Y - (100 / 2)), 100, 100)
End Using
Me.Invalidate()
End If
End Sub

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawImage(backBuffer, Point.Empty)
MyBase.OnPaint(e)
End Sub
End Class

关于VB.Net BufferedGraphics透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12840401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com