I'm developing an application which takes a screenshot an loads to a imagelist on another form.After I run the application the 2nd the captured image is not saved to the image list on the other form and I don't see if the screenshot got captured or not. Any solutions?
What I have tried:
This is my code to capture the screenshot.
public partial class Form1 : Form
{
Point startPos;
Point currentpos;
bool drawing;
public Form1()
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackColor = Color.White;
this.Opacity = 0.75;
this.Cursor = Cursors.Cross;
this.MouseDown += Form1_MouseDown;
this.MouseMove += Form1_MouseMove;
this.MouseUp += Form1_MouseUp;
this.Paint += Form1_Paint;
this.KeyDown += Form1_KeyDown;
this.DoubleBuffered = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
currentpos = e.Location;
if (drawing) this.Invalidate();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
}
public Rectangle GetRectangle()
{
return new Rectangle(
Math.Min(startPos.X, currentpos.X),
Math.Min(startPos.Y, currentpos.Y),
Math.Abs(startPos.X - currentpos.X),
Math.Abs(startPos.Y - currentpos.Y));
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (drawing) e.Graphics.DrawRectangle(Pens.Red, GetRectangle());
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
currentpos = startPos = e.Location;
drawing = true;
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
This is my code for imagelist
public partial class Display : Form
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#pragma warning restore CS0626 // Method, operator, or accessor is marked external and has no attributes on it
private const int TAKE_SNAP_HOTKEY_ID = 1;
private ScreenCapture objScreenCapture;
private int snapCount;
private List<Bitmap> snaps;
public Display()
{
InitializeComponent();
objScreenCapture = new ScreenCapture();
snapCount = 0;
snaps = new List<Bitmap>();
RegisterHotKey(this.Handle, TAKE_SNAP_HOTKEY_ID, 6, (int)Keys.Z);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == TAKE_SNAP_HOTKEY_ID )
{
TakeSnap();
}
base.WndProc(ref m);
}
private void takeSnapToolStripMenuItem_click(object sender, EventArgs e)
{
TakeSnap();
}
private void setBoundsToolStripMenuItem_click(object sender, EventArgs e)
{
objScreenCapture.SetCanvas();
}
private void TakeSnap()
{
var snap = objScreenCapture.GetSnapShot();
snaps.Add(snap);
AddTopreview(snap);
}
private void AddTopreview(object snap1)
{
throw new NotImplementedException();
}
private void AddTopreview(Bitmap snap)
{
imageList1.Images.Add(snap);
listView1.Items.Add(new ListViewItem("Snap_" + (++snapCount), imageList1.Images.Count - 1)).EnsureVisible();
}
private void SaveAsImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
uti.SaveAsImages(snaps);
}
private void SaveAsWordToolStripMenuItem_Click(object sender, EventArgs e)
{
uti.SaveAsWord(snaps);
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, TAKE_SNAP_HOTKEY_ID);
}