我是在C#的环境下开发的,使用Managed DirectX 9.0c的SDK。关于Draw2D的重载方式,不明白的可以见以下链接:
http://www.cnblogs.com/T-MAC/archive/2007/08/09/848793.html下面是我输出的语句:sprite.Draw2D(
    Background1, //是一张800*600的图片,以Texture的形式加载入了D3D设备。
    new Rectangle(0, 0, 1024, 768), //原图片裁剪,四个参数分别是(X、Y,宽,高)
    new Rectangle(0, 0, 800, 600),  //目标图片尺寸。
    new Point(0, 0), //放入2D空间的坐标
    Color.White //透明颜色
);现在我遇到的问题是:无论如何设置,载入的图片总是变形的。我的展示空间是800*600,屏幕分辨率是1024*768。
Background1载入的图片是以800*600做的,但无论我如何设置,它总是以放大的形式出现在展示框中。请问我应该如何设置才能让这个图片保持原大?谢谢大家了。

解决方案 »

  1.   

    既然原图是800*600的,为什么原图又裁剪为new Rectangle(0, 0, 1024, 768)?用new Rectangle(0, 0, 800, 600)是什么效果?http://www.cnblogs.com/T-MAC/archive/2007/08/09/848793.html 
    这个上好像没有你写的Draw2D签名的方法呀
      

  2.   


    是啊,我也发现了,所有的Draw2D范例在我的机子上都要改一下输出代码才能编译通过。不知道是不是我的SDK有问题,我是用最新版(今年6月)的Managed DirectX 9.0C版本的SDK包。DLL的版本使用的是“1.0.2902.0”版。
      

  3.   


    这个是我DirectX类的接口,很奇怪与大家的不一样
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   rotationCenter:
            //     A System.Drawing.Point object that represents the sprite's rotational center.
            //
            //   rotationAngle:
            //     A floating point value that specifies the rotation angle around the rotational
            //     center of the sprite.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     A System.Drawing.Color
            public void Draw2D(Texture srcTexture, Point rotationCenter, float rotationAngle, Point position, Color color);
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   rotationCenter:
            //     A System.Drawing.Point object that represents the sprite's rotational center.
            //
            //   rotationAngle:
            //     A floating point value that specifies the rotation angle around the rotational
            //     center of the sprite.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     Color value represented as an integer.
            public void Draw2D(Texture srcTexture, Point rotationCenter, float rotationAngle, Point position, int color);
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   srcRectangle:
            //     A System.Drawing.Rectangle object that indicates the portion of the source
            //     texture to use for the sprite. Specify System.Drawing.Rectangle.Empty to
            //     use the entire source image for the sprite.
            //
            //   destinationRectangle:
            //     A System.Drawing.Rectangle object that indicates the size of the destination
            //     rectangle.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     A System.Drawing.Color
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point position, Color color);
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   srcRectangle:
            //     A System.Drawing.Rectangle object that indicates the portion of the source
            //     texture to use for the sprite. Specify System.Drawing.Rectangle.Empty to
            //     use the entire source image for the sprite.
            //
            //   destinationRectangle:
            //     A System.Drawing.Rectangle object that indicates the size of the destination
            //     rectangle.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     Color value represented as an integer.
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point position, int color);
            [CLSCompliant(false)]
            public void Draw2D(Texture srcTexture, Rectangle* srcRectangle, Rectangle* destinationRectangle, Point* center, float rotationAngle, Point position, int color);
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   srcRectangle:
            //     A System.Drawing.Rectangle object that indicates the portion of the source
            //     texture to use for the sprite. Specify System.Drawing.Rectangle.Empty to
            //     use the entire source image for the sprite.
            //
            //   destinationRectangle:
            //     A System.Drawing.Rectangle object that indicates the size of the destination
            //     rectangle.
            //
            //   rotationCenter:
            //     A System.Drawing.Point object that represents the sprite's rotational center.
            //
            //   rotationAngle:
            //     A floating point value that specifies the rotation angle around the rotational
            //     center of the sprite.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     A System.Drawing.Color
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point rotationCenter, float rotationAngle, Point position, Color color);
            //
            // 摘要:
            //     Adds a sprite to the list of batched sprites.
            //
            // 参数:
            //   srcTexture:
            //     A Microsoft.DirectX.Direct3D.Texture object that represents the sprite texture.
            //
            //   srcRectangle:
            //     A System.Drawing.Rectangle object that indicates the portion of the source
            //     texture to use for the sprite. Specify System.Drawing.Rectangle.Empty to
            //     use the entire source image for the sprite.
            //
            //   destinationRectangle:
            //     A System.Drawing.Rectangle object that indicates the size of the destination
            //     rectangle.
            //
            //   center:
            //     A Microsoft.DirectX.Vector3 structure that identifies the center of the sprite.
            //     A value of (0,0) indicates the upper-left corner.
            //
            //   rotationAngle:
            //     A floating point value that specifies the rotation angle around the rotational
            //     center of the sprite.
            //
            //   position:
            //     A System.Drawing.Point object that represents the position of the sprite
            //     in 2D-space.
            //
            //   color:
            //     Color value represented as an integer.
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point rotationCenter, float rotationAngle, Point position, int color);
      

  4.   


    大家可以看上面的接口,在网上找来的Draw2D范例中边界与大小都是使用PointF和SizeF但我使用这个DirectX接口上都是使用Point与Size整型是我版本用错了吗?上面那个接口是在“Microsoft.DirectX.Direct3D.Sprite”类下的。
      

  5.   

    你上面写的这些代码哪儿来的?
    Draw2D这个不是本身已经提供了么
    public sealed class Sprite : System.MarshalByRefObject
        Microsoft.DirectX.Direct3D 的成员
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.PointF rotationCenter, float rotationAngle, System.Drawing.PointF position, int color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.PointF rotationCenter, float rotationAngle, System.Drawing.PointF position, System.Drawing.Color color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.Rectangle srcRectangle, System.Drawing.SizeF destinationSize, System.Drawing.PointF rotationCenter, float rotationAngle, System.Drawing.PointF position, int color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.Rectangle srcRectangle, System.Drawing.SizeF destinationSize, System.Drawing.PointF center, float rotationAngle, System.Drawing.PointF position, int color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.Rectangle srcRectangle, System.Drawing.SizeF destinationSize, System.Drawing.PointF rotationCenter, float rotationAngle, System.Drawing.PointF position, System.Drawing.Color color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.Rectangle srcRectangle, System.Drawing.SizeF destinationSize, System.Drawing.PointF position, int color)
    public void Draw2D(Microsoft.DirectX.Direct3D.Texture srcTexture, System.Drawing.Rectangle srcRectangle, System.Drawing.SizeF destinationSize, System.Drawing.PointF position, System.Drawing.Color color)
      

  6.   

    真的奇怪了,你看我的:namespace Microsoft.DirectX.Direct3D
    {
        public sealed class Sprite : MarshalByRefObject, IDisposable
        {
            public void Draw2D(Texture srcTexture, Point rotationCenter, float rotationAngle, Point position, Color color);
            public void Draw2D(Texture srcTexture, Point rotationCenter, float rotationAngle, Point position, int color);
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point position, Color color);
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point position, int color);
            [CLSCompliant(false)]
            public void Draw2D(Texture srcTexture, Rectangle* srcRectangle, Rectangle* destinationRectangle, Point* center, float rotationAngle, Point position, int color);
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point rotationCenter, float rotationAngle, Point position, Color color);
            public void Draw2D(Texture srcTexture, Rectangle srcRectangle, Rectangle destinationRectangle, Point rotationCenter, float rotationAngle, Point position, int color);
        }}
      

  7.   

    整形或者浮点型的点的作用差不多,关系不大,浮点型的点是为了计算过程中间精度不丢失设计的,对你这个问题,Point还是PointF无所谓。
    我不明白的是你的第二个参数srcRectangle,你为什么要设置成new Rectangle(0, 0, 1024, 768), 你试试用System.Drawing.Rectangle.Empty取整张图片试试啊。
      

  8.   


    谢谢楼上的答复。刚才试了下,用Rectangle.Empty的话,是整张图片都不取。(显示出来是空的)
      

  9.   

    Specify System.Drawing.Rectangle.Empty to
    use the entire source image for the sprite.按照函数注释,用Rectangle.Empty的话应该是整张图片啊?
    是第二个参数srcRectangle而不是第三个参数啊
    要再不行试试改成 Rectangle(0, 0, 800, 600)
      

  10.   


    变形解决了,在载入纹理的时候。Background1 = TextureLoader.FromFile(Engine.device, FileName);
    //改成了
    Background1 = TextureLoader.FromFile(Engine.device, FileName, 800, 600, 0, Usage.None, Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, 0);在纹理中指定了图片的大小就解决了问题,但这样也太麻烦了,我在载入图片的时候怎么知道它的大小呢?微软应该不会这么不人性化吧,本来大家按正常概念想都应该是OK的问题,到这里怎么会出现变形呢。为什么会出现这样的问题?请教大家,谢谢了。
      

  11.   

    我觉得 Background1 = TextureLoader.FromFile(Engine.device, FileName); 这个应该没错,可能还是绘图的地方有点问题。
    这儿写了,和你错误的地方可能正好抵消,就正常了
      

  12.   


    奇怪了,能帮看看您们用的Managed DirectX的版本是多少吗?我的Sprite.Draw2D接口就是与你们的不一样~~#_#谢谢了。
      

  13.   

    你下载的不是最新版的?
    我的是directx_jun2008_redist.exe
      

  14.   

    对了,我的SDK是March2008的,里面只有c++的帮助文件和例程,没有c#的,你们的版本里面有嘛?
      

  15.   


    是的,但我所有的控件都是选择1.0.2902.0,
    包括有可以选择多个版本的Direct3DX....
    我换个版本试试,一会回来跟帖,谢谢了。
      

  16.   

    我也很奇怪,以前的版本有.net的示例,现在的新的好像没有例子了,全是C++的。
      

  17.   

    是哦,初期简单的还行,深入就不行了,WPF或许应该考虑了
      

  18.   


    刚才换了版本,方法不一致的问题解决了。原来我想引用的几个库同一个版本兼容是否会好一些,于是3DX就选择了和DirectX同样的1.0.2902.0,
    不曾想还会有这样的问题。倒唉~~接口的问题是没有了,不过变形是依旧的,不知道会不会是我这台本本显卡的问题,一会我发个包到同事电脑上去试试。谢谢大家了。PS:最新带C#范例的是DirectX 9.0b的一个SDK。而且是For .Net1.1的,和9.0c接口完全不同,根本没办法用。
      

  19.   


    那个不算解决啊,如果按上面那样解决的话,我要载入的时候就要知道图片的大小啊。难道我要再用一个ImageLoader去建一个实例,然后取出大小来放到TextureLoader里?不可能这么麻烦的吧完全是画蛇添足。
      

  20.   


    原因找到了,原来D3D在上载纹理的时候,如果不指定,系统都会自动把图片大小改为2倍数的正方型。比如,2*2,4*4,256*256,512*512,1024*1024.。。我原图是800*600的,于是就被改成了1024*1024所以大家在使用DirectX上传纹理的时候,尽量都使用2倍数的正方型。我一会再想想办法。