0

我在 a 中有一个图像,PictureBox并且我还有其他几个要PictureBox在不同位置绘制的图像。

例如,我有几张图像要绘制到PictureBox像这两个星星内的图片上(都是具有透明背景的单独图片(PNG)):

星星

我想在不同的位置绘制多个不同的星星,如下图所示。(美国地图是已经存在于PictureBox.

imgUS

我正在尝试编写一个函数,当提供/给定'和值的Point变量以及图像的位置(例如或)时,会将多个图像绘制到.PictureBoxxyMy.Resources.RedCityMy.Resources.BlueCityPictureBox

到目前为止,我已经完成了在 上绘制一张图片picturebox,但是我无法一次PictureBox在不同位置绘制多张图片,因为当我使图像无效时另一张图像消失了。

我正在考虑编写一个类似这样的函数来将图片/点添加到某个列表或其他内容中,并在PictureBox'paint事件下添加一些内容,但我不确定它是如何工作的。

是否存在或可以编写任何可以编写的函数,能够同时PictureBox在不同位置绘制多个图像?

谢谢..

4

2 回答 2

1

我正在考虑编写一个类似这样的函数来将图片/点添加到某个列表或其他内容中,并在 PictureBox 的绘制事件下添加一些内容,但我不确定它是如何工作的。

这正是正确的做法。简单的例子:

Public Class Form1

    Private dataPoints As New List(Of Tuple(Of Point, Integer))

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        dataPoints.Add(Tuple.Create(New Point(nudX.Value, nudY.Value), If(cbBlue.Checked, 0, 1)))
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Dim G As Graphics = e.Graphics
        For Each dataPoint In dataPoints
            Dim img As Image = If(dataPoint.Item2 = 0, My.Resources.BlueCity, My.Resources.RedCity)
            G.DrawImage(img, dataPoint.Item1)
        Next
    End Sub

End Class
于 2021-04-01T22:56:48.187 回答
0

我会将地图加载到图形环境中,然后在其上绘制。

下面是一个如何绘制字符串的示例(可以在地图上旋转),gr.DrawString(,,,,0,0) 末尾的两个零将字符串放在左上角)。FromImage(bmp) 是您可以加载美国地图的地方,只需使用地图的文件地址将 bmp 导入文件路径,或将其添加到资源中。我会添加来自 Wingding 字体的彩色星星和圆圈,它们具有不同形状的字符,您只需要更改它们的颜色。我提供了字体大小,所以你必须使用它。

Dim bmp As New Bitmap(500, 15)
Dim gr As Graphics = Graphics.FromImage(bmp)
gr.SmoothingMode = SmoothingMode.HighQuality
gr.InterpolationMode = InterpolationMode.HighQualityBicubic
gr.PixelOffsetMode = PixelOffsetMode.HighQuality
gr.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
gr.SmoothingMode = SmoothingMode.AntiAlias
Dim mybrush As New SolidBrush(Color.Yellow)
Dim fontbrush As New SolidBrush(Color.Black)
fontbrush.Color = SystemColors.ControlText
mybrush.Color = SystemColors.Control
Dim sfont As New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
Dim strformat As StringFormat = New StringFormat(StringFormatFlags.DirectionVertical)
strformat.Alignment = StringAlignment.Far
Dim strToDraw As String
strToDraw = "Test string"
Dim stringSize As New SizeF()
stringSize = gr.MeasureString(strToDraw, sfont)
gr.FillRectangle(mybrush, CInt(0), CInt(0), stringSize.Width, stringSize.Height)
gr.DrawString(strToDraw, sgenefont, fontbrush, 0, 0)
gr.Dispose
于 2021-04-02T14:14:27.137 回答