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 MainForm_Load(object sender, EventArgs e) { imageList1.Images.Clear(); List <stImage> imgList = DB_LoadImage(); for (int n = 0; n < imgList.Count; n++) { imageList1.Images.Add(imgList[n].Img); var new_key = new ImageKeys(imgList[n].Image_ID, imgList[n].Image_State); //ImageList控件上的图片对应DB的ID _imgIndexImgIDMap.Add(n, new_key); //缓存图片信息 _imgMap.Add(new_key, imgList[n]); } RefreshListView(); InitConfigItem(); UpdateJsonStr = mapControl1.UpdateJsonStr; ResetMap = mapControl1.ResetMap; GetJson = mapControl1.ImageArray2Json; mapControl1.SetImageList(imageList1); mapControl1.SetImageIndex(-1); mapControl1._UpdateJson = UpdateJsonText; mapControl1._GetImageByID = DB_GetImageByImageID; mapControl1._OnClickPB = OnClickPb; InitJsonText(); textBox4.SelectionStart = 0; textBox4.SelectionLength = 0; }
private void DB_DeleteImage(ImageKeys k) { using (SQLiteConnection conn = new SQLiteConnection(_sqlCon)) { conn.Open(); string sql = string.Format("delete from tb_image where image_id ={0} and image_state={1}", k.Image_ID, k.Image_State); SQLiteCommand command = new SQLiteCommand(sql, conn); command.ExecuteNonQuery(); } }
private void button_Delete_Click(object sender, EventArgs e) { if (textBox_ImageID.Text == "" || _cur_Image_Index == -1) { return; } if (MessageBox.Show(string.Format("确定要删除元素{0}-{1}?", textBox_ImageID.Text, textBox_State.Text), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { var cur_key = new ImageKeys(Convert.ToInt64(textBox_ImageID.Text), Convert.ToInt64(textBox_State.Text)); //数据库中删除图片 DB_DeleteImage(_imgIndexImgIDMap[_cur_Image_Index]); _imgIndexImgIDMap.Remove(_cur_Image_Index); RefreshListView(); ClearSelectedInfo(); } }
public void UpdateImageInfo(ImageKeys oldIK, ImageKeys newIK) { for (int x = 0; x < 9; x++) { for (int y = 0; y < 9; y++) { foreach (KeyValuePair <int, ImageProperty> kv in _ImageArray[x, y]) { if (kv.Value.Image_ID == oldIK.Image_ID && kv.Value.Image_State == oldIK.Image_State) { _ImageArray[x, y][kv.Key].Image_ID = newIK.Image_ID; _ImageArray[x, y][kv.Key].Image_State = newIK.Image_State; } } } } }
void UpdateImageInfo(ImageKeys oldkey, ImageKeys newkey, string newtext) { Control[] lvs = this.Controls.Find("lv", true); foreach (var lv in lvs) { foreach (ListViewItem item in ((ListView)lv).Items) { var oldname = string.Format("{0}%{1}", oldkey.Image_ID, oldkey.Image_State); if (item.Name == oldname) { var newname = string.Format("{0}%{1}", newkey.Image_ID, newkey.Image_State); item.Name = newname; item.Text = newtext; return; } } } }
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 void button1_Click(object sender, EventArgs e) { try { if (!IsNumberic(textBox_ImageID.Text)) { MessageBox.Show("元素ID不能包含字符串!"); return; } var image_id = Convert.ToInt64(textBox_ImageID.Text); if (!IsNumberic(textBox_State.Text)) { MessageBox.Show("元素状态不能包含字符串!"); return; } var image_state = Convert.ToInt64(textBox_State.Text); var new_key = new ImageKeys(image_id, image_state); var old_key = new ImageKeys(_cur_Image_ID, _cur_Image_State); if (!IsNumberic(textBox_Level.Text)) { MessageBox.Show("元素层级不能包含字符串!"); return; } if (!_imgMap.ContainsKey(old_key)) { MessageBox.Show("当前选中元素不存在!"); return; } string sql = ""; if (_cur_Image_ID != Convert.ToInt64(textBox_ImageID.Text) || _cur_Image_State != Convert.ToInt64(textBox_State.Text)) { if (DB_IsImgIDExist(image_id, image_state)) { MessageBox.Show(string.Format("该键值({0}-{1})已经存在!", image_id, image_state)); return; } var temp_img_info = _imgMap[old_key]; temp_img_info.Image_ID = image_id; temp_img_info.Image_State = image_state; _imgMap.Add(new_key, temp_img_info); _imgMap.Remove(old_key); _imgIndexImgIDMap[(int)_cur_Image_Index] = new_key; sql += string.Format("update tb_image set image_id={0},image_state={1} where id={2};", image_id, image_state, _cur_ID); } if (_cur_Image_Name != textBox_Name.Text) { _imgMap[new_key].Image_Name = textBox_Name.Text; sql += string.Format("update tb_image set image_name='{0}' where id={1};", textBox_Name.Text, _cur_ID); } bool change_level = false; if (_cur_Image_level != Convert.ToInt64(textBox_Level.Text)) { _imgMap[new_key].Image_Level = Convert.ToInt64(textBox_Level.Text); sql += string.Format("update tb_image set image_level='{0}' where id={1};", Convert.ToInt64(textBox_Level.Text), _cur_ID); change_level = true; } if (sql != "") { ExecuteSql(sql); if (change_level) { RefreshListView(); } else { UpdateImageInfo(old_key, new_key, textBox_Name.Text); } mapControl1.UpdateImageInfo(old_key, new_key); UpdateJsonStr(); label4.Text = "tips:更新成功"; } //重置当前选中图片信息 _cur_Image_ID = (int)image_id; _cur_Image_State = image_state; } catch (Exception err) { MessageBox.Show(err.Message); } }