2017-05-28 80 views
-1

在Visual Basic 2012中,我试图将图片盒中生成的图形复制到另一个图片框中,闪烁的效果。我在互联网上发现了几个解决方案,但是他们都不会工作。如何将图片从图片库转换为位图图片以插入到另一个图片库中的视觉基本

问题是,画布中的图形不会被复制到displaygraphics picturebox。

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 
+0

您的程序泄漏GDI对象,因为您不处理未使用的位图和图形对象。 – Dai

+0

是的,我刚刚在几个星期前进入了这个视觉基础 – Joe

回答

0

调用CreateGraphics几乎总是一个坏主意。相反,请为每个图片框创建一个位图,并为Image属性创建Graphics实例,然后使用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)妥善处理。