Ejemplo n.º 1
0
 private void Init()
 {
     SystemEvents.PowerModeChanged += OnPowerChange;
     if (ConfigurationManager.AppSettings["COMPORT"] != null && ConfigurationManager.AppSettings["COMPORT"].Contains("COM") == true && ComPort.Items.Contains(ConfigurationManager.AppSettings["COMPORT"]))
     {
         ComPort.SelectedIndex = ComPort.FindString(ConfigurationManager.AppSettings["COMPORT"]);
     }
     else if (Convert.ToInt32(ConfigurationManager.AppSettings["COMPORT"]) == 0)
     {
         ComPort.SelectedIndex = 0;
     }
     LedsX.Value                  = Convert.ToInt32(ConfigurationManager.AppSettings["LEDSX"]);
     LedsY.Value                  = Convert.ToInt32(ConfigurationManager.AppSettings["LEDSY"]);
     UpperOffset.Text             = ConfigurationManager.AppSettings["UpperOffset"];
     LowerOffset.Text             = ConfigurationManager.AppSettings["LowerOffset"];
     LeftOffset.Text              = ConfigurationManager.AppSettings["LeftOffset"];
     RightOffset.Text             = ConfigurationManager.AppSettings["RightOffset"];
     CustomWidth.Text             = ConfigurationManager.AppSettings["CustomWidth"];
     CustomHeight.Text            = ConfigurationManager.AppSettings["CustomHeight"];
     BaudRate.SelectedIndex       = Convert.ToInt32(ConfigurationManager.AppSettings["BaudRate"]);
     InterpMode.SelectedIndex     = Convert.ToInt32(ConfigurationManager.AppSettings["InterpolationMode"]);
     FadeTiming.Value             = Convert.ToInt32(ConfigurationManager.AppSettings["FadeTiming"]);
     AmbilightModes.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["AmbilightModes"]);
     AudioInputs.SelectedIndex    = Convert.ToInt32(ConfigurationManager.AppSettings["AudioDevice"]);
     CaptureArea.SelectedIndex    = Convert.ToInt32(ConfigurationManager.AppSettings["CaptureArea"]);
     PreventSleep.Checked         = Convert.ToBoolean(ConfigurationManager.AppSettings["PreventSleep"]);
     PreventAwayMode.Checked      = Convert.ToBoolean(ConfigurationManager.AppSettings["PreventAwayMode"]);
     StartOnBoot.Checked          = Convert.ToBoolean(ConfigurationManager.AppSettings["StartOnBoot"]);
     if (StartOnBoot.Checked)
     {
         AutostartOnBoot.SetValue("Dynamic Ambilight", Application.ExecutablePath);
     }
     else
     {
         AutostartOnBoot.DeleteValue("Dynamic Ambilight", false);
     }
     if (ConfigurationManager.AppSettings["CapturedDevice"] != null && CapturedDevice.Items.Contains(ConfigurationManager.AppSettings["CapturedDevice"]))
     {
         CapturedDevice.SelectedIndex = CapturedDevice.FindString(ConfigurationManager.AppSettings["CapturedDevice"]);
     }
     else
     {
         CapturedDevice.SelectedIndex = 0;
     }
 }
Ejemplo n.º 2
0
        private void DXCapture()
        {
            Adapter adapter = null;

            CapturedDevice.Invoke((MethodInvoker) delegate { adapter = new Factory1().Adapters[CapturedDevice.SelectedIndex]; }); //Console.WriteLine(factory.GetAdapterCount());
            var    device = new Device(adapter);                                                                                  //Create device from Adapter //foreach (Adapter adapters in factory.Adapters) Console.WriteLine(adapters.Description.Description);
            Output output = null;                                                                                                 //Get DXGI.Output  //foreach (Output outputs in adapter.Outputs) Console.WriteLine(outputs.Description.DeviceName);

            CapturedMonitor.Invoke((MethodInvoker) delegate { output = adapter.GetOutput(CapturedMonitor.SelectedIndex); });
            var output1 = output.QueryInterface <Output1>();
            int width   = output.Description.DesktopBounds.Right;       //Width/Height of desktop to capture
            int height  = output.Description.DesktopBounds.Bottom;
            Texture2DDescription textureDesc = new Texture2DDescription //Create Staging texture CPU-accessible
            {
                CpuAccessFlags    = CpuAccessFlags.Read,
                BindFlags         = BindFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = { Count = 1, Quality = 0 },
                Usage             = ResourceUsage.Staging
            };
            var screenTexture = new Texture2D(device, textureDesc);
            var duplicatedOutput = output1.DuplicateOutput(device); //Duplicate the output
            int ledsx = LedsX.Value;
            int ledsy = LedsY.Value;
            int leftoffset = 0, upperoffset = 0, customwidth = width, customheight = height;
            int index = 0;

            CaptureArea.Invoke((MethodInvoker) delegate { index = CaptureArea.SelectedIndex; });
            if (index == 1)
            {
                leftoffset   = (width - Convert.ToInt32(CustomWidth.Text)) / 2;
                upperoffset  = (height - Convert.ToInt32(CustomHeight.Text)) / 2;
                customwidth  = Convert.ToInt32(CustomWidth.Text);
                customheight = Convert.ToInt32(CustomHeight.Text);
            }
            else if (index == 2)
            {
                leftoffset   = Convert.ToInt32(LeftOffset.Text);
                upperoffset  = Convert.ToInt32(UpperOffset.Text);
                customwidth  = width - leftoffset - Convert.ToInt32(RightOffset.Text);
                customheight = height - upperoffset - Convert.ToInt32(LowerOffset.Text);
            }
            bool init = false;

            sw.Start();
            Task.Factory.StartNew(() =>
            {
                while (StartStop.Checked && SerialPort.IsOpen)
                {
                    WakeUp();
                    try
                    {
                        duplicatedOutput.AcquireNextFrame(100, out OutputDuplicateFrameInformation duplicateFrameInformation, out SharpDX.DXGI.Resource screenResource); // Try to get duplicated frame within given time
                        if (init)
                        {
                            using (var screenTexture2D = screenResource.QueryInterface <Texture2D>()) device.ImmediateContext.CopyResource(screenTexture2D, screenTexture);  // copy resource into memory that can be accessed by the CPU
                            var mapSource  = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, MapFlags.None);                                          // Get the desktop capture texture
                            var bitmap     = new Bitmap(width, height, PixelFormat.Format32bppArgb);                                                                         // Create Drawing.Bitmap
                            var boundsRect = new Rectangle(0, 0, width, height);
                            var mapDest    = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);                                                       // Copy pixels from screen capture Texture to GDI bitmap
                            var sourcePtr  = mapSource.DataPointer;
                            var destPtr    = mapDest.Scan0;
                            for (int y = 0; y < height; y++)
                            {
                                Utilities.CopyMemory(destPtr, sourcePtr, width * 4);   // Copy a single line
                                sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); // Advance pointers
                                destPtr   = IntPtr.Add(destPtr, mapDest.Stride);
                            }
                            bitmap.UnlockBits(mapDest); // Release source and dest locks
                            device.ImmediateContext.UnmapSubresource(screenTexture, 0);
                            using (var tempbmp = new Bitmap(ledsx, ledsy, PixelFormat.Format32bppRgb))
                            {
                                Graphics tempbmpgr          = Graphics.FromImage(tempbmp);
                                tempbmpgr.InterpolationMode = intrpmode; //nearest nighbour, low, default, bicubic, bilinear,
                                tempbmpgr.DrawImage(bitmap, new Rectangle(0, 0, ledsx, ledsy), leftoffset, upperoffset, customwidth, customheight, GraphicsUnit.Pixel);
                                Color color;
                                for (int x = 0; x < ledsx; x++) //these for's take 5-7ms
                                {
                                    color = tempbmp.GetPixel(x, ledsy - 1);
                                    SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3);
                                }
                                for (int y = ledsy - 1; y >= 0; y--)
                                {
                                    color = tempbmp.GetPixel(ledsx - 1, y);
                                    SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3);
                                }
                                for (int x = ledsx - 1; x >= 0; x--)
                                {
                                    color = tempbmp.GetPixel(x, 0);
                                    SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3);
                                }
                                for (int y = 0; y < ledsy; y++)
                                {
                                    color = tempbmp.GetPixel(0, y);
                                    SerialPort.Write(new byte[] { color.R, color.G, color.B }, 0, 3);
                                }
                                ScreenRefreshed?.Invoke(this, EventArgs.Empty);
                                GC.Collect();
                            }
                        }
                        init = true;
                        screenResource.Dispose();
                        duplicatedOutput.ReleaseFrame();
                    }
                    catch (SharpDXException) { }
                }
                if (!StartStop.Checked)
                {
                    sw.Stop();
                    SerialPort.Close();
                    duplicatedOutput.Dispose();
                    screenTexture.Dispose();
                }
            });
            GC.Collect();
        }