/// <summary> /// 用于N*M切割图片,以生成特征向量 /// </summary> /// <param name="NMbmp"></param> /// <param name="N"></param> /// <param name="M"></param> /// <returns>返回生成向量的字符串表示</returns> public string CutNM(Bitmap sourceImage, int xNum, int yNum)//传进来一幅图片,切割、计算每份子图的像素比例,最后输出字符串形式的特征向量 { float xWidth = sourceImage.Width / xNum; float yWidth = sourceImage.Height / yNum; StringBuilder sbresult = new StringBuilder(); for (int countY = 0; countY < yNum; countY++) { for (int countX = 0; countX < xNum; countX++) { RectangleF cloneRect = new RectangleF(countX * xWidth, countY * yWidth, xWidth, yWidth); Bitmap kidbmp = sourceImage.Clone(cloneRect, PixelFormat.Format24bppRgb); double kidpercent = PicUtils.PixlPercent(kidbmp); //判断是为了以后用split切割时不会出现最后一个数组元素为空的情况 if (countY == yNum - 1 && countX == xNum - 1) { sbresult.Append(kidpercent.ToString()); } else { sbresult.Append(kidpercent.ToString() + ","); } } } return(sbresult.ToString()); }
/// <summary> /// 将一幅图片以数组的形式存入数据库 /// </summary> /// <param name="cutbmp"></param> public void SaveCutBmpToAccess(Bitmap cutbmp) //mark是图片的标记 { try { double pp = PicUtils.PixlPercent(cutbmp); MemoryStream ms = new MemoryStream(); cutbmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Flush(); //将二进制数据存到byte数字中 byte[] bmpBytes = ms.ToArray(); pictureBox1.Image = Image.FromStream(new MemoryStream(bmpBytes)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bmpBytes.Length; i++) { if (i != bmpBytes.Length - 1) { sb.Append(bmpBytes[i] + ","); } else { sb.Append(bmpBytes[i]); } } if (sb.Length > 65536) { sb.Clear(); sb.Append("该图片太大了,字节数超过65536"); } mycnn.ConnectionString = Myconnstr; cmd.Connection = mycnn;//出错,因为cmd为空 mycnn.Open(); cmd.CommandText = "delete from AfterCut"; cmd.ExecuteNonQuery(); cmd.CommandText = "insert into AfterCut(content_,pixel_percent) values('" + sb.ToString() + "','" + pp.ToString() + "')";//access数据库中的备注类型最大长度为65536,所以图片不能太大 cmd.ExecuteNonQuery(); } catch (Exception w) { MessageBox.Show(w.ToString()); } finally { mycnn.Close(); } }