Hi, i’ve build a chart app with XNA primitive, and now i’d like use spritebatch to do a semitrasparent rectangle for select data. They work alone, but when i mix them like in the follow code i get exception
Code:
Option Strict On
Option Explicit On
Imports System.IO
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Public Class Form1
Public Shared test As Integer
Private grafix As Graphics.GraphicsDevice = Nothing
Private s_Batch As SpriteBatch
Private sprite As Texture2D
Private sprite2 As Texture2D
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
Public Shared Function BitmapToTexture2D(ByVal GraphicsDevice As GraphicsDevice, ByVal image As System.Drawing.Bitmap) As Texture2D
Dim bufferSize As Integer = image.Height * image.Width * 4
' Create new memory stream and save image to stream so
' we don't have to save and read file
Dim memoryStream As New System.IO.MemoryStream(bufferSize)
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png)
' Creates a texture from IO.Stream - our memory stream
memoryStream.Seek(0, SeekOrigin.Begin)
Dim texture As Texture2D = Texture2D.FromStream(GraphicsDevice, memoryStream, image.Width, image.Height, False)
memoryStream.Close()
Return texture
End Function
Private Sub Load_Content()
If IsNothing(grafix) = False Then
s_Batch = New SpriteBatch(grafix)
sprite = BitmapToTexture2D(grafix, My.Resources.my_image)
sprite2 = BitmapToTexture2D(grafix, My.Resources.my_image2)
Else
Throw New ArgumentNullException("Grafix")
Exit Sub
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do Until True = False
grafix.Clear(Color.Black)
' Dim newlineORI() As VertexPositionColor = Graphic2D.Set2dLine(x1ori, y1ori, z1, x2ori, y2ori, z2, colorOri) '
Dim newlineORI() As VertexPositionColor = Set2dLine(10, 20, 0, 200, 250, 0, Color.Azure)
grafix.DrawUserPrimitives(PrimitiveType.LineList, newlineORI, 0, 1)
With s_Batch
.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
.Draw(sprite, New Rectangle(0, 0, test, 200), Color.White * 0.3F)
.End()
End With
grafix.Present()
Loop
End Sub
Public Shared 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 SetUpCamera()
Dim cameraPos = New Vector3(-25, 13, 18)
Dim viewMatrix = Matrix.CreateLookAt(cameraPos, New Vector3(0, 2, -12), New Vector3(0, 1, 0))
Dim projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, grafix.Viewport.AspectRatio, 1.0F, 200.0F)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bool As Boolean = initialize(PictureBox1)
If bool = True Then
Call Load_Content()
' SetUpCamera()
BackgroundWorker1.RunWorkerAsync()
Else
MessageBox.Show("There was a problem initializing XNA.")
Me.Close()
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
test = e.Location.X
End Sub
End Class
I get this exception: In order to perform design operations , setting both a vertex shader that a pixel shader on the device. Anyone can help me to solve?