private ImageEntry SaveBitmap(Bitmap bmp, Screen screen) { ImageEntry result = null; ImageCodecInfo ici = GetEncoderInfo("image/jpeg"); using (EncoderParameters p = GetEncoderParameters()) { //string path = dataFolder + "\\" + DateTime.Now.Ticks.ToString() + ".jpg"; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { bmp.Save(stream, ici, p); //bmp.Save(path, ici, p); string base64data = Convert.ToBase64String(stream.ToArray()); using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO images (name, data) VALUES(@0,@1)"; string name = DateTime.Now.ToString() + " - " + screen.DeviceName; SQLiteParameter pname = new SQLiteParameter("@0", name); SQLiteParameter pdata = new SQLiteParameter("@1", base64data); cmd.Parameters.Add(pname); cmd.Parameters.Add(pdata); cmd.ExecuteNonQuery(); result = new ImageEntry() { ID = conn.LastInsertRowId, Name = name }; } } } return(result); }
private void LoadData() { using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = "SELECT id, name FROM images ORDER BY id DESC"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { ImageEntry entry = new ImageEntry { ID = reader.GetInt32(0), Name = reader.GetString(1) }; lstItems.Items.Add(entry); } txtNum.Text = lstItems.Items.Count.ToString(); } } }
private void UpdateNewEntry(ImageEntry entry) { lstItems.Items.Insert(0, entry); if (lstItems.Items.Count > max) { int removenum = lstItems.Items.Count - max; for (int i = 0; i < removenum; i++) { ImageEntry rentry = lstItems.Items[lstItems.Items.Count - 1] as ImageEntry; lstItems.Items.RemoveAt(lstItems.Items.Count - 1); using (SQLiteCommand cmd = conn.CreateCommand()) { cmd.CommandText = "DELETE FROM images WHERE id = @0"; SQLiteParameter pid = new SQLiteParameter("@0", rentry.ID); cmd.Parameters.Add(pid); cmd.ExecuteNonQuery(); } } } txtNum.Text = lstItems.Items.Count.ToString(); }
private void TakeScreenShot(object sender, System.Timers.ElapsedEventArgs e) { foreach (Screen screen in Screen.AllScreens) { using (Bitmap bmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height)) { using (Graphics g = Graphics.FromImage(bmp)) { try { g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size); ImageEntry entry = SaveBitmap(bmp, screen); if (entry != null) { this.Invoke((MethodInvoker)(() => UpdateNewEntry(entry))); } } catch { } } } } timer.Interval = (random.Next(period) + 1) * 1000; }