Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:45:20+00:00 2026-06-01T23:45:20+00:00

I just did a simple application to get a screenshot periodically. The main issue

  • 0

I just did a simple application to get a screenshot periodically.

The main issue I have got is that application freezes when I move it.

So the main goal is to take out this influence of the screenshot, threads and etc.

I put here all code and it works so u can reproduce it.

Here is some .NET Profiling info of this code.

enter image description here

enter image description here

Any clue how do I can fix it?

XAML

<Window x:Class="Screenshot.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" >
    <Grid Height="Auto">
        <Image Name="Image1"/>
    </Grid>
</Window>

C#

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }

        ScreenGrabber grabber;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            grabber = new ScreenGrabber(5);
            grabber.Changed += new ChangedEventHandler(grabber_Changed);
        }

        void grabber_Changed(object sender, EventArgs e)
        {
            Image1.Dispatcher.Invoke(new Action(() => {
                BitmapSource bs = ((ScreenGrabber)sender).GetImage();
                Image1.Width = bs.Width;
                Image1.Height = bs.Height;
                Image1.Source = bs;
            } ));
        } 
    }

C# DLL

namespace MyScreenGrabber
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);

    public class ScreenGrabber : Window
    {
        public event ChangedEventHandler Changed;

        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }

        byte[] BitmapData { set; get; }

        int Interval { set; get; }

        DispatcherTimer Timer { set; get; }

        public ScreenGrabber(int interval)
        {
            Interval = interval;
            Timer = new DispatcherTimer();
            Timer.Interval = new TimeSpan(0, 0, Interval);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            WindowInteropHelper windowInteropHelper = windowInteropHelper = new WindowInteropHelper(this);
            Screen screen = Screen.FromHandle(windowInteropHelper.Handle);

            using (MemoryStream ms = new MemoryStream())
            {
                if (screen != null)
                {
                    using (Bitmap bitmap = new Bitmap(screen.Bounds.Size.Width, screen.Bounds.Size.Height))
                    {
                        using (Graphics g = Graphics.FromImage(bitmap))
                        {
                            g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
                        }
                        ImageCodecInfo myImageCodecInfo;
                        myImageCodecInfo = GetEncoderInfo("image/jpeg");
                        System.Drawing.Imaging.Encoder myEncoder;
                        myEncoder = System.Drawing.Imaging.Encoder.Quality;
                        EncoderParameters encoderParameters = new EncoderParameters();
                        EncoderParameter encoderParameter = new EncoderParameter(myEncoder, 25L);
                        encoderParameters.Param[0] = encoderParameter;
                        bitmap.Save(ms, myImageCodecInfo, encoderParameters);
                        BitmapData = ms.ToArray();
                        OnChanged(EventArgs.Empty);
                    }
                }
            }
        }

        static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        public BitmapSource GetImage()
        {
            using (MemoryStream ms = new MemoryStream(this.BitmapData))
            {
                var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }
    }
}

Optimized code:

namespace MyScreenGrabber
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);

    public class ScreenGrabber : Window
    {
        public event ChangedEventHandler Changed;

        protected virtual void OnChanged(EventArgs e)
        {
            if (Changed != null)
                Changed(this, e);
        }

        byte[] BitmapData { set; get; }

        int Interval { set; get; }

        WindowInteropHelper windowInteropHelper;
        Screen screen;

        DispatcherTimer Timer { set; get; }

        BackgroundWorker worker = new BackgroundWorker();

        public ScreenGrabber(int interval)
        {
            Interval = interval;

            windowInteropHelper = windowInteropHelper = new WindowInteropHelper(this);
            screen = Screen.FromHandle(windowInteropHelper.Handle);

            isDone = true;

            Timer = new DispatcherTimer();
            Timer.Interval = new TimeSpan(0, 0, Interval);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();

            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OnChanged(EventArgs.Empty);
            isDone = true;
        }

        bool isDone;

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            GetScreenshot();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            if (isDone)
            {
                isDone = false;
                worker.RunWorkerAsync();
            }
        }

        void GetScreenshot()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                if (screen != null)
                {
                    using (Bitmap bitmap = new Bitmap(screen.Bounds.Size.Width, screen.Bounds.Size.Height))
                    {
                        using (Graphics g = Graphics.FromImage(bitmap))
                        {
                            g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
                        }
                        ImageCodecInfo myImageCodecInfo;
                        myImageCodecInfo = GetEncoderInfo("image/jpeg");
                        System.Drawing.Imaging.Encoder myEncoder;
                        myEncoder = System.Drawing.Imaging.Encoder.Quality;
                        EncoderParameters encoderParameters = new EncoderParameters();
                        EncoderParameter encoderParameter = new EncoderParameter(myEncoder, 25L);
                        encoderParameters.Param[0] = encoderParameter;
                        bitmap.Save(ms, myImageCodecInfo, encoderParameters);
                        BitmapData = ms.ToArray();
                    }
                }
            }
        }

        static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        public BitmapSource GetImage()
        {
            using (MemoryStream ms = new MemoryStream(this.BitmapData))
            {
                var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:45:21+00:00Added an answer on June 1, 2026 at 11:45 pm

    I believe you can avoid this by using a background worker process to execute the function that takes the screenshot.

    As the background worker uses another thread and the main thread continues to render the UI , it should not get stuck.

    EDIT :// I found this question on SO that might clarify things on Background Workers VS Delegates

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is a simple issue, but I have noticed that when I
I have worked on a simple application. It application includes diffrent levels that can
WPF can be infuriating sometimes. I have a fairly simple application that consists of
I am working on a simple app engine application that utilize the geo-searching proximity_fetch.
I just got started today developing for Android. I tried to make an application
I was just recently futzing about with a sample application trying to get my
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I did a simple view based application. I need to place three textfields in
In an application that I'm writing I have to load a lot of data
I am trying to create a single executable of a simple Win32 application that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.