Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
Posted
Updated 28-Aug-18 4:24am
Comments
Richard MacCutchan 28-Aug-18 5:14am    
Where is the code for objScreenCapture.GetSnapShot(); and what does it return? Also, could the problem be in

private void AddTopreview(object snap1)
{
throw new NotImplementedException();
}

?
Member 13958707 28-Aug-18 7:46am    
Thank you for pointing it. It should return the snap shot and it should be loaded to tje image list which I'm unable to do

1 solution

Quote:
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


One can't be concerned about the "image list" until one has actually confirmed that a screen is being captured; which you haven't.

Concentrate on getting the screen capture to work; THEN focus on the rest of it.

c# - Capture screenshot of active window? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900