山寨版百度识图之大学生课余学习计算机知识

       这种东西还是第一次写,主要是因为第一次做出了点东西,收获了不少。而且之前做过数据库的东西现在再想用的时候去发现全都忘了,所以就更要写一下了。

       首先写一下制作学习构成的心得吧,小弟今年大三,这个项目是老师大二下学期布置的,怎么算都有半年了,起初我的小组有四名成员,但现在做完了就只有我一个人了,他们的推出我很理解,因为我也一度想要退出。我上的只是一个二本大学,到目前为只是学完了c语言和数据结构,刚接触时老师让我看看sql server和c#,我差不多各看了一本书,但是一到要做的时候还是完全不会,在网上看别人写的东西也是一行都读不懂。我是在这个时候想要放弃的,这时候可能是多媒体老师无意间启发了我,他说人学东西最快的方式是看视频,上课,做东西最快的方法是把报错都发到百度上,宿舍也有一位同学之前做过类似的东西(虽然还没有什么成果),但是他的方法也是这种哪里不会就百度的方法。而我尝试了一下发现确实是一个很有效的方法,忽然间想嘲笑自己原来一有不懂的地方就抱着一本书看,每次都是从头看,哈哈。而且这学期学了JAVA这门课,因为是面向对象的,瞬间就看懂了类的概念,还有一些异常类,感觉这门课对我帮助很大,虽然只学到一半。

        起初做这个识图程序的时候想得很美好,总是想一口气做完,后来把功能都加进去发下很多错误,却又找不到。我因此也就把程序分成了几个小功能。

        首先是选择图片

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog opnDlg = new OpenFileDialog();//创建OpenFileDialog对象

            //为图像选择一个筛选器

            opnDlg.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +

                "*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +

                "位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +

                "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";

            opnDlg.Title = "打开图像文件";

            opnDlg.ShowHelp = true;//启动帮助按钮

            if (opnDlg.ShowDialog() == DialogResult.OK)
            {

                curFileName = opnDlg.FileName;

                try
                {

                    curBitmap1 = (Bitmap)Image.FromFile(curFileName);//使用Image.FromFile创建图像对象
                    img = Image.FromFile(curFileName);

                    imgOutput = new Bitmap(img, 256, 256);

                }

                catch (Exception exp)
                {

                    MessageBox.Show(exp.Message);

                }

            }

            //使控件的整个图面无效并导致重绘控件

            Invalidate();//对窗体进行重新绘制,这将强制执行Paint事件处理程序
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g1 = e.Graphics;//获取Graphics对象


            if (curBitmap1 != null)
            {

                g1.DrawImage(curBitmap1, 0, 0, 256, 256);//这里的参数分别为Image,位置的x,y,大小的长,高

            }
        }
           这里应该没什么好说的,不过话说让我自己写的话应该费劲,哈哈,我是在这里学习的 http://www.csharpwin.com/csharpspace/13444r4591.shtml,这篇文章是一个灰度化图片的实例,我也意识到我如果用一个M单位大小的图片会很慢,同时也是受了另一篇文章的影响决定要模仿一下,缩小图片大小。 http://hi.baidu.com/sdfiyon/item/26e84fa72bdf0ed95af19135

       缩小图片我最初看的是这篇文章http://www.alixixi.com/program/a/2008020624216.shtml,但是后面的几种按照比例缩放实验时不知道为什么总是报错,不过第一种方法还是很好用的,就是图片可能就走形了,哈哈。之后我看了这篇文章http://blog.csdn.net/jiangxinyu/article/details/7968139,真的可以说是获益匪浅,不仅验证了前段时间看的缩小图片的方法,还学到了最核心的相似度对比方法。模仿他那篇文章,我做了一个实时对比两张图片相似度的小程序。


        突然想起一件很好笑的事,因为不懂窗体程序怎么输出,想检验结果又不会,最后不得已用记事本输出结果,话说记事本的存储也是现学的。记事本的存储网上还是很多的,也很简单。

 string filepath = "d:\\1.txt";//文件路径和文件名字
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }

            if (!File.Exists(filepath))//如果文件不存在就创建
            {
                FileStream fs = new FileStream(filepath, FileMode.Create);
                fs.Close();
            }
            StreamWriter sw = new StreamWriter(filepath, true, Encoding.Default);
            //通过流将字符串写入txt文件
            for (int i = 1; i < 257; i++)
            {
                int t=i-1;
                sw.Write(" histogram["+t+"] = reader.GetInt32(reader.GetOrdinal(\"No");
                sw.Write(i + "\"));");
                sw.WriteLine();
            }
            sw.Close();
           上面那段代码还有一段unsafe的代码段,解决的办法也是在网上找到的, http://www.fangsi.net/archives/514.html

           下面我就要进入到sql server部分了,我的目标是把很多张照片的直方图值和完整路径存入数据库。其实数据库我之前是看了挺多的,而且也做了一些练习,但是因为暑假一段时间根本没看,就忘了大部分,当时也没写过什么让自己记住的东西。搞得我弄个存储过程还要在网上找半天。我的思路是建一张有257列的表,一列是路径,剩下的256列存的是直方图数组的每一个值,这样存起来极其麻烦,但是我一时想不出更好的方法,建表的时候就麻烦死了,要写257列呀,我有了上面那个记事本的小程序的循环写的,省了点儿事。传建表格我本来是会的但是也忘了,是这篇文章提醒了我。http://tech.ddvip.com/2008-08/121912976855706.html

       我首先实现的是把一张固定路径的图片的直方图值和路径存到数据库。

 string smallFileName = "d:\\1.bmp";
            string curFileName = "D:\\1.jpg";
            curBitmap = (Bitmap)Image.FromFile(curFileName);//使用Image.FromFile创建图像对象
            smallBitmap = Resize(curFileName, smallFileName);
            int[] histogram = new int[256];
            histogram = GetHisogram(smallBitmap);



            cn.Open();
            SqlCommand insertCommand = cn.CreateCommand();
            insertCommand.CommandText = "execute addinformation @path,@No1,@No2,@No3,@No4,@No5,@No6,@No7,@No8,@No9,@No10,@No11,@No12,@No13,@No14,@No15,@No16,@No17,@No18,@No19,@No20,@No21,@No22,@No23,@No24,@No25,@No26,@No27,@No28,@No29,@No30,@No31,@No32,@No33,@No34,@No35,@No36,@No37,@No38,@No39,@No40,@No41,@No42,@No43,@No44,@No45,@No46,@No47,@No48,@No49,@No50,@No51,@No52,@No53,@No54,@No55,@No56,@No57,@No58,@No59,@No60,@No61,@No62,@No63,@No64,@No65,@No66,@No67,@No68,@No69,@No70,@No71,@No72,@No73,@No74,@No75,@No76,@No77,@No78,@No79,@No80,@No81,@No82,@No83,@No84,@No85,@No86,@No87,@No88,@No89,@No90,@No91,@No92,@No93,@No94,@No95,@No96,@No97,@No98,@No99,@No100,@No101,@No102,@No103,@No104,@No105,@No106,@No107,@No108,@No109,@No110,@No111,@No112,@No113,@No114,@No115,@No116,@No117,@No118,@No119,@No120,@No121,@No122,@No123,@No124,@No125,@No126,@No127,@No128,@No129,@No130,@No131,@No132,@No133,@No134,@No135,@No136,@No137,@No138,@No139,@No140,@No141,@No142,@No143,@No144,@No145,@No146,@No147,@No148,@No149,@No150,@No151,@No152,@No153,@No154,@No155,@No156,@No157,@No158,@No159,@No160,@No161,@No162,@No163,@No164,@No165,@No166,@No167,@No168,@No169,@No170,@No171,@No172,@No173,@No174,@No175,@No176,@No177,@No178,@No179,@No180,@No181,@No182,@No183,@No184,@No185,@No186,@No187,@No188,@No189,@No190,@No191,@No192,@No193,@No194,@No195,@No196,@No197,@No198,@No199,@No200,@No201,@No202,@No203,@No204,@No205,@No206,@No207,@No208,@No209,@No210,@No211,@No212,@No213,@No214,@No215,@No216,@No217,@No218,@No219,@No220,@No221,@No222,@No223,@No224,@No225,@No226,@No227,@No228,@No229,@No230,@No231,@No232,@No233,@No234,@No235,@No236,@No237,@No238,@No239,@No240,@No241,@No242,@No243,@No244,@No245,@No246,@No247,@No248,@No249,@No250,@No251,@No252,@No253,@No254,@No255,@No256";
            insertCommand.Parameters.Add("@path", SqlDbType.VarChar, 50).Value = curFileName;
            insertCommand.Parameters.Add("@No1", SqlDbType.Int).Value = histogram[0];
            insertCommand.Parameters.Add("@No2", SqlDbType.Int).Value = histogram[1];
            insertCommand.Parameters.Add("@No3", SqlDbType.Int).Value = histogram[2];
            insertCommand.Parameters.Add("@No4", SqlDbType.Int).Value = histogram[3];
            insertCommand.Parameters.Add("@No5", SqlDbType.Int).Value = histogram[4];
            insertCommand.Parameters.Add("@No6", SqlDbType.Int).Value = histogram[5];
            insertCommand.Parameters.Add("@No7", SqlDbType.Int).Value = histogram[6];
            insertCommand.Parameters.Add("@No8", SqlDbType.Int).Value = histogram[7];
            insertCommand.Parameters.Add("@No9", SqlDbType.Int).Value = histogram[8];
            insertCommand.Parameters.Add("@No10", SqlDbType.Int).Value = histogram[9];
            insertCommand.Parameters.Add("@No11", SqlDbType.Int).Value = histogram[10];
            insertCommand.Parameters.Add("@No12", SqlDbType.Int).Value = histogram[11];
            insertCommand.Parameters.Add("@No13", SqlDbType.Int).Value = histogram[12];
            insertCommand.Parameters.Add("@No14", SqlDbType.Int).Value = histogram[13];
            insertCommand.Parameters.Add("@No15", SqlDbType.Int).Value = histogram[14];
            insertCommand.Parameters.Add("@No16", SqlDbType.Int).Value = histogram[15];
            insertCommand.Parameters.Add("@No17", SqlDbType.Int).Value = histogram[16];
            insertCommand.Parameters.Add("@No18", SqlDbType.Int).Value = histogram[17];
            insertCommand.Parameters.Add("@No19", SqlDbType.Int).Value = histogram[18];
            insertCommand.Parameters.Add("@No20", SqlDbType.Int).Value = histogram[19];
            insertCommand.Parameters.Add("@No21", SqlDbType.Int).Value = histogram[20];
            insertCommand.Parameters.Add("@No22", SqlDbType.Int).Value = histogram[21];
            insertCommand.Parameters.Add("@No23", SqlDbType.Int).Value = histogram[22];
            insertCommand.Parameters.Add("@No24", SqlDbType.Int).Value = histogram[23];
            insertCommand.Parameters.Add("@No25", SqlDbType.Int).Value = histogram[24];
            insertCommand.Parameters.Add("@No26", SqlDbType.Int).Value = histogram[25];
            insertCommand.Parameters.Add("@No27", SqlDbType.Int).Value = histogram[26];
            insertCommand.Parameters.Add("@No28", SqlDbType.Int).Value = histogram[27];
            insertCommand.Parameters.Add("@No29", SqlDbType.Int).Value = histogram[28];
            insertCommand.Parameters.Add("@No30", SqlDbType.Int).Value = histogram[29];
            insertCommand.Parameters.Add("@No31", SqlDbType.Int).Value = histogram[30];
            insertCommand.Parameters.Add("@No32", SqlDbType.Int).Value = histogram[31];
            insertCommand.Parameters.Add("@No33", SqlDbType.Int).Value = histogram[32];
            insertCommand.Parameters.Add("@No34", SqlDbType.Int).Value = histogram[33];
            insertCommand.Parameters.Add("@No35", SqlDbType.Int).Value = histogram[34];
            insertCommand.Parameters.Add("@No36", SqlDbType.Int).Value = histogram[35];
            insertCommand.Parameters.Add("@No37", SqlDbType.Int).Value = histogram[36];
            insertCommand.Parameters.Add("@No38", SqlDbType.Int).Value = histogram[37];
            insertCommand.Parameters.Add("@No39", SqlDbType.Int).Value = histogram[38];
            insertCommand.Parameters.Add("@No40", SqlDbType.Int).Value = histogram[39];
            insertCommand.Parameters.Add("@No41", SqlDbType.Int).Value = histogram[40];
            insertCommand.Parameters.Add("@No42", SqlDbType.Int).Value = histogram[41];
            insertCommand.Parameters.Add("@No43", SqlDbType.Int).Value = histogram[42];
            insertCommand.Parameters.Add("@No44", SqlDbType.Int).Value = histogram[43];
            insertCommand.Parameters.Add("@No45", SqlDbType.Int).Value = histogram[44];
            insertCommand.Parameters.Add("@No46", SqlDbType.Int).Value = histogram[45];
            insertCommand.Parameters.Add("@No47", SqlDbType.Int).Value = histogram[46];
            insertCommand.Parameters.Add("@No48", SqlDbType.Int).Value = histogram[47];
            insertCommand.Parameters.Add("@No49", SqlDbType.Int).Value = histogram[48];
            insertCommand.Parameters.Add("@No50", SqlDbType.Int).Value = histogram[49];
            insertCommand.Parameters.Add("@No51", SqlDbType.Int).Value = histogram[50];
            insertCommand.Parameters.Add("@No52", SqlDbType.Int).Value = histogram[51];
            insertCommand.Parameters.Add("@No53", SqlDbType.Int).Value = histogram[52];
            insertCommand.Parameters.Add("@No54", SqlDbType.Int).Value = histogram[53];
            insertCommand.Parameters.Add("@No55", SqlDbType.Int).Value = histogram[54];
            insertCommand.Parameters.Add("@No56", SqlDbType.Int).Value = histogram[55];
            insertCommand.Parameters.Add("@No57", SqlDbType.Int).Value = histogram[56];
            insertCommand.Parameters.Add("@No58", SqlDbType.Int).Value = histogram[57];
            insertCommand.Parameters.Add("@No59", SqlDbType.Int).Value = histogram[58];
            insertCommand.Parameters.Add("@No60", SqlDbType.Int).Value = histogram[59];
            insertCommand.Parameters.Add("@No61", SqlDbType.Int).Value = histogram[60];
            insertCommand.Parameters.Add("@No62", SqlDbType.Int).Value = histogram[61];
            insertCommand.Parameters.Add("@No63", SqlDbType.Int).Value = histogram[62];
            insertCommand.Parameters.Add("@No64", SqlDbType.Int).Value = histogram[63];
            insertCommand.Parameters.Add("@No65", SqlDbType.Int).Value = histogram[64];
            insertCommand.Parameters.Add("@No66", SqlDbType.Int).Value = histogram[65];
            insertCommand.Parameters.Add("@No67", SqlDbType.Int).Value = histogram[66];
            insertCommand.Parameters.Add("@No68", SqlDbType.Int).Value = histogram[67];
            insertCommand.Parameters.Add("@No69", SqlDbType.Int).Value = histogram[68];
            insertCommand.Parameters.Add("@No70", SqlDbType.Int).Value = histogram[69];
            insertCommand.Parameters.Add("@No71", SqlDbType.Int).Value = histogram[70];
            insertCommand.Parameters.Add("@No72", SqlDbType.Int).Value = histogram[71];
            insertCommand.Parameters.Add("@No73", SqlDbType.Int).Value = histogram[72];
            insertCommand.Parameters.Add("@No74", SqlDbType.Int).Value = histogram[73];
            insertCommand.Parameters.Add("@No75", SqlDbType.Int).Value = histogram[74];
            insertCommand.Parameters.Add("@No76", SqlDbType.Int).Value = histogram[75];
            insertCommand.Parameters.Add("@No77", SqlDbType.Int).Value = histogram[76];
            insertCommand.Parameters.Add("@No78", SqlDbType.Int).Value = histogram[77];
            insertCommand.Parameters.Add("@No79", SqlDbType.Int).Value = histogram[78];
            insertCommand.Parameters.Add("@No80", SqlDbType.Int).Value = histogram[79];
            insertCommand.Parameters.Add("@No81", SqlDbType.Int).Value = histogram[80];
            insertCommand.Parameters.Add("@No82", SqlDbType.Int).Value = histogram[81];
            insertCommand.Parameters.Add("@No83", SqlDbType.Int).Value = histogram[82];
            insertCommand.Parameters.Add("@No84", SqlDbType.Int).Value = histogram[83];
            insertCommand.Parameters.Add("@No85", SqlDbType.Int).Value = histogram[84];
            insertCommand.Parameters.Add("@No86", SqlDbType.Int).Value = histogram[85];
            insertCommand.Parameters.Add("@No87", SqlDbType.Int).Value = histogram[86];
            insertCommand.Parameters.Add("@No88", SqlDbType.Int).Value = histogram[87];
            insertCommand.Parameters.Add("@No89", SqlDbType.Int).Value = histogram[88];
            insertCommand.Parameters.Add("@No90", SqlDbType.Int).Value = histogram[89];
            insertCommand.Parameters.Add("@No91", SqlDbType.Int).Value = histogram[90];
            insertCommand.Parameters.Add("@No92", SqlDbType.Int).Value = histogram[91];
            insertCommand.Parameters.Add("@No93", SqlDbType.Int).Value = histogram[92];
            insertCommand.Parameters.Add("@No94", SqlDbType.Int).Value = histogram[93];
            insertCommand.Parameters.Add("@No95", SqlDbType.Int).Value = histogram[94];
            insertCommand.Parameters.Add("@No96", SqlDbType.Int).Value = histogram[95];
            insertCommand.Parameters.Add("@No97", SqlDbType.Int).Value = histogram[96];
            insertCommand.Parameters.Add("@No98", SqlDbType.Int).Value = histogram[97];
            insertCommand.Parameters.Add("@No99", SqlDbType.Int).Value = histogram[98];
            insertCommand.Parameters.Add("@No100", SqlDbType.Int).Value = histogram[99];
            insertCommand.Parameters.Add("@No101", SqlDbType.Int).Value = histogram[100];
            insertCommand.Parameters.Add("@No102", SqlDbType.Int).Value = histogram[101];
            insertCommand.Parameters.Add("@No103", SqlDbType.Int).Value = histogram[102];
            insertCommand.Parameters.Add("@No104", SqlDbType.Int).Value = histogram[103];
            insertCommand.Parameters.Add("@No105", SqlDbType.Int).Value = histogram[104];
            insertCommand.Parameters.Add("@No106", SqlDbType.Int).Value = histogram[105];
            insertCommand.Parameters.Add("@No107", SqlDbType.Int).Value = histogram[106];
            insertCommand.Parameters.Add("@No108", SqlDbType.Int).Value = histogram[107];
            insertCommand.Parameters.Add("@No109", SqlDbType.Int).Value = histogram[108];
            insertCommand.Parameters.Add("@No110", SqlDbType.Int).Value = histogram[109];
            insertCommand.Parameters.Add("@No111", SqlDbType.Int).Value = histogram[110];
            insertCommand.Parameters.Add("@No112", SqlDbType.Int).Value = histogram[111];
            insertCommand.Parameters.Add("@No113", SqlDbType.Int).Value = histogram[112];
            insertCommand.Parameters.Add("@No114", SqlDbType.Int).Value = histogram[113];
            insertCommand.Parameters.Add("@No115", SqlDbType.Int).Value = histogram[114];
            insertCommand.Parameters.Add("@No116", SqlDbType.Int).Value = histogram[115];
            insertCommand.Parameters.Add("@No117", SqlDbType.Int).Value = histogram[116];
            insertCommand.Parameters.Add("@No118", SqlDbType.Int).Value = histogram[117];
            insertCommand.Parameters.Add("@No119", SqlDbType.Int).Value = histogram[118];
            insertCommand.Parameters.Add("@No120", SqlDbType.Int).Value = histogram[119];
            insertCommand.Parameters.Add("@No121", SqlDbType.Int).Value = histogram[120];
            insertCommand.Parameters.Add("@No122", SqlDbType.Int).Value = histogram[121];
            insertCommand.Parameters.Add("@No123", SqlDbType.Int).Value = histogram[122];
            insertCommand.Parameters.Add("@No124", SqlDbType.Int).Value = histogram[123];
            insertCommand.Parameters.Add("@No125", SqlDbType.Int).Value = histogram[124];
            insertCommand.Parameters.Add("@No126", SqlDbType.Int).Value = histogram[125];
            insertCommand.Parameters.Add("@No127", SqlDbType.Int).Value = histogram[126];
            insertCommand.Parameters.Add("@No128", SqlDbType.Int).Value = histogram[127];
            insertCommand.Parameters.Add("@No129", SqlDbType.Int).Value = histogram[128];
            insertCommand.Parameters.Add("@No130", SqlDbType.Int).Value = histogram[129];
            insertCommand.Parameters.Add("@No131", SqlDbType.Int).Value = histogram[130];
            insertCommand.Parameters.Add("@No132", SqlDbType.Int).Value = histogram[131];
            insertCommand.Parameters.Add("@No133", SqlDbType.Int).Value = histogram[132];
            insertCommand.Parameters.Add("@No134", SqlDbType.Int).Value = histogram[133];
            insertCommand.Parameters.Add("@No135", SqlDbType.Int).Value = histogram[134];
            insertCommand.Parameters.Add("@No136", SqlDbType.Int).Value = histogram[135];
            insertCommand.Parameters.Add("@No137", SqlDbType.Int).Value = histogram[136];
            insertCommand.Parameters.Add("@No138", SqlDbType.Int).Value = histogram[137];
            insertCommand.Parameters.Add("@No139", SqlDbType.Int).Value = histogram[138];
            insertCommand.Parameters.Add("@No140", SqlDbType.Int).Value = histogram[139];
            insertCommand.Parameters.Add("@No141", SqlDbType.Int).Value = histogram[140];
            insertCommand.Parameters.Add("@No142", SqlDbType.Int).Value = histogram[141];
            insertCommand.Parameters.Add("@No143", SqlDbType.Int).Value = histogram[142];
            insertCommand.Parameters.Add("@No144", SqlDbType.Int).Value = histogram[143];
            insertCommand.Parameters.Add("@No145", SqlDbType.Int).Value = histogram[144];
            insertCommand.Parameters.Add("@No146", SqlDbType.Int).Value = histogram[145];
            insertCommand.Parameters.Add("@No147", SqlDbType.Int).Value = histogram[146];
            insertCommand.Parameters.Add("@No148", SqlDbType.Int).Value = histogram[147];
            insertCommand.Parameters.Add("@No149", SqlDbType.Int).Value = histogram[148];
            insertCommand.Parameters.Add("@No150", SqlDbType.Int).Value = histogram[149];
            insertCommand.Parameters.Add("@No151", SqlDbType.Int).Value = histogram[150];
            insertCommand.Parameters.Add("@No152", SqlDbType.Int).Value = histogram[151];
            insertCommand.Parameters.Add("@No153", SqlDbType.Int).Value = histogram[152];
            insertCommand.Parameters.Add("@No154", SqlDbType.Int).Value = histogram[153];
            insertCommand.Parameters.Add("@No155", SqlDbType.Int).Value = histogram[154];
            insertCommand.Parameters.Add("@No156", SqlDbType.Int).Value = histogram[155];
            insertCommand.Parameters.Add("@No157", SqlDbType.Int).Value = histogram[156];
            insertCommand.Parameters.Add("@No158", SqlDbType.Int).Value = histogram[157];
            insertCommand.Parameters.Add("@No159", SqlDbType.Int).Value = histogram[158];
            insertCommand.Parameters.Add("@No160", SqlDbType.Int).Value = histogram[159];
            insertCommand.Parameters.Add("@No161", SqlDbType.Int).Value = histogram[160];
            insertCommand.Parameters.Add("@No162", SqlDbType.Int).Value = histogram[161];
            insertCommand.Parameters.Add("@No163", SqlDbType.Int).Value = histogram[162];
            insertCommand.Parameters.Add("@No164", SqlDbType.Int).Value = histogram[163];
            insertCommand.Parameters.Add("@No165", SqlDbType.Int).Value = histogram[164];
            insertCommand.Parameters.Add("@No166", SqlDbType.Int).Value = histogram[165];
            insertCommand.Parameters.Add("@No167", SqlDbType.Int).Value = histogram[166];
            insertCommand.Parameters.Add("@No168", SqlDbType.Int).Value = histogram[167];
            insertCommand.Parameters.Add("@No169", SqlDbType.Int).Value = histogram[168];
            insertCommand.Parameters.Add("@No170", SqlDbType.Int).Value = histogram[169];
            insertCommand.Parameters.Add("@No171", SqlDbType.Int).Value = histogram[170];
            insertCommand.Parameters.Add("@No172", SqlDbType.Int).Value = histogram[171];
            insertCommand.Parameters.Add("@No173", SqlDbType.Int).Value = histogram[172];
            insertCommand.Parameters.Add("@No174", SqlDbType.Int).Value = histogram[173];
            insertCommand.Parameters.Add("@No175", SqlDbType.Int).Value = histogram[174];
            insertCommand.Parameters.Add("@No176", SqlDbType.Int).Value = histogram[175];
            insertCommand.Parameters.Add("@No177", SqlDbType.Int).Value = histogram[176];
            insertCommand.Parameters.Add("@No178", SqlDbType.Int).Value = histogram[177];
            insertCommand.Parameters.Add("@No179", SqlDbType.Int).Value = histogram[178];
            insertCommand.Parameters.Add("@No180", SqlDbType.Int).Value = histogram[179];
            insertCommand.Parameters.Add("@No181", SqlDbType.Int).Value = histogram[180];
            insertCommand.Parameters.Add("@No182", SqlDbType.Int).Value = histogram[181];
            insertCommand.Parameters.Add("@No183", SqlDbType.Int).Value = histogram[182];
            insertCommand.Parameters.Add("@No184", SqlDbType.Int).Value = histogram[183];
            insertCommand.Parameters.Add("@No185", SqlDbType.Int).Value = histogram[184];
            insertCommand.Parameters.Add("@No186", SqlDbType.Int).Value = histogram[185];
            insertCommand.Parameters.Add("@No187", SqlDbType.Int).Value = histogram[186];
            insertCommand.Parameters.Add("@No188", SqlDbType.Int).Value = histogram[187];
            insertCommand.Parameters.Add("@No189", SqlDbType.Int).Value = histogram[188];
            insertCommand.Parameters.Add("@No190", SqlDbType.Int).Value = histogram[189];
            insertCommand.Parameters.Add("@No191", SqlDbType.Int).Value = histogram[190];
            insertCommand.Parameters.Add("@No192", SqlDbType.Int).Value = histogram[191];
            insertCommand.Parameters.Add("@No193", SqlDbType.Int).Value = histogram[192];
            insertCommand.Parameters.Add("@No194", SqlDbType.Int).Value = histogram[193];
            insertCommand.Parameters.Add("@No195", SqlDbType.Int).Value = histogram[194];
            insertCommand.Parameters.Add("@No196", SqlDbType.Int).Value = histogram[195];
            insertCommand.Parameters.Add("@No197", SqlDbType.Int).Value = histogram[196];
            insertCommand.Parameters.Add("@No198", SqlDbType.Int).Value = histogram[197];
            insertCommand.Parameters.Add("@No199", SqlDbType.Int).Value = histogram[198];
            insertCommand.Parameters.Add("@No200", SqlDbType.Int).Value = histogram[199];
            insertCommand.Parameters.Add("@No201", SqlDbType.Int).Value = histogram[200];
            insertCommand.Parameters.Add("@No202", SqlDbType.Int).Value = histogram[201];
            insertCommand.Parameters.Add("@No203", SqlDbType.Int).Value = histogram[202];
            insertCommand.Parameters.Add("@No204", SqlDbType.Int).Value = histogram[203];
            insertCommand.Parameters.Add("@No205", SqlDbType.Int).Value = histogram[204];
            insertCommand.Parameters.Add("@No206", SqlDbType.Int).Value = histogram[205];
            insertCommand.Parameters.Add("@No207", SqlDbType.Int).Value = histogram[206];
            insertCommand.Parameters.Add("@No208", SqlDbType.Int).Value = histogram[207];
            insertCommand.Parameters.Add("@No209", SqlDbType.Int).Value = histogram[208];
            insertCommand.Parameters.Add("@No210", SqlDbType.Int).Value = histogram[209];
            insertCommand.Parameters.Add("@No211", SqlDbType.Int).Value = histogram[210];
            insertCommand.Parameters.Add("@No212", SqlDbType.Int).Value = histogram[211];
            insertCommand.Parameters.Add("@No213", SqlDbType.Int).Value = histogram[212];
            insertCommand.Parameters.Add("@No214", SqlDbType.Int).Value = histogram[213];
            insertCommand.Parameters.Add("@No215", SqlDbType.Int).Value = histogram[214];
            insertCommand.Parameters.Add("@No216", SqlDbType.Int).Value = histogram[215];
            insertCommand.Parameters.Add("@No217", SqlDbType.Int).Value = histogram[216];
            insertCommand.Parameters.Add("@No218", SqlDbType.Int).Value = histogram[217];
            insertCommand.Parameters.Add("@No219", SqlDbType.Int).Value = histogram[218];
            insertCommand.Parameters.Add("@No220", SqlDbType.Int).Value = histogram[219];
            insertCommand.Parameters.Add("@No221", SqlDbType.Int).Value = histogram[220];
            insertCommand.Parameters.Add("@No222", SqlDbType.Int).Value = histogram[221];
            insertCommand.Parameters.Add("@No223", SqlDbType.Int).Value = histogram[222];
            insertCommand.Parameters.Add("@No224", SqlDbType.Int).Value = histogram[223];
            insertCommand.Parameters.Add("@No225", SqlDbType.Int).Value = histogram[224];
            insertCommand.Parameters.Add("@No226", SqlDbType.Int).Value = histogram[225];
            insertCommand.Parameters.Add("@No227", SqlDbType.Int).Value = histogram[226];
            insertCommand.Parameters.Add("@No228", SqlDbType.Int).Value = histogram[227];
            insertCommand.Parameters.Add("@No229", SqlDbType.Int).Value = histogram[228];
            insertCommand.Parameters.Add("@No230", SqlDbType.Int).Value = histogram[229];
            insertCommand.Parameters.Add("@No231", SqlDbType.Int).Value = histogram[230];
            insertCommand.Parameters.Add("@No232", SqlDbType.Int).Value = histogram[231];
            insertCommand.Parameters.Add("@No233", SqlDbType.Int).Value = histogram[232];
            insertCommand.Parameters.Add("@No234", SqlDbType.Int).Value = histogram[233];
            insertCommand.Parameters.Add("@No235", SqlDbType.Int).Value = histogram[234];
            insertCommand.Parameters.Add("@No236", SqlDbType.Int).Value = histogram[235];
            insertCommand.Parameters.Add("@No237", SqlDbType.Int).Value = histogram[236];
            insertCommand.Parameters.Add("@No238", SqlDbType.Int).Value = histogram[237];
            insertCommand.Parameters.Add("@No239", SqlDbType.Int).Value = histogram[238];
            insertCommand.Parameters.Add("@No240", SqlDbType.Int).Value = histogram[239];
            insertCommand.Parameters.Add("@No241", SqlDbType.Int).Value = histogram[240];
            insertCommand.Parameters.Add("@No242", SqlDbType.Int).Value = histogram[241];
            insertCommand.Parameters.Add("@No243", SqlDbType.Int).Value = histogram[242];
            insertCommand.Parameters.Add("@No244", SqlDbType.Int).Value = histogram[243];
            insertCommand.Parameters.Add("@No245", SqlDbType.Int).Value = histogram[244];
            insertCommand.Parameters.Add("@No246", SqlDbType.Int).Value = histogram[245];
            insertCommand.Parameters.Add("@No247", SqlDbType.Int).Value = histogram[246];
            insertCommand.Parameters.Add("@No248", SqlDbType.Int).Value = histogram[247];
            insertCommand.Parameters.Add("@No249", SqlDbType.Int).Value = histogram[248];
            insertCommand.Parameters.Add("@No250", SqlDbType.Int).Value = histogram[249];
            insertCommand.Parameters.Add("@No251", SqlDbType.Int).Value = histogram[250];
            insertCommand.Parameters.Add("@No252", SqlDbType.Int).Value = histogram[251];
            insertCommand.Parameters.Add("@No253", SqlDbType.Int).Value = histogram[252];
            insertCommand.Parameters.Add("@No254", SqlDbType.Int).Value = histogram[253];
            insertCommand.Parameters.Add("@No255", SqlDbType.Int).Value = histogram[254];
            insertCommand.Parameters.Add("@No256", SqlDbType.Int).Value = histogram[255];

            insertCommand.ExecuteNonQuery();
            cn.Close();
        这个长度可以想象到了257列的麻烦之处了吧,我还是用记事本循环编写的。关于这个存储的方法是看了一个课件学的, http://wenku.baidu.com/view/bed698a0f524ccbff12184d0.html。关于数据库那边的存储过程是这样的。感觉这个课件写的很到位,包括了数据库链接、存储、读取、操作很多内容。
@path varchar(50),
@No1 int,
@No2 int,
@No3 int,
@No4 int,
@No5 int,
@No6 int,
@No7 int,
@No8 int,
@No9 int,
@No10 int,
@No11 int,
@No12 int,
@No13 int,
@No14 int,
@No15 int,
@No16 int,
@No17 int,
@No18 int,
@No19 int,
@No20 int,
@No21 int,
@No22 int,
@No23 int,
@No24 int,
@No25 int,
@No26 int,
@No27 int,
@No28 int,
@No29 int,
@No30 int,
@No31 int,
@No32 int,
@No33 int,
@No34 int,
@No35 int,
@No36 int,
@No37 int,
@No38 int,
@No39 int,
@No40 int,
@No41 int,
@No42 int,
@No43 int,
@No44 int,
@No45 int,
@No46 int,
@No47 int,
@No48 int,
@No49 int,
@No50 int,
@No51 int,
@No52 int,
@No53 int,
@No54 int,
@No55 int,
@No56 int,
@No57 int,
@No58 int,
@No59 int,
@No60 int,
@No61 int,
@No62 int,
@No63 int,
@No64 int,
@No65 int,
@No66 int,
@No67 int,
@No68 int,
@No69 int,
@No70 int,
@No71 int,
@No72 int,
@No73 int,
@No74 int,
@No75 int,
@No76 int,
@No77 int,
@No78 int,
@No79 int,
@No80 int,
@No81 int,
@No82 int,
@No83 int,
@No84 int,
@No85 int,
@No86 int,
@No87 int,
@No88 int,
@No89 int,
@No90 int,
@No91 int,
@No92 int,
@No93 int,
@No94 int,
@No95 int,
@No96 int,
@No97 int,
@No98 int,
@No99 int,
@No100 int,
@No101 int,
@No102 int,
@No103 int,
@No104 int,
@No105 int,
@No106 int,
@No107 int,
@No108 int,
@No109 int,
@No110 int,
@No111 int,
@No112 int,
@No113 int,
@No114 int,
@No115 int,
@No116 int,
@No117 int,
@No118 int,
@No119 int,
@No120 int,
@No121 int,
@No122 int,
@No123 int,
@No124 int,
@No125 int,
@No126 int,
@No127 int,
@No128 int,
@No129 int,
@No130 int,
@No131 int,
@No132 int,
@No133 int,
@No134 int,
@No135 int,
@No136 int,
@No137 int,
@No138 int,
@No139 int,
@No140 int,
@No141 int,
@No142 int,
@No143 int,
@No144 int,
@No145 int,
@No146 int,
@No147 int,
@No148 int,
@No149 int,
@No150 int,
@No151 int,
@No152 int,
@No153 int,
@No154 int,
@No155 int,
@No156 int,
@No157 int,
@No158 int,
@No159 int,
@No160 int,
@No161 int,
@No162 int,
@No163 int,
@No164 int,
@No165 int,
@No166 int,
@No167 int,
@No168 int,
@No169 int,
@No170 int,
@No171 int,
@No172 int,
@No173 int,
@No174 int,
@No175 int,
@No176 int,
@No177 int,
@No178 int,
@No179 int,
@No180 int,
@No181 int,
@No182 int,
@No183 int,
@No184 int,
@No185 int,
@No186 int,
@No187 int,
@No188 int,
@No189 int,
@No190 int,
@No191 int,
@No192 int,
@No193 int,
@No194 int,
@No195 int,
@No196 int,
@No197 int,
@No198 int,
@No199 int,
@No200 int,
@No201 int,
@No202 int,
@No203 int,
@No204 int,
@No205 int,
@No206 int,
@No207 int,
@No208 int,
@No209 int,
@No210 int,
@No211 int,
@No212 int,
@No213 int,
@No214 int,
@No215 int,
@No216 int,
@No217 int,
@No218 int,
@No219 int,
@No220 int,
@No221 int,
@No222 int,
@No223 int,
@No224 int,
@No225 int,
@No226 int,
@No227 int,
@No228 int,
@No229 int,
@No230 int,
@No231 int,
@No232 int,
@No233 int,
@No234 int,
@No235 int,
@No236 int,
@No237 int,
@No238 int,
@No239 int,
@No240 int,
@No241 int,
@No242 int,
@No243 int,
@No244 int,
@No245 int,
@No246 int,
@No247 int,
@No248 int,
@No249 int,
@No250 int,
@No251 int,
@No252 int,
@No253 int,
@No254 int,
@No255 int,
@No256 int
as insert into information values(@path,@No1,@No2,@No3,@No4,@No5,@No6,@No7,@No8,@No9,@No10,@No11,@No12,@No13,@No14,@No15,@No16,@No17,@No18,@No19,@No20,@No21,@No22,@No23,@No24,@No25,@No26,@No27,@No28,@No29,@No30,@No31,@No32,@No33,@No34,@No35,@No36,@No37,@No38,@No39,@No40,@No41,@No42,@No43,@No44,@No45,@No46,@No47,@No48,@No49,@No50,@No51,@No52,@No53,@No54,@No55,@No56,@No57,@No58,@No59,@No60,@No61,@No62,@No63,@No64,@No65,@No66,@No67,@No68,@No69,@No70,@No71,@No72,@No73,@No74,@No75,@No76,@No77,@No78,@No79,@No80,@No81,@No82,@No83,@No84,@No85,@No86,@No87,@No88,@No89,@No90,@No91,@No92,@No93,@No94,@No95,@No96,@No97,@No98,@No99,@No100,@No101,@No102,@No103,@No104,@No105,@No106,@No107,@No108,@No109,@No110,@No111,@No112,@No113,@No114,@No115,@No116,@No117,@No118,@No119,@No120,@No121,@No122,@No123,@No124,@No125,@No126,@No127,@No128,@No129,@No130,@No131,@No132,@No133,@No134,@No135,@No136,@No137,@No138,@No139,@No140,@No141,@No142,@No143,@No144,@No145,@No146,@No147,@No148,@No149,@No150,@No151,@No152,@No153,@No154,@No155,@No156,@No157,@No158,@No159,@No160,@No161,@No162,@No163,@No164,@No165,@No166,@No167,@No168,@No169,@No170,@No171,@No172,@No173,@No174,@No175,@No176,@No177,@No178,@No179,@No180,@No181,@No182,@No183,@No184,@No185,@No186,@No187,@No188,@No189,@No190,@No191,@No192,@No193,@No194,@No195,@No196,@No197,@No198,@No199,@No200,@No201,@No202,@No203,@No204,@No205,@No206,@No207,@No208,@No209,@No210,@No211,@No212,@No213,@No214,@No215,@No216,@No217,@No218,@No219,@No220,@No221,@No222,@No223,@No224,@No225,@No226,@No227,@No228,@No229,@No230,@No231,@No232,@No233,@No234,@No235,@No236,@No237,@No238,@No239,@No240,@No241,@No242,@No243,@No244,@No245,@No246,@No247,@No248,@No249,@No250,@No251,@No252,@No253,@No254,@No255,@No256)

       接下来我要实现的是,把一个文件夹下的所有图片都像那样存进去,http://wenku.baidu.com/link?url=nsWYEFgCbqXc49iid4TGlXDbgQ3jp4QZPBGwbBtSGqIeQ55qXEm4ENH_2BiHrMevsE59l7kydaTq0hE741MXV9uf1xys3rjBFsfcivT4eai,我是参考的这篇文章编写的。

DirectoryInfo dicInfo = new DirectoryInfo(@"D:\照片\手机照片2");
            FileInfo[] textFiles = dicInfo.GetFiles("*.*", SearchOption.AllDirectories);


            foreach (FileInfo fileInfo in textFiles) 
         接下来还有一个获取完整路径的问题,在网上查了一下,使用的这个函数,fileInfo.FullName,这样存的时候还遇到了一点问题,就是不知道为什么提示主键重复,照理说主键是路径不可能重复,我就关联了一下,把前两列共同设为主键。

      嗯,数据都存好了,下面就是排序和输出的问题了,我的思路是在数据库中选出相似度大于0.75的最相近的十个(这里可能不足十个),然后用十个picturebox显示它们,这里是排序的算法。

 if (r > 0.75&&num<10)
                    {
                        queue[num].result = r;
                        queue[num].path = strFileName;
                        num++;
                    }
                    else if (r > 0.75 && num >= 9)
                    {
                        sort();
                        for (int i = 0; i < 10; i++)
                        {
                            if (r > queue[i].result&&i!=9)
                            {
                                
                                for ( int x = 8; x >=i; x--)
                                {
                                    queue[x+1].path = queue[x].path;
                                    queue[x +1].result = queue[x].result;

                                }
                          
                                queue[i].path = strFileName;
                                queue[i].result = r;
                                break;
                            }
                            else if (r > queue[i].result && i == 9)
                            {
                                queue[9].path = strFileName;
                                    queue[9].result = r;
                            }
                        }
                    }
       里面用到了结构体,感觉很像操作系统课的作业——处理机算法的调用,哈哈,我就是模仿那个写的。不过c#的机构提数组声明是好像有点不同。
 void sort()
        {
            max temp;
            int i, j;
            for (i = 0; i < 9; i++)
            {
                for (j = i; j < 9; j++)
                {
                    if (queue[i].result < queue[j].result)
                    {
                        temp.path = queue[i].path;
                        queue[i].path = queue[j].path;
                        queue[j].path = temp.path;
                        temp.result = queue[i].result;
                        queue[i].result = queue[j].result;
                        queue[j].result = temp.result;

                    }
                }
            }
        }
        int num = 0;
        public struct max
        {
            public double result;
            public string path;
        };
        public max[] queue = new max[10];
      这个是picturebox显示的那段代码。而且要把属性SizeMode设为StretchIngmage,以显示整张图片。这点是在这里学到的。 http://blog.csdn.net/friendan/article/details/7463905
if(queue[0].path!=null)
                        pictureBox1.Image = Image.FromFile(queue[0].path);
                    if (queue[1].path != null)
                        pictureBox2.Image = Image.FromFile(queue[1].path);
                    if (queue[2].path != null)
                        pictureBox3.Image = Image.FromFile(queue[2].path);
                    if (queue[3].path != null)
                        pictureBox4.Image = Image.FromFile(queue[3].path);
                    if (queue[4].path != null)
                        pictureBox5.Image = Image.FromFile(queue[4].path);
                    if (queue[5].path!= null)
                        pictureBox6.Image = Image.FromFile(queue[5].path);
                    if (queue[6].path != null)
                        pictureBox7.Image = Image.FromFile(queue[6].path);
                    if (queue[7].path != null)
                        pictureBox8.Image = Image.FromFile(queue[7].path);
                    if (queue[8].path != null)
                        pictureBox9.Image = Image.FromFile(queue[8].path);
                    if (queue[9].path != null)
                        pictureBox10.Image = Image.FromFile(queue[9].path);
       

       就是这样了,非常感谢帮助过我的人,虽然是个很简单的程序,但是第一次做还是很有成就感的,也希望更多人能在我的这篇文章里学到些知识。

       代码下载地址:http://download.csdn.net/detail/liangshanxiaohan/7212905

           



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值