VB.NET 在不同位置的图片框上绘制多个图像

问题描述

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

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

star

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

imgUS

我正在尝试编写一个函数,当提供/给定 PointPictureBoxx 值的 y 变量以及图像的位置时(例如 My.Resources.RedCityMy.Resources.BlueCity)会将多个图像绘制到 PictureBox 上。

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

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

是否有任何存在或可以编写的函数可以同时将多个图像绘制到不同位置的 PictureBox 上?

谢谢..

解决方法

我想写一个这样的函数来添加一个 图片/指向某个列表或某物并在 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,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
,

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

下面是一个如何绘制字符串的例子(可以在地图上旋转),gr.DrawString(,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),stringSize.Width,stringSize.Height)
gr.DrawString(strToDraw,sgenefont,fontbrush,0)
gr.Dispose