Home All Groups Group Topic Archive Search About

DirectX and False Stretching Images

Author
30 Jun 2006 4:55 AM
A. Gaubatz
I am using some code that I somewhat cannibalized from a tutorial, and
whenever I try to put in an image not of specific seemingly random
sizes, it stretches them out alot.

The module that runs the code I am using is this: (note that the
tutorial had a 3D segment as well, which has been remarked)

-------------------------------------------------------

Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Module mdx9a
    Public device As device
    Public sprite As Sprite
    Dim deviceSetting As New PresentParameters
    Structure oggX
        'contain mesh and material for an object
        'Public mesh As Mesh
        'Public numX As Integer
        Public tex() As Texture
        'Public mat() As Material
    End Structure
    'Public Const GradToRad = Math.PI / 180



    Sub createDevice(ByVal width As Integer, ByVal heigth As Integer,
ByVal bpp As Integer, ByVal fhWnd As System.IntPtr, ByVal windowed As
Boolean)
        'screen description
        deviceSetting.BackBufferCount = 1 'backbuffer number
        deviceSetting.AutoDepthStencilFormat = DepthFormat.D16
'Z/Stencil buffer formats
        deviceSetting.EnableAutoDepthStencil = True 'active Z/Stencil
buffer
        deviceSetting.DeviceWindowHandle = fhWnd 'handle del form
        deviceSetting.SwapEffect = SwapEffect.Flip 'rendering type
        If windowed Then
            deviceSetting.Windowed = True 'setting for windowed mode
        Else
            deviceSetting.Windowed = False 'setting for fullscreen
            deviceSetting.BackBufferWidth = width 'screen resolution
            deviceSetting.BackBufferHeight = heigth 'screen resolution
            If bpp = 16 Then
                deviceSetting.BackBufferFormat = Format.R5G6B5
'backbuffer format at 16Bit
            Else
                deviceSetting.BackBufferFormat = Format.X8R8G8B8
'backbuffer format at 32Bit
            End If

        End If
        'presentation type
        deviceSetting.PresentationInterval = PresentInterval.Immediate
        'create device
        device = New device(0, DeviceType.Hardware, fhWnd,
CreateFlags.HardwareVertexProcessing, deviceSetting)

        'create a sprite controller for device
        sprite = New sprite(device)

    End Sub

    'must be executed when form is resized
    Sub resetDevice()
        'you must putting them to zero to permit directX to change
backbuffer size
        deviceSetting.BackBufferHeight = 0
        deviceSetting.BackBufferWidth = 0
        device.Reset(deviceSetting)
    End Sub

    Sub defaultSetting()

        ' device.RenderState.ZBufferEnable = True 'Z buffer on
        ' device.RenderState.Lighting = False 'lights off
        '  device.RenderState.ShadeMode = ShadeMode.Gouraud 'gouraud
mode


        device.Transform.World = Matrix.Identity
        device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, -30),
New Vector3(0, 0, 0), New Vector3(0, 1, 0))
        device.Transform.Projection =
Matrix.PerspectiveFovLH(CSng(Math.PI / 3), CSng(4 / 3), 1, 2000)

    End Sub

    'create texture from file
    Function createTexture(ByVal filesrc As String, Optional ByVal
colorKey As Integer = 0) As Texture
        Return TextureLoader.FromFile(device, filesrc, 0, 0, 0, 0,
Format.Unknown, Pool.Managed, Filter.Linear, Filter.Linear, colorKey)
    End Function
End Module

--------------------------------------------------

I am then using code like this to display pictures to the screen.

This to Create:
Texture = createTexture("filename", Color.FromArgb(255, 255, 0,
255).ToArgb)

This to Draw:
sprite.Draw(Texture, New Rectangle(0, 0, 275, 2000), New Vector3(0, 0,
0), New Vector3(0, 0, 0), Color.White)


Note that everything works fine, except that some file sizes are
distorted. (the file size I need that doesnt work is 1021x2997)

*** Sent via Developersdex http://www.developersdex.com ***

Author
3 Jul 2006 10:41 AM
Andrew Morton
A. Gaubatz wrote:
Show quoteHide quote
> I am using some code that I somewhat cannibalized from a tutorial, and
> whenever I try to put in an image not of specific seemingly random
> sizes, it stretches them out alot.
....
> I am then using code like this to display pictures to the screen.
>
> This to Create:
> Texture = createTexture("filename", Color.FromArgb(255, 255, 0,
> 255).ToArgb)
>
> This to Draw:
> sprite.Draw(Texture, New Rectangle(0, 0, 275, 2000), New Vector3(0, 0,
> 0), New Vector3(0, 0, 0), Color.White)
>
>
> Note that everything works fine, except that some file sizes are
> distorted. (the file size I need that doesnt work is 1021x2997)

It appears that you are trying to squash a 1021px x 2997px image into a
275px x 2000px box. You could either
a) Crop the image
or
b) Resize the image into the available space while keeping the same aspect
ratio.

Andrew