如何修复char的索引超出范围异常?

时间:2015-05-08 15:05:36

标签: c# .net winforms

首先是我自己没有这样做的项目我从github那里拿了它:

spazzarama/Direct3DHook

当我尝试使用任何directx11游戏时发生异常的问题。 例外情况发生在DXHookD3D11.cs

#region Draw overlay (after screenshot so we don't capture overlay as well)
                if (this.Config.ShowOverlay)
                {
                    // Initialise Overlay Engine
                    if (_swapChainPointer != swapChain.NativePointer || _overlayEngine == null)
                    {
                        if (_overlayEngine != null)
                            _overlayEngine.Dispose();

                        _overlayEngine = new DX11.DXOverlayEngine();
                        _overlayEngine.Overlays.Add(new Capture.Hook.Common.Overlay
                        {
                            Elements =
                            {
                                //new Capture.Hook.Common.TextElement(new System.Drawing.Font("Times New Roman", 22)) { Text = "Test", Location = new System.Drawing.Point(200, 200), Color = System.Drawing.Color.Yellow, AntiAliased = false},
                                new Capture.Hook.Common.FramesPerSecond(new System.Drawing.Font("Arial", 16)) { Location = new System.Drawing.Point(5,5), Color = System.Drawing.Color.Red, AntiAliased = true }
                            }
                        });
                        _overlayEngine.Initialise(swapChain);

                        _swapChainPointer = swapChain.NativePointer;
                    }
                    // Draw Overlay(s)
                    else if (_overlayEngine != null)
                    {
                        foreach (var overlay in _overlayEngine.Overlays)
                            overlay.Frame();
                        _overlayEngine.Draw();
                    }
                }
                #endregion

然后,当它尝试使用Draw()时,我使用了一个断点,它将DXOverlayEngine.cs类转到此行:

_spriteEngine.DrawString(textElement.Location.X, textElement.Location.Y, textElement.Text, textElement.Color.R, textElement.Color.G, textElement.Color.B, textElement.Color.A, font);

然后它会转到DXSprite.cs这个部分:

public void DrawString(int X, int Y, string text, int R, int G, int B, int A, DXFont F)
        {
            Color4 blendFactor = new Color4(1.0f);
            Color4 backupBlendFactor;
            int backupMask;
            var backupBlendState = _deviceContext.OutputMerger.GetBlendState(out backupBlendFactor, out backupMask);
            _deviceContext.OutputMerger.SetBlendState(_transparentBS, blendFactor);

            BeginBatch(F.GetFontSheetSRV());


            int length = text.Length;

            int posX = X;
            int posY = Y;

            Color4 color;
            Vector4 Vec = new Vector4(R > 0 ? (float)(R / 255.0f) : 0.0f, G > 0 ? (float)(G / 255.0f) : 0.0f, B > 0 ? (float)(B / 255.0f) : 0.0f, A > 0 ? (float)(A / 255.0f) : 0.0f);
            color = new Color4(Vec);

            for (int i = 0; i < length; ++i)
            {
                char character = text[i];

                if (character == ' ')
                    posX += F.GetSpaceWidth();
                else if (character == '\n')
                {
                    posX = X;
                    posY += F.GetCharHeight();
                }
                else
                {
                    Rectangle charRect = F.GetCharRect(character);

最后它在DXFont.cs类中执行F.GetCharRect(字符)行:

public Rectangle GetCharRect(char c)
        {
            Debug.Assert(_initialized);

            return _charRects[c - StartChar];
        }

我在方法GetCharRect上使用断点,例如在这种情况下,变量c值为:163'£,变量StartChar中的值为33'!'

然后它显示了该行的异常:

return _charRects[c - StartChar];

我试图向店主询问这个问题,到目前为止,他告诉我的是:

“听起来它正试图画一个它没有准备的角色......”

我试图尽可能地缩小代码,但它们都是相互连接的。

1 个答案:

答案 0 :(得分:2)

查看DXFont.cs看起来它只准备了使用33到127之间的ASCII代码的字符

     const char StartChar = (char)33; 
     const char EndChar = (char)127; 
     const uint NumChars = EndChar - StartChar; 

_charRects根据此范围在BuildFontSheetBitmap期间设置 string input = "(src=\"cid:image001.png@01D081C1.C5908B40\")"; string replacement = "src=\"\\\\resources\\\\images\\\\$1\""; Regex rgx = new Regex("src=\"cid:(image[0-9]+\\.png)@[A-Za-z0-9]+\\.[A-Za-z0-9]+\""); Console.WriteLine(rgx.Replace(input, replacement)); 。您需要扩展范围以包括要渲染的所有字符