【问题标题】:vb 2015 GDI+ bitmap placementvb 2015 GDI+位图放置
【发布时间】:2016-01-06 23:36:28
【问题描述】:

我几乎没有运气试图在 stackoverflow 上找到这个问题的答案,或者在一般的互联网上。

我在一个 vb 2015 Windows Forms 项目中有一个表单。

在那个表单上,我放置了六个控件:四个文本框、一个面板和一个按钮。

当我点击按钮时,它会生成一个位图,如下所示:

 Private Sub btnSetLeft_Click(sender As Object, e As EventArgs) Handles btnSetLeft.Click
    Dim R As Integer = CInt(txtRedLeft.Text)
    Dim G As Integer = CInt(txtGreenLeft.Text)
    Dim B As Integer = CInt(txtBlueLeft.Text)
    Dim A As Integer = CInt(txtAlphaLeft.Text)
    gcL = Color.FromArgb(A, R, G, B)
    Using bm As Bitmap = New Bitmap(9, 9)
        Using gBM As Graphics = Graphics.FromImage(bm)
            Using br As SolidBrush = New SolidBrush(gcL)
                gBM.FillRectangle(br, New Rectangle(0, 0, 8, 8))
            End Using
        End Using
    End Using
 End Sub

但是,在构建位图之后,我希望按钮将位图放在面板上,然后重新绘制面板,从而显示新的位图。

我该怎么做?

【问题讨论】:

  • thePanel.BackgroundImage = bm 你必须将它从 USING 块中删除,这样你就不会丢弃它,这意味着如果你再次点击,你应该丢弃旧的(或重复使用同一个)

标签: vb.net bitmap panel gdi+


【解决方案1】:

这并不难(我简化了你的例子):

Dim gcL As Color = Color.Blue
Using bm As New Bitmap(9, 9)
    Using gBM As Graphics = Graphics.FromImage(bm)
        Using br As New SolidBrush(gcL)
            gBM.FillRectangle(br, New Rectangle(0, 0, 8, 8))
        End Using
    End Using
    panel1.BackgroundImage = bm
    panel1.BackgroundImageLayout = ImageLayout.Tile
    panel1.Update()
End Using

【讨论】:

  • 谢谢 - 面板明显大于位图,我不想将其用作面板的背景图像。相反,我希望能够将位图放置在面板上的某个指定位置。
  • 你可以有两个位图。一个会填满面板。您可以在较大位图的任何位置“绘制”较小的位图。
  • 如果没有办法直接放在面板上,我想我可以做到。但是,不知何故,将它直接放在面板上似乎更优雅。 (叹气)在较大的位图上绘制较小的位图会是什么样的代码?
  • 您也可以覆盖控件的 OnPaint 消息,但我认为这更复杂。使用 Google 查找技术。
猜你喜欢
  • 2012-05-10
  • 2016-06-15
  • 1970-01-01
  • 2011-12-27
  • 2013-07-22
  • 2013-09-09
  • 2018-12-12
  • 2010-12-07
  • 1970-01-01
相关资源
最近更新 更多