Ejemplo n.º 1
0
 public static BitmapImage CaptureRect(System.Drawing.Rectangle rect, ImageFormat format)
 {
     using (var ms = new System.IO.MemoryStream())
     {
         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rect.Width, rect.Height,
                                                                         System.Drawing.Imaging.PixelFormat.Format32bppRgb))
         {
             using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
             {
                 graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, System.Drawing.CopyPixelOperation.SourceCopy);
             }
             bitmap.Save(ms, format);
         }
         var image = new BitmapImage();
         image.BeginInit();
         image.CacheOption  = BitmapCacheOption.OnLoad;
         image.StreamSource = ms;
         image.EndInit();
         return(image);
     }
 }
Ejemplo n.º 2
0
        private void RefreshAvator()
        {
            double TailHeight = Source.Height * Tailor.Height / SourceImage.Height;
            double TailWidth  = Source.Width * Tailor.Width / SourceImage.Width;
            double TailLeft   = Source.Width * (Canvas.GetLeft(Tailor) - Canvas.GetLeft(SourceImage)) / SourceImage.Width;
            double TailTop    = Source.Height * (Canvas.GetTop(Tailor) - Canvas.GetTop(SourceImage)) / SourceImage.Height;

            Target = new Bitmap((int)TailWidth, (int)TailHeight); //目标图
            var Graphic = Graphics.FromImage(Target);

            Graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            Graphic.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Graphic.Clear(System.Drawing.Color.Transparent);

            var SrcRect = new System.Drawing.Rectangle
                              ((int)TailLeft, (int)TailTop,
                              (int)TailWidth, (int)TailHeight);
            var DestRect = new System.Drawing.Rectangle
                               (0, 0, (int)TailWidth, (int)TailHeight);

            Graphic.DrawImage(Source, DestRect, SrcRect, GraphicsUnit.Pixel);
            Graphic.Save();
            Graphic.Dispose();

            using (MemoryStream Stream = new MemoryStream())
            {
                Target.Save(Stream, Source.RawFormat);
                BitmapImage Temp = new BitmapImage();
                Temp.BeginInit();
                Temp.CacheOption  = BitmapCacheOption.OnLoad;
                Temp.StreamSource = Stream;
                Temp.EndInit();
                AvatorImage.ImageSource = Temp;
            }

            SizeText.Text = "尺寸:" + Target.Width + "×" + Target.Height;
        }
Ejemplo n.º 3
0
        private void WindowScreenshotWithoutClass(String filename)
        {
            System.Drawing.Rectangle bounds = new System.Drawing.Rectangle((int)gcwLocal.Left, (int)gcwLocal.Top, (int)gcwLocal.Width, (int)gcwLocal.Height);
            left   = bounds.Left;
            top    = bounds.Top;
            width  = bounds.Width;
            height = bounds.Height;


            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }


                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                bitmap.Save(filename, ImageFormat.Bmp);
            }
        }
        private void ExecuteSelectImageCommand()
        {
            testItemController.MinimizeTestItemEditorWindow();

            Window window = new Window();
            window.Height = SystemParameters.VirtualScreenHeight;
            window.Width = SystemParameters.VirtualScreenWidth;
            bool doPicture = false;

            Canvas canvas = new Canvas();
            canvas.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(100, 100,100,100));
            Rectangle rectangle = new Rectangle();
            rectangle.StrokeThickness = 2;
            rectangle.Stroke = Brushes.Red;
            rectangle.Height = 0;
            rectangle.Width = 0;
            Canvas.SetTop(rectangle, 0);
            Canvas.SetLeft(rectangle, 0);
            System.Drawing.Point point = new System.Drawing.Point();

            GlobalHooker hooker = new GlobalHooker();
            MouseHookListener listener = new MouseHookListener(hooker);

            bool isMouseDown = false;
            bool isComplete = false;
            System.Drawing.Point topLeft = new System.Drawing.Point();
            System.Drawing.Point bottomRight = new System.Drawing.Point();

            listener.MouseDown += (o, args) =>
            {
                isMouseDown = true;
                topLeft = System.Windows.Forms.Cursor.Position;
            };

            listener.MouseUp += (o, args) =>
            {

                isMouseDown = false;
                bottomRight = System.Windows.Forms.Cursor.Position;
                isComplete = true;
            };

            listener.Enabled = true;

            Task task = new Task(() =>
            {
                listener.Enabled = true;

                while (!isComplete)
                {
                    Thread.Sleep(250);
                    while (isMouseDown)
                    {
                        point = System.Windows.Forms.Cursor.Position;

                        Rect bounds = new Rect(new Point(topLeft.X, topLeft.Y), new Point(point.X, point.Y));

                        Thread.Sleep(100);
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            rectangle.Width = bounds.Width;
                            rectangle.Height = bounds.Height;
                            Canvas.SetTop(rectangle, bounds.Y);
                            Canvas.SetLeft(rectangle, bounds.X);
                        }));
                    }
                }

            });

            canvas.Children.Add(rectangle);
            window.Left = 0;
            window.Top = 0;

            window.Content = canvas;
            window.WindowStyle = new WindowStyle();
            window.ShowInTaskbar = false;
            window.AllowsTransparency = true;

            Bitmap screenshot = new Bitmap(1,1);

            BitmapImage bitmapImage = new BitmapImage();

            window.Topmost = true;

            Task waitTask = new Task(() =>
            {
                Thread.Sleep(3000);
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    using (MemoryStream memory = new MemoryStream())
                    {
                        screenshot = Camera.Capture(0, ScreenCaptureMode.Screen);
                        screenshot.Save(memory, ImageFormat.Png);
                        memory.Position = 0;

                        bitmapImage.BeginInit();
                        bitmapImage.StreamSource = memory;
                        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                        bitmapImage.EndInit();
                    }

                    window.Background = new ImageBrush(bitmapImage);

                    window.Show();
                }));

            });

            task.ContinueWith(t =>
            {
                System.Drawing.Rectangle systemRect = new System.Drawing.Rectangle(topLeft.X, topLeft.Y,
                    bottomRight.X-topLeft.X, bottomRight.Y-topLeft.Y);

                Bitmap tempBitmap = new Bitmap(systemRect.Width, systemRect.Height);

                using (Graphics g = Graphics.FromImage(tempBitmap))
                {
                    g.DrawImage(screenshot, new System.Drawing.Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height),
                        systemRect, GraphicsUnit.Pixel);
                }

                Image = tempBitmap;

                TestItem testItem = testItemController.CurrentTestItem;
                OperationParameter operationParameter = testItem.Operation.GetParameterNamed("Image");
                operationParameter.Value = Image;

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    window.Close();
                    Thread.Sleep(500);
                    testItemController.RestoreTestItemEditorWindow();
                    Thread.Sleep(500);
                    testItemController.RestoreTestItemEditorWindow();
                }));

            });

            waitTask.Start();
            waitTask.ContinueWith(t => task.Start());

            window.Closed += (o, args) =>
            {
                doPicture = false;
                task.Dispose();
            };
        }
Ejemplo n.º 5
0
        public MainWindow()
        {
            InitializeComponent();

            myFlag = 0;
            //  BitmapImage img = new BitmapImage();
            //   BitmapImage imgPopl = new BitmapImage();
            //  BitmapImage imgVosk = new BitmapImage();

            Rectangle rect = new Rectangle();

            System.Drawing.Rectangle myrect = new System.Drawing.Rectangle();
            System.Drawing.Size      size   = new System.Drawing.Size(200, 200);

            this.Loaded += delegate
            {
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Elapsed += delegate
                {
                    if (myFlag == 1)
                    {
                        this.Dispatcher.Invoke(new Action(delegate
                        {
                            Mouse.Capture(this);
                            System.Windows.Point pointToWindow = Mouse.GetPosition(this);
                            System.Windows.Point pointToScreen = PointToScreen(pointToWindow);
                            LbPos.Content = pointToScreen.ToString();
                            Mouse.Capture(null);

                            rect.RadiusX  = pointToScreen.X;
                            rect.RadiusY  = pointToScreen.Y;
                            myrect.X      = Convert.ToInt32(pointToScreen.X);
                            myrect.Y      = Convert.ToInt32(pointToScreen.Y);
                            myrect.Height = 200;
                            myrect.Width  = 200;
                            myrect.Size   = size;

                            img                       = CaptureRect(myrect, ImageFormat.Png);
                            imgPlane.Source           = img;
                            imgPlane.StretchDirection = StretchDirection.Both;

                            if (f1flag == 1)
                            {
                                imgPopl = img;
                                //   myFlag = 0;
                                imagePoplavok.Source = img;
                                f1flag = 0;
                                //    btnStart.Content = "Start";
                            }
                            if (f2flag == 1)
                            {
                                imgVosk = img;
                                //    myFlag = 0;
                                imageVoskl.Source = img;
                                f2flag            = 0;
                                //    btnStart.Content = "Start";
                            }
                        }));
                    }
                };
                timer.Interval = 100;
                timer.Start();
            };
        }