基本代码逻辑OK
时间:2010-12-02 来源:jinhaoxia
打开图片,并且能添加和保存批注
目前不具有实用性。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Text; using System.Windows.Forms; namespace PICTUREVIEW { public partial class Form1 : Form { GalleryDataSet gallery; GalleryDataSetTableAdapters.picturesTableAdapter gallery_pictures_adpater; GalleryDataSetTableAdapters.commentsTableAdapter gallery_comments_adpater; Point panel1_location; BackgroundWorker doser; public Form1() { InitializeComponent(); gallery = new GalleryDataSet(); gallery_pictures_adpater = new GalleryDataSetTableAdapters.picturesTableAdapter(); gallery_comments_adpater = new GalleryDataSetTableAdapters.commentsTableAdapter(); panel1_location = new Point(0, 0); //flush_gallery_view(); doser = new BackgroundWorker(); doser.RunWorkerCompleted += new RunWorkerCompletedEventHandler(doser_RunWorkerCompleted); doser.RunWorkerAsync(); picture_view.DoubleClick += new EventHandler(picture_view_DoubleClick); panel1.ControlAdded += new ControlEventHandler(panel1_ControlAdded); tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged); panel1.MouseDoubleClick+=new MouseEventHandler(menu_item_file_open_Click); } void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { menu_tools_item_next.Enabled = menu_tools_item_pre.Enabled = (sender as TabControl).SelectedTab == tab_picture_view; } void doser_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { flush_gallery_view(); } void panel1_ControlAdded(object sender, ControlEventArgs e) { e.Control.Location = panel1_location; panel1_location.X += 120; if (panel1_location.X > panel1.Width - 120) { panel1_location.X = 0; panel1_location.Y += 80; } } void picture_view_DoubleClick(object sender, EventArgs e) { string _comment; if ((_comment = Microsoft.VisualBasic.Interaction.InputBox("输入要标注在此的批注", "输入批注")) != String.Empty) { gallery_comments_adpater.Insert(((sender as PictureBox).Tag as GalleryDataSet.picturesRow).pid, _comment, (e as MouseEventArgs).X, (e as MouseEventArgs).Y); } set_current_picture((sender as PictureBox).Tag as GalleryDataSet.picturesRow); } LinkedList<GalleryDataSet.picturesRow> gallery_for_find; LinkedListNode<GalleryDataSet.picturesRow> picture_view_current_node; void flush_gallery_view() { gallery_for_find = new LinkedList<GalleryDataSet.picturesRow>(); gallery_pictures_adpater.Fill(gallery.pictures); panel1.Controls.Clear(); panel1_location.X = 0; panel1_location.Y = 0; foreach (GalleryDataSet.picturesRow x in gallery.pictures.Rows) { try { PictureBox _pictures_box = new PictureBox(); _pictures_box.Size = new Size(120, 80); _pictures_box.SizeMode = PictureBoxSizeMode.StretchImage; _pictures_box.Cursor = Cursors.Hand; _pictures_box.Click += new EventHandler(_pictures_box_Click); _pictures_box.Image = new Bitmap(x.path); _pictures_box.Tag = x; panel1.Controls.Add(_pictures_box); gallery_for_find.AddLast(x); } catch { //什么也不干 } } } void set_current_picture(GalleryDataSet.picturesRow p) { tabControl1.SelectedTab = tab_picture_view; try { picture_view.Image = new Bitmap(p.path); } catch { } menu_tools_item_pre.Enabled = p != gallery_for_find.First.Value; menu_tools_item_next.Enabled = p != gallery_for_find.Last.Value; picture_view.Tag = p; gallery_comments_adpater.Fill(gallery.comments); picture_view.Controls.Clear(); foreach (GalleryDataSet.commentsRow x in gallery.comments.Select("pid=" + p.pid.ToString())) { Label _label = new Label(); _label.Text = x.text; _label.Location = new Point(x.x, x.y); _label.AutoSize = true; _label.Padding = new Padding(6); _label.BackColor = Color.Orange; picture_view.Controls.Add(_label); } } void enroll_picture(string p) { if (gallery.pictures.Select("path='" + p + "'").Length==0) { gallery_pictures_adpater.Insert(p, 0); } } void _pictures_box_Click(object sender, EventArgs e) { set_current_picture((sender as PictureBox).Tag as GalleryDataSet.picturesRow); } private void menu_item_file_open_Click(object sender, EventArgs e) { OpenFileDialog _selector = new OpenFileDialog(); _selector.Title = "选择要打开的图片"; _selector.Filter = "图像文件|*.jpg"; if (_selector.ShowDialog()==DialogResult.OK) { enroll_picture(_selector.FileName); } flush_gallery_view(); } private void menu_item_file_enroll_Click(object sender, EventArgs e) { OpenFileDialog _selector = new OpenFileDialog(); _selector.Title = "选择要打开的图片(可以多选)"; _selector.Filter = "图像文件|*.jpg"; _selector.Multiselect = true; if (_selector.ShowDialog() == DialogResult.OK) { foreach (string x in _selector.FileNames) { enroll_picture(x); } } flush_gallery_view(); } private void menu_file_item_exit_Click(object sender, EventArgs e) { Application.Exit(); } private void menu_file_clear_Click(object sender, EventArgs e) { int i = 0, c = 0; foreach (GalleryDataSet.picturesRow xi in gallery.pictures) { if (!System.IO.File.Exists(xi.path)) { i++; foreach (GalleryDataSet.commentsRow xj in gallery.comments.Select("pid="+xi.pid)) { c++; gallery_comments_adpater.Delete(xj.cid, xj.pid, xj.text, xj.x, xj.y); } gallery_pictures_adpater.Delete(xi.pid, xi.path, xi.hash); } } MessageBox.Show("共清除" + i.ToString() + "条不存在的记录,以及他们的批注" + c.ToString() + "条。"); } private void menu_tools_item_next_Click(object sender, EventArgs e) { set_current_picture(gallery_for_find.Find(picture_view.Tag as GalleryDataSet.picturesRow).Next.Value); } private void menu_tools_item_pre_Click(object sender, EventArgs e) { set_current_picture(gallery_for_find.Find(picture_view.Tag as GalleryDataSet.picturesRow).Previous.Value); } private void 关于AToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("[email protected]"); } private void menu_file_item_scan_Click(object sender, EventArgs e) { MessageBox.Show("尚未实现"); } } }
相关阅读 更多 +