I've created this and have posted it on my programming forums I'm a member of --Forum URL removed, rule #4--. Basically what it is, is an XNA control in a windows form application. There are three pretty ground breaking things going on with this post. The first is that the control is written in visual basic.net, most XNA stuff you'll find would be in C#. The second is that it's for a windows form application, meaning that you can use the regular vb.net IDE as opposed to using the XNA game studios. The third is that it is done without the use of the content pipeline. Just about everything that you see on XNA, be it in C# or VB.Net, it uses the content pipeline.
To get started. We will first turn option strict and explicit on. It's the basis of all good vb.net programming:
Next we will Import the System.IO, Xna framework, and the Xna graphics:
Next we are going to inherit a control to get all the desired events:
Next we're going to declare an instance of GraphicsDevice and an instance of SpriteBatch at the form level:
Next we're going to set up two properties, one for our image and one for the transparency color:
Next we're going to declare a texture2d at the form level and set up our initialize sub:
What's going on in the initialize sub is, we're trying to set up our graphics adapter with the desired PresentationParameters. If all goes well we set up the graphics adapter and return a true value. If it doesn't work out, we eat up the exception(bad, but w/e) and return a false value.
Next we're going to set up a function that converts bitmaps to texture2d's:
The only issue with this function is, if you want to create Xbox games, you cannot use Bitmaps, you have to use true Texture2d's which means you'll need to use the content pipeline.
Next we set up our load and draw subs:
Finally we override OnPaint, to reflect any changes in the designer, and do all the drawing:
All in all the code will look like this finalized:
- - - Updated - - -
I just noticed that it has 'Last edited by Boart' why is that and who is that?
To get started. We will first turn option strict and explicit on. It's the basis of all good vb.net programming:
Code:
Option Strict On
Option Explicit On
Next we will Import the System.IO, Xna framework, and the Xna graphics:
Code:
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports System.IO
Next we are going to inherit a control to get all the desired events:
Code:
Public Class Xna_Sprite
Inherits Windows.Forms.Control
Next we're going to declare an instance of GraphicsDevice and an instance of SpriteBatch at the form level:
Code:
Private grafix As Graphics.GraphicsDevice
Private s_Batch As SpriteBatch
Next we're going to set up two properties, one for our image and one for the transparency color:
Code:
Private img As Bitmap
Public Property Image() As Bitmap
Get
Return img
End Get
Set(ByVal value As Bitmap)
img = value
Call Reset()
End Set
End Property
Private transColor As Color = Color.CornflowerBlue
Public Property TransparentColor() As Color
Get
Return transColor
End Get
Set(ByVal value As Color)
transColor = value
End Set
End Property
Code:
Private textur As Texture2D
Private Function initialize(ByRef surface As Xna_Sprite) 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
Next we're going to set up a function that converts bitmaps to texture2d's:
Code:
Private Function BitmapToTexture2D(GraphicsDevice As GraphicsDevice, image As System.Drawing.Bitmap) As Texture2D
Dim bufferSize As Integer = image.Height * image.Width * 4
Dim memoryStream As New System.IO.MemoryStream(bufferSize)
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png)
memoryStream.Seek(0, SeekOrigin.Begin)
Dim texture As Texture2D = Texture2D.FromStream(GraphicsDevice, memoryStream, image.Width, image.Height, False)
memoryStream.Close()
Return texture
End Function
Next we set up our load and draw subs:
Code:
Private Sub Load_Content()
If IsNothing(grafix) = False Then
s_Batch = New SpriteBatch(grafix)
textur = BitmapToTexture2D(grafix, img)
Else
Throw New ArgumentNullException("Grafix")
Exit Sub
End If
End Sub
Private Sub Draw()
grafix.Clear(transColor)
With s_Batch
.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
.Draw(textur, New Rectangle(0, 0, img.Width, img.Height), transColor)
.End()
End With
grafix.Present()
End Sub
Finally we override OnPaint, to reflect any changes in the designer, and do all the drawing:
Code:
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
If IsNothing(img) Then
Dim g As Drawing.Graphics = e.Graphics
g.DrawIcon(System.Drawing.SystemIcons.Error, Me.DisplayRectangle)
Else
Call Reset()
End If
'MyBase.OnPaint(e)
End Sub
Public Sub Reset()
If initialize(Me) Then
Call Load_Content()
Call Draw()
End If
End Sub
End Class
All in all the code will look like this finalized:
Code:
Option Strict On
Option Explicit On
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports System.IO
Public Class Xna_Sprite
Inherits Windows.Forms.Control
Private grafix As Graphics.GraphicsDevice
Private s_Batch As SpriteBatch
Private img As Bitmap
Public Property Image() As Bitmap
Get
Return img
End Get
Set(ByVal value As Bitmap)
img = value
Call Reset()
End Set
End Property
Private transColor As Color = Color.CornflowerBlue
Public Property TransparentColor() As Color
Get
Return transColor
End Get
Set(ByVal value As Color)
transColor = value
End Set
End Property
Private textur As Texture2D
Private Function initialize(ByRef surface As Xna_Sprite) 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 Function BitmapToTexture2D(GraphicsDevice As GraphicsDevice, image As System.Drawing.Bitmap) As Texture2D
Dim bufferSize As Integer = image.Height * image.Width * 4
Dim memoryStream As New System.IO.MemoryStream(bufferSize)
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png)
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)
textur = BitmapToTexture2D(grafix, img)
Else
Throw New ArgumentNullException("Grafix")
Exit Sub
End If
End Sub
Private Sub Draw()
grafix.Clear(transColor)
With s_Batch
.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
.Draw(textur, New Rectangle(0, 0, img.Width, img.Height), transColor)
.End()
End With
grafix.Present()
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
If IsNothing(img) Then
Dim g As Drawing.Graphics = e.Graphics
g.DrawIcon(System.Drawing.SystemIcons.Error, Me.DisplayRectangle)
Else
Call Reset()
End If
'MyBase.OnPaint(e)
End Sub
Public Sub Reset()
If initialize(Me) Then
Call Load_Content()
Call Draw()
End If
End Sub
End Class
- - - Updated - - -
I just noticed that it has 'Last edited by Boart' why is that and who is that?
Last edited by a moderator: