本文介绍了相比于所有其他窗口在Windows Server 2008 R2 64位.NET System.Drawing中的差异(图像被2008R2-64打破,但没有其他Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经拿到了开发PC已安装的Windows Server 2008 R2 64位(x86)的,所以它被用作工作站。我的开发环境是VS2008针对.NET3.5(YES, 2008 !)

I have been given a development PC that has Windows Server 2008R2 64bit (x86) installed, so it is used as a workstation. My development environment is VS2008 targeting .NET3.5 (YES, 2008!!!)

在我们的网站(现在是几岁)有一个内部开发的验证码。

In our website (which is a few years old now) there is a in-house developed Captcha.

要削减长话短说,在字母的不输出在我开发计算机2008R2-64但所有其他窗口(我们的测试和放大器;现场服务器2003 64位)和Virtual PC的(2008 32位NOT R2,Windows XP,Win7的),他们是。

To cut a long story short, the letters are not outputted on my dev machine 2008R2-64 but on all other windows (our test & live servers are 2003 64bit) and Virtual PC's (2008 32bit NOT R2, XP, Win7) they are.

我已经看到了网上的一些职位,说,这样的像素被测量的也许的已改变(很难找出一定的),但仅此而已。

I have seen some posts on the web that saying that the way pixels is measured might have changed (hard to find out for certain) but not much else.

下面是一个小型的Web应用程序来说明这个问题。

Here is a small web-application to illustrate the problem.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Captcha1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Random _randomNumberGenerator = new Random();

            /* Output random string */
            float _xPosition = 20;
            float _xPositionMax = 0;
            float _yPosition = 0;
            float _yPositionMax = 0;
            string strRandom = string.Empty;

            /* Prepare drawing functions to output */
            System.Drawing.Bitmap objBmp = new System.Drawing.Bitmap(500, 200);
            System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(objBmp);
            objGraphics.Clear(System.Drawing.Color.AliceBlue);
            objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            string[] strFont = new string[] { "Arial" };
            string[] strArray = new string[] { "a", "b", "c", "d", "e" };
            strRandom = "AAAAAA";//these are the characters for the captcha. In normal circumstances it would be random!

            /* Render the image */
            int _rotate = -1;
            objGraphics.RotateTransform(3);
            foreach (char _char in strRandom.ToCharArray())
            {
                /* Rotate the image */
                objGraphics.RotateTransform((float)(_rotate * 5));
                _rotate = (_rotate == 1) ? -1 : 1;

                /* Generate random y position */
                _yPosition = Convert.ToInt32(_randomNumberGenerator.Next(15, 20));

                /* Select random font */
                System.Drawing.Font objFont = new System.Drawing.Font("Arial", 22, System.Drawing.FontStyle.Bold);

                /* Output the next character */
                objGraphics.DrawString(_char.ToString(), objFont, System.Drawing.Brushes.Black, _xPosition, _yPosition);

                /* Get the width of the string added */
                System.Drawing.SizeF _stringSize = objGraphics.MeasureString(_char.ToString(), objFont);

                /* Dispose of the font */
                objFont.Dispose();

                /* Output the next character */
                objGraphics.DrawImage(objBmp, _xPosition, _yPosition);

                /* Increment x position */
                _xPosition = _xPosition + (_stringSize.Width * (float)0.8);

                /* Get bounding area */
                _xPositionMax = _xPosition;
                _yPositionMax = (int)(_yPosition + _stringSize.Height);
            }
            _xPositionMax += 20;
            _yPositionMax += 15;

            System.Drawing.Bitmap objBmpFinal = new System.Drawing.Bitmap((int)_xPositionMax, (int)_yPositionMax);
            System.Drawing.Graphics objGraphicsFinal = System.Drawing.Graphics.FromImage(objBmpFinal);
            System.Drawing.Rectangle _rectDest = new System.Drawing.Rectangle(0, 0, (int)_xPositionMax, (int)_yPositionMax);
            objGraphicsFinal.DrawImageUnscaledAndClipped(objBmp, _rectDest);

            /* Draw a curve over the image */
            float _yPositionMaxCurve = _yPositionMax - 20;
            System.Drawing.Pen _penCurve = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
            objGraphicsFinal.DrawBezier(_penCurve,
                _randomNumberGenerator.Next(5, 10), _randomNumberGenerator.Next(5, (int)_yPositionMaxCurve),
                _randomNumberGenerator.Next((int)(_xPositionMax / 6), (int)(_xPositionMax / 5)), _randomNumberGenerator.Next((int)(_yPositionMaxCurve / 2), (int)_yPositionMaxCurve),              // y2
                _randomNumberGenerator.Next((int)(_xPositionMax / 4), (int)(_xPositionMax / 3)), _randomNumberGenerator.Next(5, (int)_yPositionMaxCurve),                             // y3
                _randomNumberGenerator.Next((int)(_xPositionMax - 5), (int)_xPositionMax), _randomNumberGenerator.Next(5, (int)_yPositionMaxCurve));                            // y4
            _penCurve.Dispose();

            /* Draw diagonal lines */
            System.Drawing.Pen _penHatch = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            for (int _i = -100; _i < _xPositionMax; _i += ((int)_xPositionMax / 8))
            {
                float _bottomPos = ((float)Math.Tan((Math.PI * 45 / 180.0)) * _yPositionMax);
                objGraphicsFinal.DrawLine(_penHatch, _i, 0, _bottomPos + _i, _yPositionMax);
            }
            for (int _i = 0; _i < (_xPositionMax + 100); _i += ((int)_xPositionMax / 8))
            {
                float _bottomPos = ((float)Math.Tan((Math.PI * 45 / 180.0)) * _yPositionMax);
                objGraphicsFinal.DrawLine(_penHatch, _i, 0, ((-_bottomPos) + _i), _yPositionMax);
            }
            _penHatch.Dispose();

            /* Draw bounding rectangle */
            System.Drawing.Pen _penBorder = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
            objGraphicsFinal.DrawRectangle(_penBorder, 0, 0, (int)(_xPositionMax - 1), (int)(_yPositionMax - 1));
            _penBorder.Dispose();

            /* Draw random speckles */
            for (int _speckle = 0; _speckle < 100; _speckle++)
            {
                /* Generate a random colour */
                System.Drawing.Color _speckleColor = System.Drawing.Color.FromArgb((byte)_randomNumberGenerator.Next(0, 255), (byte)_randomNumberGenerator.Next(0, 255), (byte)_randomNumberGenerator.Next(0, 255));

                /* Create a brush */
                System.Drawing.SolidBrush _speckleBrush = new System.Drawing.SolidBrush(_speckleColor);

                /* Select random font */
                System.Drawing.Font _speckleFont = new System.Drawing.Font(strFont[_randomNumberGenerator.Next(0, (strFont.Length - 1))], _randomNumberGenerator.Next(2, 6), System.Drawing.FontStyle.Regular);

                /* Get speckle character */
                int i = Convert.ToInt32(_randomNumberGenerator.Next(0, (strArray.Length - 1)));
                string _speckleCharacter = strArray[i].ToString();

                /* Draw speckle */
                objGraphicsFinal.DrawString(_speckleCharacter, _speckleFont, _speckleBrush, _randomNumberGenerator.Next(0, (int)_xPositionMax), _randomNumberGenerator.Next(0, (int)_yPositionMax));

                /* Dispose of objects */
                _speckleBrush.Dispose();
                _speckleFont.Dispose();
            }

            /* Output the image */
            Response.ContentType = "image/GIF";
            objBmpFinal.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

            objGraphics.Dispose();
            objGraphicsFinal.Dispose();

            objBmpFinal.Dispose();
            objBmp.Dispose();

            /* End the response */
            Response.End();
        }
    }
}

和这里有一些图片显示它的的外观和它看起来像在Win 2008 R2 64。

and here are some images showing what it should look like and what it looks like on Win 2008R2 64.

应该怎么看待:

How it should look:

不工作!!!!

Not Working!!!!

我才刚刚拿到这台PC所以它可能是某些角色都没有接通(ASP.NET,.NET3.5,静态内容都被选中)。

I have only just got this PC so it's possible that some of the "roles" are not switched on (ASP.NET, .NET3.5, Static Content are all ticked).

任何人有什么好主意?

推荐答案

您需要分而治之这个问题。做到这一点的方法是从消除不影响您的输出(背景资料)的事情。然后消除随机因素(你可以种子的发电机,以便它为您提供了相同的结果),或使用常量。当你能得到它归结为当我有这条线在我没有得到任何的文字,但是当我把它拿出来,我有文字您已经找到了原因。

You need to divide and conquer this problem.The way to do this is to start eliminating the things that aren't affecting your output (background items). Then eliminate the random elements (you could seed your generator so that it gives you the same results), or use constants.When you can get it down to "when I have this line in I get no text, but when I take it out, I have text" you have located the cause.

它的乐趣,绿豆与周围的验证码,但你的方式,使得它很难调试和验证做到了。这种类型的code应该单元测试,并取得了单元测试,以便给出一个特定的起始种子,你得到一致的输出无处不在。

It's fun to mung around with the captcha, but you've done it in a way that makes it very hard to debug and verify. This type of code should be unit tested and made unit testable so that given a particular starting seed you get consistent output everywhere.

在我们的环境,我们有问题,现在又在哪里字体不渲染像素相同的,因为一个测试服务器的字体的一个版本,另一个则没有。我不认为这是你的问题。

In our environment, we have issues now and again where fonts don't render pixel-identical because one test server has one version of the font and another doesn't. I don't see that as your issue.

这篇关于相比于所有其他窗口在Windows Server 2008 R2 64位.NET System.Drawing中的差异(图像被2008R2-64打破,但没有其他Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:35