使png图像透明
Make a png image transparent
我用 Panel1.Controls.Add(pb)
将 PictureBox
添加到我的 Panel1
,并且我尝试使我的 .png
图片透明。
我已经尝试使用 Color.Transparent
和 System.Drawing.Color.Transparent
,但是当我将 PictureBox
添加到我的 Panel
时,我无法使其透明。
而且我也无法将图像带到其他人的面前。
这是我的代码。
Private Function molduraint()
Dim pb As New PictureBox
pb.BringToFront()
pb.ImageLocation = OpenFileDialog1.FileName
pb.SizeMode = PictureBoxSizeMode.StretchImage
Panel1.Controls.Add(pb)
pb.Location = New Point(txtValueX.Text, txtValueY.Text)
If txtValueX.Text = 0 Or txtValueY.Text = 0 Then
pb.Location = New Point(300, 172)
End If
pb.Visible = True
pb.Size = New Size(TrackBar1.Value, TrackBar2.Value)
pb.Image = PictureBox1.Image
End Function
正如您可能知道的那样,WinForms
控件的设计并不完全支持真正的透明(除了 Forms
,那些实际上可以是透明的)。
Bitmaps
,另一方面,支持透明度。
如果您使用支持 Alpha 通道的图像格式创建 Bitmap
对象,例如 .png
位图,您可以绘制该图像并保持其透明度。
首先要做的是创建一个对象,该对象可用于引用我们要绘制的每个 Bitmap
,这样我们就可以跟踪它们。
由于您希望能够指定这些对象的位置和大小,因此这些是对象必须具有的两个属性。我在这里添加了一些有用的内容。
Public Class BitmapObject
Public Property Name As String
Public Property Image As Bitmap
Public Property Position As Point
Public Property Size As Size
Public Property Order As Integer
End Class
属性 Name
将是源文件的名称,Order
将引用 Bitmap
相对于另一个 Bitmaps
在容器内绘制。
所有 Bitmaps
将使用 Bitmap
对象的列表进行分组,因此我们可以使用列表索引或其中一个属性来召唤它们。
Public MyBitmaps As List(Of BitmapObject) = New List(Of BitmapObject)
至于绘图表面 (canvas),我们可以使用 Form
本身,一个 PictureBox
或一个 Panel
(因为它们 - 更多或更少 - 只是表面)。我更喜欢 Panel
,它很轻,可以承载其他控件,并且可以根据需要四处移动。
如果你想在控件上绘制,你只需要订阅它的 Paint()
事件并调用控件的 Invalidate()
方法引发它。
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
If MyBitmaps.Count > 0 Then
MyBitmaps.OrderBy(Function(item) item.Order).
Select(Function(item)
e.Graphics.DrawImage(item.Image, New Rectangle(item.Position, item.Size))
Return item
End Function).ToList()
End If
End Sub
添加一个Bitmap
到List(Of BitmapObject)
,既然你想用一个OpenFileDialog
让用户select一个Bitmap
,我们赋值此功能添加到 Button
中,当 Bitmap
被 select 编辑时,将创建一个新的 BitmapObject
并将其附加到列表中。
Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.InitialDirectory = "[Images Path]"
Dim dr As DialogResult = fd.ShowDialog()
If dr = Windows.Forms.DialogResult.OK Then
Dim BitmapName As String = New FileInfo(fd.FileName).Name
Using tmpBitmap As Bitmap = New Bitmap(fd.FileName)
MyBitmaps.Add(New BitmapObject With {
.Image = New Bitmap(tmpBitmap),
.Position = New Point(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)),
.Size = New Size(tmpBitmap.Height, tmpBitmap.Width),
.Order = MyBitmaps.Count,
.Name = BitmapName})
ComboBox1.Items.Add(BitmapName)
ComboBox1.SelectedIndex = MyBitmaps.Count - 1
TrackBar1.Value = tmpBitmap.Height
TrackBar2.Value = tmpBitmap.Width
Panel1.Invalidate()
End Using
End If
End Sub
这是结果:(Full source code in PasteBin)
我用 Panel1.Controls.Add(pb)
将 PictureBox
添加到我的 Panel1
,并且我尝试使我的 .png
图片透明。
我已经尝试使用 Color.Transparent
和 System.Drawing.Color.Transparent
,但是当我将 PictureBox
添加到我的 Panel
时,我无法使其透明。
而且我也无法将图像带到其他人的面前。
这是我的代码。
Private Function molduraint()
Dim pb As New PictureBox
pb.BringToFront()
pb.ImageLocation = OpenFileDialog1.FileName
pb.SizeMode = PictureBoxSizeMode.StretchImage
Panel1.Controls.Add(pb)
pb.Location = New Point(txtValueX.Text, txtValueY.Text)
If txtValueX.Text = 0 Or txtValueY.Text = 0 Then
pb.Location = New Point(300, 172)
End If
pb.Visible = True
pb.Size = New Size(TrackBar1.Value, TrackBar2.Value)
pb.Image = PictureBox1.Image
End Function
正如您可能知道的那样,WinForms
控件的设计并不完全支持真正的透明(除了 Forms
,那些实际上可以是透明的)。
Bitmaps
,另一方面,支持透明度。
如果您使用支持 Alpha 通道的图像格式创建 Bitmap
对象,例如 .png
位图,您可以绘制该图像并保持其透明度。
首先要做的是创建一个对象,该对象可用于引用我们要绘制的每个 Bitmap
,这样我们就可以跟踪它们。
由于您希望能够指定这些对象的位置和大小,因此这些是对象必须具有的两个属性。我在这里添加了一些有用的内容。
Public Class BitmapObject
Public Property Name As String
Public Property Image As Bitmap
Public Property Position As Point
Public Property Size As Size
Public Property Order As Integer
End Class
属性 Name
将是源文件的名称,Order
将引用 Bitmap
相对于另一个 Bitmaps
在容器内绘制。
所有 Bitmaps
将使用 Bitmap
对象的列表进行分组,因此我们可以使用列表索引或其中一个属性来召唤它们。
Public MyBitmaps As List(Of BitmapObject) = New List(Of BitmapObject)
至于绘图表面 (canvas),我们可以使用 Form
本身,一个 PictureBox
或一个 Panel
(因为它们 - 更多或更少 - 只是表面)。我更喜欢 Panel
,它很轻,可以承载其他控件,并且可以根据需要四处移动。
如果你想在控件上绘制,你只需要订阅它的 Paint()
事件并调用控件的 Invalidate()
方法引发它。
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
If MyBitmaps.Count > 0 Then
MyBitmaps.OrderBy(Function(item) item.Order).
Select(Function(item)
e.Graphics.DrawImage(item.Image, New Rectangle(item.Position, item.Size))
Return item
End Function).ToList()
End If
End Sub
添加一个Bitmap
到List(Of BitmapObject)
,既然你想用一个OpenFileDialog
让用户select一个Bitmap
,我们赋值此功能添加到 Button
中,当 Bitmap
被 select 编辑时,将创建一个新的 BitmapObject
并将其附加到列表中。
Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.InitialDirectory = "[Images Path]"
Dim dr As DialogResult = fd.ShowDialog()
If dr = Windows.Forms.DialogResult.OK Then
Dim BitmapName As String = New FileInfo(fd.FileName).Name
Using tmpBitmap As Bitmap = New Bitmap(fd.FileName)
MyBitmaps.Add(New BitmapObject With {
.Image = New Bitmap(tmpBitmap),
.Position = New Point(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)),
.Size = New Size(tmpBitmap.Height, tmpBitmap.Width),
.Order = MyBitmaps.Count,
.Name = BitmapName})
ComboBox1.Items.Add(BitmapName)
ComboBox1.SelectedIndex = MyBitmaps.Count - 1
TrackBar1.Value = tmpBitmap.Height
TrackBar2.Value = tmpBitmap.Width
Panel1.Invalidate()
End Using
End If
End Sub
这是结果:(Full source code in PasteBin)