1

私はグラフィックスのプログラミングにまったく慣れていないため、VB はあまり使用していないため、ここで壁にぶつかっています。基本的にすべてのコードを実行しましたが、欠けているのは、画像の周りに透明なパディング/境界線を数ピクセル追加することだけで、行き詰まりました。私は周りを見回しましたが、私が見た例は圧倒的に複雑に見え、やり過ぎのように見えました (コードのページ上のページ)。

ポインタやわかりやすいチュートリアル/例は非常に高く評価されます。

以下に現在のコードを追加しました。

現在のコード

Dim img As Image = New Bitmap(100, 100)

Dim Drawing As Graphics = Graphics.FromImage(img)
Dim rand As New Random
Dim bgcolor As Color = Color.FromArgb(rand.Next(64, 196), rand.Next(64, 196), rand.Next(64, 196))

Dim family As FontFamily = Nothing

Dim fontName As String = "Lucida Sans Typewriter"
Dim fontSize As Single = 42

Using fontTester As New Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.Pixel)
    If fontTester.Name = fontName Then 
        family = New FontFamily("Lucida Sans Typewriter")
    Else
        Try
            Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
            privateFonts.AddFontFile(HttpContext.Current.Server.MapPath("~/") + "\styles\fonts\LTYPE.ttf")
            Dim font As New System.Drawing.Font(privateFonts.Families(0), 42)
            family = font.FontFamily
        Catch ex As Exception
            family = New FontFamily("Arial")
        End Try
    End If
End Using

Dim FontText As Font = New Font(family, 42, FontStyle.Regular)

Drawing.Clear(bgcolor)
Dim textBrush As Brush = New SolidBrush(Color.White)
Drawing.DrawString(initials, FontText, textBrush, 7, 16)
Drawing.Save()
textBrush.Dispose()
Drawing.Dispose()
4

2 に答える 2

0

次のコードは、黒のフォントの周囲の 1 ピクセルを透明に変更する例です。複数のピクセルを考慮するようにコードを拡張するのは簡単です。このコードは、サンプル コードに示されているように、画像がテキスト文字列のような長方形でない場合に機能します。フォントの色を黒に設定しても、純粋な黒ではないピクセルがいくつかあり、それらにはいくつかの色合いがあるため、私の例では、グレーの色合いを考慮して赤のコンポーネントが 255 であるかどうかを確認します。

        Dim bmp As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim rand As New Random
        Dim bgcolor As Color = Color.Red
        g.Clear(bgcolor)
        Dim FontText As Font = New Font("Arial", 42, FontStyle.Regular)

        Dim textBrush As Brush = New SolidBrush(Color.Black)
        g.DrawString("Teste", FontText, textBrush, 7, 16)
        For i As Integer = 0 To Width - 1
            For j As Integer = 0 To Height - 1
               If bmp.GetPixel(i, j).R < 255 Then
                    If (bmp.GetPixel(i - 1, j).R = 255 And bmp.GetPixel(i - 1, j).G = 0 And bmp.GetPixel(i - 1, j).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j).R = 255 And bmp.GetPixel(i + 1, j).G = 0 And bmp.GetPixel(i + 1, j).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i, j - 1).R = 255 And bmp.GetPixel(i, j - 1).G = 0 And bmp.GetPixel(i, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i, j + 1).R = 255 And bmp.GetPixel(i, j + 1).G = 0 And bmp.GetPixel(i, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j + 1).R = 255 And bmp.GetPixel(i + 1, j + 1).G = 0 And bmp.GetPixel(i + 1, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i - 1, j - 1).R = 255 And bmp.GetPixel(i - 1, j - 1).G = 0 And bmp.GetPixel(i - 1, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i + 1, j - 1).R = 255 And bmp.GetPixel(i + 1, j - 1).G = 0 And bmp.GetPixel(i + 1, j - 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                    If (bmp.GetPixel(i - 1, j + 1).R = 255 And bmp.GetPixel(i - 1, j + 1).G = 0 And bmp.GetPixel(i - 1, j + 1).B = 0) Then
                        bmp.SetPixel(i, j, Color.Transparent)
                    End If
                   End If

            Next
        Next

        textBrush.Dispose()
        g.Dispose()

        bmp.Save("TESTE.png", System.Drawing.Imaging.ImageFormat.Png)
于 2015-09-18T13:47:31.897 に答える