透明线的绘制方式与彩色线不同 [英] Transparent line draws differently than colored line

查看:89
本文介绍了透明线的绘制方式与彩色线不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是示例代码,该代码显示如果以某个角度绘制线条,则使用透明颜色绘制时为线条渲染的像素与使用常规颜色绘制时不同。该示例将CompositingMode
设置为默认值"SourceOver";到"SourceCopy"这样透明线将移除放置在其上的像素,而不是仅在现有图形的顶部覆盖一条没有可见效果的透明线。

Included below is sample code that shows that if a line is drawn at a certain angle then the pixels rendered for the line are different when drawn with the transparent color than they are when drawn with a regular color. The sample sets the CompositingMode from the default of "SourceOver" to "SourceCopy" so that the transparent line will remove the pixels it is placed upon instead of just overlaying a transparent line on top of the existing graphics which would have no visible effect.

尝试粘贴样本Windows窗体应用程序中的代码,替换初始代码。然后运行该应用程序。 Form1将显示。然后执行以下操作以查看演示:

To try the sample paste the code in a Windows Forms application, replacing the initial code. Then run the application. The Form1 will display. Then do the following to see the demonstration:


  1. 单击表单。 a]一条线将以黑色绘制。 b]一秒钟过去了。 c]同一条线将以白色绘制。白线将完全取代黑线。
  2. 第二次单击表格。 a] 相同的线条将以黑色绘制。 b]一秒钟过去了。 c]同一条线将以透明方式绘制。透明线会留下一个黑色像素。
  3. 第三次单击表格。 a]同一条线将在充满红色的背景上画成黑色。 b]一秒钟过去了。 c]同一条线将以透明方式绘制。结果显示背景颜色为白色,其中绘制了透明线,
    是留下黑色的一个像素。

我不明白为什么透明线应使用与彩色线略有不同的算法绘制。有谁知道纠正这个问题的方法?它可能是.NET Framework代码中绘图逻辑本身的一个错误吗?或者它可能是故意这样的
?将Point2从6,5更改为5,5,以查看没有像素的示例。将Point2更改为50,5以查看剩余许多像素的示例。

I don't see why the transparent line should draw with a slightly different algorithm than the colored line. Does anyone know of a way to correct the issue? Is it perhaps a bug in the drawing logic itself in the NET Framework code or so? Or is it perhaps like this on purpose? Change Point2 from 6,5 to 5,5 to see an example where no pixels are left. Change Point2 to 50, 5 to see an example where many pixels are left.

Public Class Form1

    Private WithEvents t As New Timer
    Private b = New Bitmap(ClientSize.Width, ClientSize.Height)

    Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        Static Mode As Integer
        Dim Point1 As Point = New Point(0, 0)
        Dim Point2 As Point = New Point(6, 5)
        Using g As Graphics = Graphics.FromImage(b)
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy

            If Mode <> 2 Then
                g.Clear(Color.Transparent)
            End If
            If Mode = 2 Then
                g.Clear(Color.Red)
            End If

            g.DrawLine(Pens.Black, Point1, Point2)
            Me.Invalidate()

            t.Interval = 1000
            t.Start()
            Do While t.Enabled
                Application.DoEvents()
            Loop

            If Mode = 1 Or Mode = 2 Then
                g.DrawLine(Pens.Transparent, Point1, Point2)
            Else
                g.DrawLine(Pens.White, Point1, Point2)
            End If

            Me.Invalidate()
        End Using

        Mode = (Mode + 1) Mod 3
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        b.Dispose()
    End Sub

    Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
        t.Stop()
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.BackColor = Color.White
    End Sub

    Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawImage(b, 0, 0)
    End Sub
End Class

推荐答案

确实有趣。


这篇关于透明线的绘制方式与彩色线不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆