0

I'm drawing lines in a picturebox inside a form, when I maximize the form the picturebox change height and width automatically because of anchor bounds. Problem is that the lines are rendered in wrong way on the maximized window,and lines that should be 1 pixel width seem bigger. I'm missing some zoom proprety in picturebox control? There is a way to avoid that? I'm using XNA 4.0, here the basic code where pbGame is my picturebox.

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

Private quit As Boolean = False
Public grafix As GraphicsDevice

 Private Function initialize(ByRef surface As PictureBox) As Boolean
    Try
        Dim pparam As New PresentationParameters
        pparam.DeviceWindowHandle = surface.Handle
        pparam.IsFullScreen = False

        Dim grafixAdapt As GraphicsAdapter = GraphicsAdapter.DefaultAdapter

        grafix = New GraphicsDevice(grafixAdapt, GraphicsProfile.HiDef, pparam)


        initialize = True
    Catch ex As Exception
        initialize = False
    End Try
End Function

 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Set up the initialize function found above
    If InitializeGraphics(pbGame) AndAlso InitializeEffect(grafix) Then
        BackgroundWorker1.RunWorkerAsync()
    Else
        MessageBox.Show("There was a problem initializing XNA.")
        Me.Close()
    End If
End Sub


Private effect As BasicEffect
Private Function InitializeEffect(ByVal graphics As GraphicsDevice) As Boolean
    effect = New BasicEffect(graphics)

    Try
        effect.VertexColorEnabled = True
        effect.Projection = Matrix.CreateOrthographicOffCenter(0, graphics.Viewport.Width, graphics.Viewport.Height, 0, 0, 1)

        InitializeEffect = True
    Catch ex As Exception
        InitializeEffect = False
    End Try
End Function



Private Function Set2dLine(ByVal x1 As Integer, ByVal y1 As Integer, ByVal z1 As Integer, _
                                 ByVal x2 As Integer, ByVal y2 As Integer, ByVal z2 As Integer, _
                                 ByVal color As Color) As VertexPositionColor()
        Dim vertices1, vertices2 As New VertexPositionColor

        vertices1.Position = New Vector3(x1, y1, z1)
        vertices1.Color = color
        vertices2.Position = New Vector3(x2, y2, z2)
        vertices2.Color = color

        Return {vertices1, vertices2}
    End Function

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Do Until quit = True
        grafix.Clear(Color.CornflowerBlue)

        effect.CurrentTechnique.Passes(0).Apply()

        Dim newline() As VertexPositionColor = Set2dLine(50, 10, 0, 150, 10, 0, Color.Black)
        

        grafix.DrawUserPrimitives(PrimitiveType.LineList, newline, 0, 1)
      

        grafix.Present()
    Loop
End Sub
  • Are you drawing on the PictureBox surface or to a Bitmap shown in the PictureBox? It looks like the latter. In any case, if the canvas can change size and your drawing need to scale with it, you need to define a scale factor to use as multiplier. -- It's required that you post your code. -- If you're drawing on a Bitmap, see, e.g., the `GetScaledSelectionRect()` here: [How to draw a transparent shape over an Image](https://stackoverflow.com/a/62724726/7444103) – Jimi Jun 14 '21 at 17:01
  • I'm drawing lines in a picturebox using XNA in a winform –  Jun 14 '21 at 17:36
  • So you probably have set the backbuffer to the original width/height of the canvas, then when you scale the canvas, the viewport is scaled along with it. XNA is very old stuff. – Jimi Jun 14 '21 at 17:49
  • I added the code that I use. –  Jun 14 '21 at 18:28
  • 1
    I've added the `XNA` tag, so you may get help from someone that's still using it. If you're using the `FNA` binding instead, add this information to your question (you should have done this from the beginning, you nee to provide all the information needed to understand the context of a question), since the `FNA` tag doesn't exist (and I won't create it for this alone). -- Add a tag that specifies the version, if needed (you didn't say what version you're using either). – Jimi Jun 14 '21 at 21:08

0 Answers0