如何将图片从图片框转换为位图图像,以插入到Visual Basic中的另一个图片框中

时间:2017-05-28 02:00:38

标签: vb.net

在visual basic 2012中,我尝试将图片框中生成的图形以不同的间隔复制到另一个,以消除闪烁效果。我在互联网上找到了几种解决方案,但没有一种可以解决。

问题是画布上的图形不会被复制到显示图形图片框。

Imports System.Drawing.Drawing2D

Public Class Form1
    Private g As Graphics
    ''(0,0)=point, (0,1)=integer
    Dim points = {
            {New Point(150, 110), 0},
            {New Point(210, 200), 0},
            {New Point(250, 200), 0},
            {New Point(250, 150), 0}
}
Public Sub EnableDoubleBuffering()
    ' Set the value of the double-buffering style bits to true.
    Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
    Me.UpdateStyles()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    g = Canvas.CreateGraphics
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 'smooth stroke
    g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias 'smooth text
    g.InterpolationMode = InterpolationMode.HighQualityBilinear 'smooth fill
End Sub
Function ODGrid(ByVal xy As Double, ByVal depth As Double, ByVal move As Double)
    Dim xyd As Double = (((xy - move) / depth) + move) ''explanation: the more a number is divided the closer to 0 it is, same concept here.
    Return xyd
End Function
Private Sub NewQuad3D(colour As Brush, ByVal p() As Point, ByVal d() As Double)
    g.FillPolygon(colour, p)
End Sub

    Dim togbuf = 1
    Dim pointfit(3) As Point
    Dim depthfit(3) As Double
    Private Sub PerTick_Tick(sender As Object, e As EventArgs) Handles PerTick.Tick
        If togbuf = 1 Then
            Dim img = New Bitmap(Canvas.Width, Canvas.Height, g)
            DisplayGraphics.Image = img

            Canvas.Refresh()
            'calculations
            points(3, 0) = New Point(points(3, 0).X + 1, 150)
            For i As Integer = 0 To 3
                pointfit(i) = points(i, 0)
                depthfit(i) = points(i, 1)
            Next
        Else
            NewQuad3D(New SolidBrush(Color.FromArgb(255, 240, 100, 50)), pointfit, depthfit)
        End If
        togbuf *= -1
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

调用CreateGraphics几乎总是一个坏主意。而是为每个图片框创建一个位图,并为Graphics属性创建Image实例,然后使用Graphics类的方法复制数据。像这样:

Public Class Form4

    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)

        'Create bitmaps for each PictureBox
        Me.Canvas.Image = New Bitmap(Me.Canvas.Width, Me.Canvas.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
        Me.CanvasCopy.Image = New Bitmap(Me.CanvasCopy.Width, Me.CanvasCopy.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

        'Clear the Canvas and draw something on it to be copied later
        'Notice that it is using a Graphics instance for the bitmap
        Using g As Graphics = Graphics.FromImage(Me.Canvas.Image)
            g.Clear(Color.White)
            g.FillEllipse(Brushes.Red, Me.Canvas.ClientRectangle)
        End Using
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Create a Graphics instance from the Image property of the 
        'destination canvas (CanvasCopy).  Draw the source canvas into
        'the destination canvas specifying the coordinates in the destination
        Using g As Graphics = Graphics.FromImage(Me.CanvasCopy.Image)
            g.DrawImage(Me.Canvas.Image, 0, 0)
        End Using

        Me.CanvasCopy.Refresh()
    End Sub
End Class

此代码使用Using ... End Using块来确保正确处理GDI资源(Graphics)。