{"id":1927,"date":"2012-01-13T00:34:51","date_gmt":"2012-01-13T00:34:51","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1927"},"modified":"2012-01-13T00:34:51","modified_gmt":"2012-01-13T00:34:51","slug":"%e9%aa%8c%e8%af%81%e7%a0%81%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2012\/01\/13\/%e9%aa%8c%e8%af%81%e7%a0%81%e8%af%86%e5%88%ab\/","title":{"rendered":"\u9a8c\u8bc1\u7801\u8bc6\u522b"},"content":{"rendered":"<pre escaped=\"true\" lang=\"csharp\">\r\n\u53c2\u8003\uff1ahttp:\/\/www.cnblogs.com\/yuanbao\/archive\/2007\/09\/25\/905322.html\r\n\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing System.Collections;\r\nusing System.Drawing;\r\nusing System.Drawing.Imaging;\r\nusing System.Runtime.InteropServices;\r\n\r\nnamespace BallotAiying2\r\n{\r\n    class UnCodebase\r\n    {\r\n        public Bitmap bmpobj;\r\n        public UnCodebase(Bitmap pic)\r\n        {\r\n            bmpobj = new Bitmap(pic);    \/\/\u8f6c\u6362\u4e3aFormat32bppRgb\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u6839\u636eRGB\uff0c\u8ba1\u7b97\u7070\u5ea6\u503c\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"posClr\"&gt;Color\u503c&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u7070\u5ea6\u503c\uff0c\u6574\u578b&lt;\/returns&gt;\r\n        private int GetGrayNumColor(System.Drawing.Color posClr)\r\n        {\r\n            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) &gt;&gt; 16;\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u7070\u5ea6\u8f6c\u6362,\u9010\u70b9\u65b9\u5f0f\r\n        \/\/\/ &lt;\/summary&gt;\r\n        public void GrayByPixels()\r\n        {\r\n            for (int i = 0; i &lt; bmpobj.Height; i++)\r\n            {\r\n                for (int j = 0; j &lt; bmpobj.Width; j++)\r\n                {\r\n                    int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));\r\n                    bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));\r\n                }\r\n            }\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u53bb\u56fe\u5f62\u8fb9\u6846\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"borderWidth\"&gt;&lt;\/param&gt;\r\n        public void ClearPicBorder(int borderWidth)\r\n        {\r\n            for (int i = 0; i &lt; bmpobj.Height; i++)\r\n            {\r\n                for (int j = 0; j &lt; bmpobj.Width; j++)\r\n                {\r\n                    if (i &lt; borderWidth || j &lt; borderWidth || j &gt; bmpobj.Width - 1 - borderWidth || i &gt; bmpobj.Height - 1 - borderWidth)\r\n                        bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));\r\n                }\r\n            }\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u7070\u5ea6\u8f6c\u6362,\u9010\u884c\u65b9\u5f0f\r\n        \/\/\/ &lt;\/summary&gt;\r\n        public void GrayByLine()\r\n        {\r\n            Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);\r\n            BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);\/\/ PixelFormat.Format32bppPArgb);\r\n            \/\/    bmpData.PixelFormat = PixelFormat.Format24bppRgb;\r\n            IntPtr scan0 = bmpData.Scan0;\r\n            int len = bmpobj.Width * bmpobj.Height;\r\n            int[] pixels = new int[len];\r\n            Marshal.Copy(scan0, pixels, 0, len);\r\n\r\n            \/\/\u5bf9\u56fe\u7247\u8fdb\u884c\u5904\u7406\r\n            int GrayValue = 0;\r\n            for (int i = 0; i &lt; len; i++)\r\n            {\r\n                GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));\r\n                pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb();      \/\/Color\u8f6cbyte\r\n            }\r\n\r\n            bmpobj.UnlockBits(bmpData);\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u5f97\u5230\u6709\u6548\u56fe\u5f62\u5e76\u8c03\u6574\u4e3a\u53ef\u5e73\u5747\u5206\u5272\u7684\u5927\u5c0f\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"dgGrayValue\"&gt;\u7070\u5ea6\u80cc\u666f\u5206\u754c\u503c&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"CharsCount\"&gt;\u6709\u6548\u5b57\u7b26\u6570&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public void GetPicValidByValue(int dgGrayValue, int CharsCount)\r\n        {\r\n            int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;\r\n            int posx2 = 0; int posy2 = 0;\r\n            for (int i = 0; i &lt; bmpobj.Height; i++)      \/\/\u627e\u6709\u6548\u533a\r\n            {\r\n                for (int j = 0; j &lt; bmpobj.Width; j++)\r\n                {\r\n                    int pixelValue = bmpobj.GetPixel(j, i).R;\r\n                    if (pixelValue &lt; dgGrayValue)     \/\/\u6839\u636e\u7070\u5ea6\u503c\r\n                    {\r\n                        if (posx1 &gt; j) posx1 = j;\r\n                        if (posy1 &gt; i) posy1 = i;\r\n\r\n                        if (posx2 &lt; j) posx2 = j;\r\n                        if (posy2 &lt; i) posy2 = i;\r\n                    };\r\n                };\r\n            };\r\n            \/\/ \u786e\u4fdd\u80fd\u6574\u9664\r\n            int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;   \/\/\u53ef\u6574\u9664\u7684\u5dee\u989d\u6570\r\n            if (Span &lt; CharsCount)\r\n            {\r\n                int leftSpan = Span \/ 2;    \/\/\u5206\u914d\u5230\u5de6\u8fb9\u7684\u7a7a\u5217 \uff0c\u5982span\u4e3a\u5355\u6570,\u5219\u53f3\u8fb9\u6bd4\u5de6\u8fb9\u59271\r\n                if (posx1 &gt; leftSpan)\r\n                    posx1 = posx1 - leftSpan;\r\n                if (posx2 + Span - leftSpan &lt; bmpobj.Width)\r\n                    posx2 = posx2 + Span - leftSpan;\r\n            }\r\n            \/\/\u590d\u5236\u65b0\u56fe\r\n            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);\r\n            bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);\r\n        }\r\n        \r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u5f97\u5230\u6709\u6548\u56fe\u5f62,\u56fe\u5f62\u4e3a\u7c7b\u53d8\u91cf\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"dgGrayValue\"&gt;\u7070\u5ea6\u80cc\u666f\u5206\u754c\u503c&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"CharsCount\"&gt;\u6709\u6548\u5b57\u7b26\u6570&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public void GetPicValidByValue(int dgGrayValue)\r\n        {\r\n            int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;\r\n            int posx2 = 0; int posy2 = 0;\r\n            for (int i = 0; i &lt; bmpobj.Height; i++)      \/\/\u627e\u6709\u6548\u533a\r\n            {\r\n                for (int j = 0; j &lt; bmpobj.Width; j++)\r\n                {\r\n                    int pixelValue = bmpobj.GetPixel(j, i).R;\r\n                    if (pixelValue &lt; dgGrayValue)     \/\/\u6839\u636e\u7070\u5ea6\u503c\r\n                    {\r\n                        if (posx1 &gt; j) posx1 = j;\r\n                        if (posy1 &gt; i) posy1 = i;\r\n\r\n                        if (posx2 &lt; j) posx2 = j;\r\n                        if (posy2 &lt; i) posy2 = i;\r\n                    };\r\n                };\r\n            };\r\n            \/\/\u590d\u5236\u65b0\u56fe\r\n            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);\r\n            bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u5f97\u5230\u6709\u6548\u56fe\u5f62,\u56fe\u5f62\u7531\u5916\u9762\u4f20\u5165\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"dgGrayValue\"&gt;\u7070\u5ea6\u80cc\u666f\u5206\u754c\u503c&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"CharsCount\"&gt;\u6709\u6548\u5b57\u7b26\u6570&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)\r\n        {\r\n            int posx1 = singlepic.Width; int posy1 = singlepic.Height;\r\n            int posx2 = 0; int posy2 = 0;\r\n            for (int i = 0; i &lt; singlepic.Height; i++)      \/\/\u627e\u6709\u6548\u533a\r\n            {\r\n                for (int j = 0; j &lt; singlepic.Width; j++)\r\n                {\r\n                    int pixelValue = singlepic.GetPixel(j, i).R;\r\n                    if (pixelValue &lt; dgGrayValue)     \/\/\u6839\u636e\u7070\u5ea6\u503c\r\n                    {\r\n                        if (posx1 &gt; j) posx1 = j;\r\n                        if (posy1 &gt; i) posy1 = i;\r\n\r\n                        if (posx2 &lt; j) posx2 = j;\r\n                        if (posy2 &lt; i) posy2 = i;\r\n                    };\r\n                };\r\n            };\r\n            \/\/\u590d\u5236\u65b0\u56fe\r\n            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);\r\n            return singlepic.Clone(cloneRect, singlepic.PixelFormat);\r\n        }\r\n        \r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u5e73\u5747\u5206\u5272\u56fe\u7247\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"RowNum\"&gt;\u6c34\u5e73\u4e0a\u5206\u5272\u6570&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"ColNum\"&gt;\u5782\u76f4\u4e0a\u5206\u5272\u6570&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;\u5206\u5272\u597d\u7684\u56fe\u7247\u6570\u7ec4&lt;\/returns&gt;\r\n        public Bitmap [] GetSplitPics(int RowNum,int ColNum)\r\n        {\r\n            if (RowNum == 0 || ColNum == 0)\r\n                return null;\r\n            int singW = bmpobj.Width \/ RowNum;\r\n            int singH = bmpobj.Height \/ ColNum;\r\n            Bitmap [] PicArray=new Bitmap[RowNum*ColNum];\r\n\r\n            Rectangle cloneRect;\r\n            for (int i = 0; i &lt; ColNum; i++)      \/\/\u627e\u6709\u6548\u533a\r\n            {\r\n                for (int j = 0; j &lt; RowNum; j++)\r\n                {\r\n                    cloneRect = new Rectangle(j*singW, i*singH, singW , singH);\r\n                    PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);\/\/\u590d\u5236\u5c0f\u5757\u56fe\r\n                }\r\n            }\r\n            return PicArray;\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u8fd4\u56de\u7070\u5ea6\u56fe\u7247\u7684\u70b9\u9635\u63cf\u8ff0\u5b57\u4e32\uff0c1\u8868\u793a\u7070\u70b9\uff0c0\u8868\u793a\u80cc\u666f\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"singlepic\"&gt;\u7070\u5ea6\u56fe&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"dgGrayValue\"&gt;\u80cc\u524d\u666f\u7070\u8272\u754c\u9650&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)\r\n        {\r\n            Color piexl;\r\n            string code = \"\";\r\n            for (int posy = 0; posy &lt; singlepic.Height; posy++)\r\n                for (int posx = 0; posx &lt; singlepic.Width; posx++)\r\n                {\r\n                    piexl = singlepic.GetPixel(posx, posy);\r\n                    if (piexl.R &lt; dgGrayValue)    \/\/ Color.Black )\r\n                        code = code + \"1\";\r\n                    else\r\n                        code = code + \"0\";\r\n                }\r\n            return code;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1ahttp:\/\/www.cnblogs.com\/yuanbao\/archive\/2007\/09\/25\/90 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1927","post","type-post","status-publish","format-standard","hentry","category-csharp"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1927"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1927\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}