vb.net显示图形的简单介绍( 二 )


.AutoDepthStencilFormat = Direct3D.DepthFormat.D16
End With
MyDevice = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, Direct3D.CreateFlags.HardwareVertexProcessing, d3dpp) '创建DX设备啦!以下两句请照抄 。
MyDevice.SetRenderState(Direct3D.RenderStates.ZEnable, True) ‘Z缓冲
MyDevice.SetRenderState(Direct3D.RenderStates.NormalizeNormals, True)'法线归一化,请看相关数学书籍 。
End Sub
Public Sub RestoreDeviceObjects()
Dim PlaneIB() As Short = {0, 1, 3, 0, 2, 3} ’顶点索引信息 。
Dim AttrTable(1) As Direct3D.AttributeRange ‘顶点分组属性表
AttrTable(0).AttributeId = 0
AttrTable(0).FaceStart = 0
AttrTable(0).FaceCount = 2 ’有两个三角形
AttrTable(0).VertexStart = 0
AttrTable(0).VertexCount = 4 ‘四个点
‘顶点坐标信息 。
VBPlane(0) = New Direct3D.CustomVertex.PositionNormalTextured(-500, -500, 0, 0, 0, 1, 0, 0)
VBPlane(1) = New Direct3D.CustomVertex.PositionNormalTextured(500, -500, 0, 0, 0, 1, 1, 0)
VBPlane(2) = New Direct3D.CustomVertex.PositionNormalTextured(-500, 500, 0, 0, 0, 1, 0, 1)
VBPlane(3) = New Direct3D.CustomVertex.PositionNormalTextured(500, 500, 0, 0, 0, 1, 1, 1)
MyPlane = New Direct3D.Mesh(2, 4, Direct3D.MeshFlags.Managed, Direct3D.VertexFormats.Position + Direct3D.VertexFormats.Normal + Direct3D.VertexFormats.Texture1, MyDevice) ’创建物体
MyPlane.SetVertexBufferData(VBPlane, Direct3D.LockFlags.None) ‘输入顶点坐标数据
MyPlane.SetIndexBufferData(PlaneIB, Direct3D.LockFlags.None) ‘输入索引数据
MyPlane.SetAttributeTable(AttrTable) ‘输入顶点分组属性表
End Sub
Public Sub Render() ‘调用它画图
Dim vlook As New Vector3(1, 0, 0)
Dim vPos As New Vector3(0,0,0)
Dim vUp As New Vector3(0, 0, 1)
MatView = Matrix.LookAtLH(vPos, vlook, vUp) ‘计算摄像机位置矩阵
Device.SetTransform(Direct3D.TransformType.View, MatView) ‘设置当前摄像机位置矩阵为MatView 。
Dim fAspect As Single = Me.Width / Me.Height ’窗口长宽比
matProj = Matrix.PerspectiveFovLH(Math.PI / 4, fAspect, 1.0F, 10001) ‘计算透视矩阵MatProj 。
MyDevice.SetTransform(Direct3D.TransformType.Projection, matProj) ‘设置当前透视矩阵为MatProj 。
MyDevice.Clear(Direct3D.ClearFlags.Target + Direct3D.ClearFlags.ZBuffer, Color.Blue, 1.0F, 0) ’先刷蓝屏
MyDevice.BeginScene() ‘开始画
MatWorld = Matrix.Identity ’物体位于原点 , 不旋转
Device.SetTransform(Direct3D.TransformType.World, MatWorld) ’设置物体位置
Me.Mesh.DrawSubset(0) ‘画物体
MyDevice.EndScene() ’结束
MyDevice.Present() ‘显示在屏幕上
End Sub
Public Sub DeleteDeviceObjects() ’结束程序时放掉资源
MyPlane.Dispose()
MyDevice.Dispose()
End Sub
#End Region
Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DeleteDeviceObjects()
Windows.Forms.Cursor.Show()
End Sub
Private Sub FormMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitDeviceObjects()
RestoreDeviceObjects()
Windows.Forms.Cursor.Hide()
Render()
End Sub
End Class
在VB.NET下面如何显示和保存缩放图像实现代码如下:
Dim img As Image = Image.FromFile("D:\Image\tstImage.jpg")'tstImage是原先的图片
Dim grfx As Graphics = Me.CreateGraphics
grfx.DrawImage(img, 0, 0, img.Width * 3, img.Height * 3)'在Form里显示
Dim imgnew As New System.Drawing.Bitmap(img, img.Height * 3, img.Width * 3)'新建一个放大的图片
imgnew.Save("D:\Image\tstNewImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)'保存放大后图片
你可以建一个Form,然后在Form里拖进一个Button , 把上面的代码放在Button_Click事件里面 , 执行就行了 。

推荐阅读