Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:14:37+00:00 2026-05-31T14:14:37+00:00

I have a winforms application which uses xna to act as a 2d editor

  • 0

I have a winforms application which uses xna to act as a 2d editor of sorts. I basically have a custom control which wraps up a newly created GraphicsDevice (using the panels handle and width/height to create the viewport) and draw loop.

Now every time I try to use this control I keep getting an exception saying:

The viewport is invalid. The viewport cannot be larger than or outside the bounds of the current render target.The MinDepth and MaxDepth but be between 0 and 1

I am a bit baffled as I an creating a GraphicsDevice like so:

public class GraphicsDeviceBuilder
{
    private Viewport viewport;
    private PresentationParameters presentationParameters;
    private GraphicsProfile graphicsProfile;
    private GraphicsAdapter graphicsAdapter;

    private void ResetBuilder()
    {
        viewport = new Viewport(0, 0, 128, 128);
        presentationParameters = new PresentationParameters();
        presentationParameters.BackBufferFormat = SurfaceFormat.Color;
        presentationParameters.DepthStencilFormat = DepthFormat.Depth24;
        presentationParameters.PresentationInterval = PresentInterval.Immediate;
        presentationParameters.IsFullScreen = false;
        graphicsProfile = GraphicsProfile.Reach;
        graphicsAdapter = GraphicsAdapter.DefaultAdapter;
    }

    public GraphicsDeviceBuilder Create()
    {
        ResetBuilder();
        return this;
    }

    public GraphicsDeviceBuilder WithViewport(Viewport viewport)
    {
        this.viewport = viewport;
        return this;
    }

    public GraphicsDeviceBuilder WithPresentationParameters(PresentationParameters presentationParameters)
    {
        this.presentationParameters = presentationParameters;
        return this;
    }

    public GraphicsDeviceBuilder WithGraphicsProfile(GraphicsProfile graphicsProfile)
    {
        this.graphicsProfile = graphicsProfile;
        return this;
    }

    public GraphicsDeviceBuilder WithGraphicsAdapter(GraphicsAdapter graphicsAdapter)
    {
        this.graphicsAdapter = graphicsAdapter;
        return this;
    }

    public GraphicsDevice Build(IntPtr handle)
    {
        presentationParameters.DeviceWindowHandle = handle;
        presentationParameters.BackBufferWidth = viewport.Width;
        presentationParameters.BackBufferHeight = viewport.Height;
        return new GraphicsDevice(graphicsAdapter, graphicsProfile, presentationParameters)
        {
            Viewport = viewport
        };
    }
}

Then in the panel that encapsulates this created device:

private void SetupGraphics(GraphicsDeviceBuilder graphicsDeviceBuilder)
{
    var viewport = new Viewport()
    {
        X = 0,
        Y = 0,
        Width = Math.Max(Width, 1),
        Height = Math.Max(Height, 1),
        MinDepth = 0.0f,
        MaxDepth = 1.0f
    };
    GraphicsDevice = graphicsDeviceBuilder.Create().WithViewport(viewport).Build(Handle);
}

I have also got a method which will Reset() the GraphicsDevice on Resizing the form, it also updates the height/width if required, which looks like this:

private void ResetDevice()
{
    if (GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost)
    { throw new DeviceLostException("Cannot regain access to video hardware"); }

    var safeWidth = Math.Max(Width, 1);
    var safeHeight = Math.Max(Height, 1);
    var newViewport = new Viewport(0, 0, safeWidth, safeHeight) { MinDepth = 0.0f, MaxDepth = 1.0f };

    GraphicsDevice.Viewport = newViewport;
    GraphicsDevice.PresentationParameters.BackBufferWidth = newViewport.Width;
    GraphicsDevice.PresentationParameters.BackBufferHeight = newViewport.Height;
    GraphicsDevice.PresentationParameters.DeviceWindowHandle = Handle;
    GraphicsDevice.Reset();
}

The error implies that the viewport’s size is invalid (which upon debugging seems false as it matches the size of the panel) and also has an invalid depth, but as you can see it is set to 0 and 1, making it within the range (debugging also proves this to be true).

When running at runtime I do not receive an error like in the designer, but I do just get a red cross up on the panel making me think it is not working there either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:14:38+00:00Added an answer on May 31, 2026 at 2:14 pm

    I have changed over a few things but the issue for me seemed to be around the fact that I was setting the viewport before a reset, and I also needed to pass the PresentationParameters into the reset method or they had no effect, so something like this:

    private void ResetDevice()
    {
        var safeWidth = Math.Max(Width, 1);
        var safeHeight = Math.Max(Height, 1);
        var newViewport = new Viewport(0, 0, safeWidth, safeHeight) { MinDepth = 0.0f, MaxDepth = 1.0f };
    
        var presentationParams = GraphicsDevice.PresentationParameters;
        presentationParams.BackBufferWidth = safeWidth;
        presentationParams.BackBufferHeight = safeHeight;
        presentationParams.DeviceWindowHandle = Handle;
        GraphicsDevice.Reset(presentationParams);
    
        GraphicsDevice.Viewport = newViewport;
    
        if (GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost)
        { throw new DeviceLostException("Cannot regain access to video hardware"); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have winforms application which uses nhibernate and active records to access the database.
I have a c# .net 2.0 winforms application which consists basically of one form
I have a WinForms application which uses a list box to display list of
I have a drawing application developed in winforms C# which uses many System.Drawing.Bitmap object
We have a WinForms application which runs on a touch-screen on a bit of
I have a winforms application in which I am using 2 Forms to display
I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to
I have a Winforms application created in Visual Studio 2005 Pro, it connects to
I have a WinForms application in which I have hosted a web page inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.