private void PictureBox_MouseClick(object sender, MouseEventArgs e) { PictureBox pb = ((PictureBox)sender); string[] pos = pb.Name.Split(':'); Int64 x = Convert.ToInt64(pos[0]); Int64 y = Convert.ToInt64(pos[1]); if (_ImageList != null && _ImageIndex < _ImageList.Images.Count) { if (_ImageIndex >= 0) { pb.Image = _ImageList.Images[_ImageIndex]; stImage curImage = _GetImageByID(Convert.ToInt32(_CurImage.Image_ID), Convert.ToInt32(_CurImage.Image_State)); ImageProperty imgPro = new ImageProperty(); imgPro.Image_ID = _CurImage.Image_ID; imgPro.Image_State = curImage.Image_State; imgPro.Image_Index = _ImageIndex; int img_level = Convert.ToInt32(curImage.Image_Level); if (!_ImageArray[x, y].ContainsKey(img_level)) { _ImageArray[x, y].Add(img_level, imgPro); } else { _ImageArray[x, y][img_level] = imgPro; } UpdateJsonStr(); } _OnClickPB((int)x, (int)y, _ImageArray[(int)x, (int)y]); } }
public string ImageArray2Json(int x_size = 9, int y_size = 9) { string text = "\"data\":\r\n[\r\n"; for (int x = 0; x < x_size; x++) { text += "["; for (int y = 0; y < y_size; y++) { if (_ImageArray[x, y].Keys.Count == 0) { text += string.Format("[\"{0}\"]", -1); } else { text += "["; int count = 1; //当前默认转换第一个key到json List <int> sortkeys = new List <int>(); foreach (var k in _ImageArray[x, y].Keys) { sortkeys.Add(k); } sortkeys.Sort(); foreach (var k in sortkeys) { stImage img = _GetImageByID((int)_ImageArray[x, y][k].Image_ID, (int)_ImageArray[x, y][k].Image_State); if (img.Image_State == Convert.ToInt64(Const.NoState)) { text += string.Format("\"{0}\"", img.Image_ID); } else { text += string.Format("\"{0}:{1}\"", img.Image_ID, img.Image_State); } if (count < _ImageArray[x, y].Count) { text += ","; } count++; } text += "]"; } if (y != y_size - 1) { text += ","; } } text += "]"; if (x != x_size - 1) { text += ",\r\n"; } } text += "\r\n]"; return(text); }
private stImage DB_GetImageByImageID(int image_id, Int64 image_state) { stImage img = new stImage(); var k = new ImageKeys(image_id, image_state); if (_imgMap.ContainsKey(k)) { img = _imgMap[k]; } else { using (SQLiteConnection conn = new SQLiteConnection(_sqlCon)) { conn.Open(); string sql = string.Format("select * from tb_image where image_id={0} and image_state={1}", image_id, image_state); SQLiteCommand command = new SQLiteCommand(sql, conn); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { img.ID = (Int64)reader["id"]; img.Img = BytesToImage((byte[])reader["image"]); img.Image_ID = (Int64)reader["image_id"]; img.Image_State = (Int64)reader["image_state"]; img.Image_Level = (Int64)reader["image_level"]; img.Image_Name = reader["image_name"] == null ? "" : reader["image_name"].ToString(); var new_key = new ImageKeys(img.Image_ID, img.Image_State); _imgMap[new_key] = img; } } } return(img); }
private void lv_Click(object sender, EventArgs e) { ListView lv = ((ListView)sender); if (lv.SelectedIndices != null && lv.SelectedIndices.Count > 0) { ListView.SelectedIndexCollection c = lv.SelectedIndices; string[] key = lv.Items[c[0]].Name.Split('%'); stImage selectImg = DB_GetImageByImageID(Convert.ToInt32(key[0]), Convert.ToInt32(key[1])); _cur_ID = selectImg.ID; _cur_Image_ID = (int)selectImg.Image_ID; _cur_Image_Index = lv.Items[c[0]].ImageIndex; _cur_Image_Name = selectImg.Image_Name; _cur_Image_level = selectImg.Image_Level; _cur_Image_State = selectImg.Image_State; mapControl1.SetImageIndex((int)_cur_Image_Index); mapControl1.SetImage(selectImg); textBox_ImageID.Text = Convert.ToString(selectImg.Image_ID); textBox_State.Text = selectImg.Image_State.ToString(); textBox_Level.Text = selectImg.Image_Level.ToString(); textBox_Name.Text = selectImg.Image_Name; pictureBox1.Image = imageList1.Images[lv.Items[c[0]].ImageIndex]; } }
private void RefreshListView() { panel1.Controls.Clear(); //创建TabControl TabControl tbc = new TabControl(); tbc.Size = new Size(356, 264); panel1.Controls.Add(tbc); for (int i = 0; i < imageList1.Images.Count; i++) { if (_imgIndexImgIDMap.ContainsKey(i)) { var image_key = _imgIndexImgIDMap[i]; var image_id = (int)image_key.Image_ID; var image_state = image_key.Image_State; stImage imgInfo = DB_GetImageByImageID(image_id, image_key.Image_State); string level = imgInfo.Image_Level.ToString(); //添加 TabPage TabPage tbp; var levelPages = tbc.Controls.Find(level, false); if (levelPages.Length > 0) { tbp = levelPages[0] as TabPage; } else { tbp = new TabPage(); tbp.Name = imgInfo.Image_Level.ToString(); tbp.Text = string.Format("第{0}层", imgInfo.Image_Level); //tbp.UseVisualStyleBackColor = true; tbc.Controls.Add(tbp); //添加 ListView ListView lv = new ListView(); lv.Dock = DockStyle.Fill; lv.Name = "lv"; lv.LargeImageList = imageList1; lv.Click += new System.EventHandler(this.lv_Click); lv.SelectedIndexChanged += new EventHandler(lv_SelectItemChange); tbp.Controls.Add(lv); } //绑定图片到 ListView ListView curLv = tbp.Controls.Find("lv", false)[0] as ListView; int count = curLv.Items.Count; curLv.Items.Add(System.IO.Path.GetFileName(imageList1.Images[i].ToString()), i); curLv.Items[count].ImageIndex = i; curLv.Items[count].Text = imgInfo.Image_Name == "" || imgInfo.Image_Name == null?image_id.ToString() : imgInfo.Image_Name; //元素name对应数据库中的ID curLv.Items[count].Name = string.Format("{0}%{1}", image_id, image_state); } } }
private void button_Add_Click(object sender, EventArgs e) { //设置打开文件控件 OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF);BMP文件(*.BMP);PNG文件(*.PNG)|*.jpg;*.jpeg;*.gif;*.bmp;*.png"; openfile.FilterIndex = 1; //当前选定索引 openfile.RestoreDirectory = true; openfile.FileName = ""; //对话框选择确定按钮 if (openfile.ShowDialog() == DialogResult.OK) { try { //FromFile从指定的文件创建Image Image img = Image.FromFile(openfile.FileName); //图片加载到ImageList控件和imageList图片列表 Int64 img_id = DB_GetMaxImgID() + 1; DB_SaveImage(img_id, img); var new_key = new ImageKeys(img_id, 1); _imgIndexImgIDMap.Add(imageList1.Images.Count, new_key); imageList1.Images.Add(img); //缓存图片信息 stImage newImg = new stImage(); newImg.ID = img_id; newImg.Image_ID = img_id; newImg.Image_State = 1; _imgMap.Add(new_key, newImg); RefreshListView(); } catch (Exception error) { MessageBox.Show(error.Message); } } }
private List <stImage> DB_LoadImage() { List <stImage> imgList = new List <stImage>(); using (SQLiteConnection conn = new SQLiteConnection(_sqlCon)) { conn.Open(); string sql = "select * from tb_image"; SQLiteCommand command = new SQLiteCommand(sql, conn); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { stImage image = new stImage(); image.ID = (Int64)reader["id"]; image.Img = BytesToImage((byte[])reader["image"]);; image.Image_ID = (Int64)reader["image_id"]; image.Image_State = (Int64)reader["image_state"]; image.Image_Level = (Int64)reader["image_level"]; image.Image_Name = reader["image_name"] == null ? "" : reader["image_name"].ToString(); imgList.Add(image); } } return(imgList); }
public void SetImage(stImage img) { _CurImage = img; }
void ResetByConfig(string config) { try { //解析每个元素 SortedDictionary <int, ImageProperty>[,] imageArray = new SortedDictionary <int, ImageProperty> [9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { imageArray[i, j] = new SortedDictionary <int, ImageProperty>(); } } string[] xCfgArray = config.Substring(7).Replace("]]", "@").Split('@'); int x = 0; for (int n = 0; n < xCfgArray.Length; n++) { if (n == 9) { break; } int y = 0; string[] subXcfgArray = xCfgArray[n].Replace(",[", "@").Split('@'); for (int k = 0; k < subXcfgArray.Length; k++) { if (subXcfgArray[k] == "") { continue; } string content = ""; int offset = 0; if (k == 0 && n == 0) { offset = 7; } if (k == 0 && n != 0) { offset = 5; } string tempXCfg = subXcfgArray[k].Substring(offset); content = tempXCfg.IndexOf(']') >= 0 ? tempXCfg.Remove(tempXCfg.IndexOf(']')) : tempXCfg; string[] levelList = content.Split(','); for (int m = 0; m < levelList.Length; m++) { string[] info = levelList[m].Split(':'); if (info.Length == 1 && (info[0] == "-1\"" || info[0] == "\"-1\"")) { break; } int offset2 = 0; if (info.Length >= 2) { offset2 = 1; } ImageProperty ip = new ImageProperty(); ip.Image_ID = info.Length == 2 ? Convert.ToInt64(info[0].Substring(offset2)) : Convert.ToInt64(info[0].Substring(1).Remove(info[0].Substring(1).IndexOf('"'))); ip.Image_State = info.Length == 2 ? Convert.ToInt64(info[1].Remove(info[1].IndexOf('"'))) : Convert.ToInt64(Const.NoState); ip.Image_Index = GetImageIndexByImgId(ip.Image_ID, ip.Image_State); stImage imgInfo = DB_GetImageByImageID((int)ip.Image_ID, ip.Image_State); imageArray[x, y].Add((int)imgInfo.Image_Level, ip); } y++; } x++; } mapControl1.LoadConfig(imageArray); } catch (Exception e) { MessageBox.Show(e.Message); } }
private void OnClickPb(int x, int y, SortedDictionary <int, ImageProperty> imgmap) { //画格子信息 Label posLbl; var posLblList = panel5.Controls.Find("posLabel", false); if (posLblList.Length == 0) { posLbl = new Label(); posLbl.Name = "posLabel"; posLbl.Text = string.Format("格子[{0}:{1}]", x, y); posLbl.Location = new Point(80, 5); posLbl.ForeColor = Color.Green; posLbl.Font = new Font("SimSun", 12); panel5.Controls.Add(posLbl); } else { posLbl = posLblList[0] as Label; } posLbl.Text = string.Format("格子[{0}:{1}]", x, y); //画层级信息 var lblNoList = panel5.Controls.Find("lblNo", false); if (imgmap.Count == 0) { Label lblNo; if (lblNoList.Length == 0) { lblNo = new Label(); lblNo.Name = "lblNo"; lblNo.AutoSize = true; lblNo.Text = "没有配置元素"; posLbl.Font = new Font("SimSun", 12); lblNo.ForeColor = Color.Red; lblNo.Location = new Point(0, 30); panel5.Controls.Add(lblNo); } List <InfoControl> icList = new List <InfoControl>(); foreach (var c in panel5.Controls) { InfoControl ic = c as InfoControl; if (ic != null) { icList.Add(ic); } } foreach (var ic in icList) { panel5.Controls.Remove(ic); } } else { if (lblNoList.Length != 0) { panel5.Controls.Remove(lblNoList[0]); } List <int> sortkeys = new List <int>(); foreach (var v in imgmap) { sortkeys.Add(v.Key); } sortkeys.Sort(); int n = 0; foreach (var key in sortkeys) { var img = imgmap[key]; InfoControl ic; stImage imgInfo = DB_GetImageByImageID((int)img.Image_ID, img.Image_State); var icList = panel5.Controls.Find(n.ToString(), false); if (icList.Length == 0) { ic = new InfoControl(); ic.Name = n.ToString(); ic.Location = new Point(10, n * 90 + (n + 1) * 30); ic.label1.Text = string.Format("第{0}层:", key); ic.textBox1.Text = imgInfo.Image_ID.ToString(); ic.textBox2.Text = img.Image_State.ToString(); ic.pictureBox1.Image = imageList1.Images[img.Image_Index]; ic._ImageInfo = string.Format("{0}:{1}:{2}:{3}", x, y, imgInfo.Image_ID, imgInfo.Image_Level); ic.DeleteImg = mapControl1.DeleteImg; panel5.Controls.Add(ic); } else { ic = icList[0] as InfoControl; ic.label1.Text = string.Format("第{0}层:", key); ic.textBox1.Text = imgInfo.Image_ID.ToString(); ic.textBox2.Text = img.Image_State.ToString(); ic.pictureBox1.Image = imageList1.Images[img.Image_Index]; ic._ImageInfo = string.Format("{0}:{1}:{2}:{3}", x, y, imgInfo.Image_ID, imgInfo.Image_Level); } n++; } List <InfoControl> ifocList = new List <InfoControl>(); foreach (var c in panel5.Controls) { InfoControl ic = c as InfoControl; if (ic != null) { ifocList.Add(ic); } } foreach (var ic in ifocList) { if (Convert.ToInt32(ic.Name) >= imgmap.Count) { panel5.Controls.Remove(ic); } } } }