MainForm.cs
changeset 14 d1fa9de444b0
parent 13 745d9ee1a2c0
child 16 985ca4b6e099
     1.1 --- a/MainForm.cs	Sat Jul 12 13:13:17 2014 +0200
     1.2 +++ b/MainForm.cs	Sat Jul 12 22:18:34 2014 +0200
     1.3 @@ -7,7 +7,10 @@
     1.4  using System.Text;
     1.5  using System.Threading.Tasks;
     1.6  using System.Windows.Forms;
     1.7 +using System.IO;
     1.8  using CodeProject.Dialog;
     1.9 +using System.Drawing.Imaging;
    1.10 +
    1.11  
    1.12  namespace SharpDisplayManager
    1.13  {
    1.14 @@ -15,6 +18,8 @@
    1.15      {
    1.16          DateTime LastTickTime;
    1.17          Display iDisplay;
    1.18 +        System.Drawing.Bitmap iBmp;
    1.19 +        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
    1.20  
    1.21          public MainForm()
    1.22          {
    1.23 @@ -31,6 +36,9 @@
    1.24              checkBoxConnectOnStartup.Checked = Properties.Settings.Default.DisplayConnectOnStartup;
    1.25              //
    1.26              tableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked ? TableLayoutPanelCellBorderStyle.Single : TableLayoutPanelCellBorderStyle.None);
    1.27 +            //We have a bug when drawing minimized and reusing our bitmap
    1.28 +            iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    1.29 +            iCreateBitmap = false;
    1.30          }
    1.31  
    1.32          private void MainForm_Load(object sender, EventArgs e)
    1.33 @@ -80,7 +88,22 @@
    1.34          {
    1.35              System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    1.36              tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    1.37 -            bmp.Save("c:\\capture.png");
    1.38 +            //Bitmap bmpToSave = new Bitmap(bmp);
    1.39 +            bmp.Save("D:\\capture.png");
    1.40 +
    1.41 +            /*
    1.42 +            string outputFileName = "d:\\capture.png";
    1.43 +            using (MemoryStream memory = new MemoryStream())
    1.44 +            {
    1.45 +                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    1.46 +                {
    1.47 +                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
    1.48 +                    byte[] bytes = memory.ToArray();
    1.49 +                    fs.Write(bytes, 0, bytes.Length);
    1.50 +                }
    1.51 +            }
    1.52 +             */
    1.53 +
    1.54          }
    1.55  
    1.56          private void CheckForRequestResults()
    1.57 @@ -117,7 +140,7 @@
    1.58          }
    1.59  
    1.60          private void timer_Tick(object sender, EventArgs e)
    1.61 -        {        
    1.62 +        {
    1.63              //Update our animations
    1.64              DateTime NewTickTime = DateTime.Now;
    1.65  
    1.66 @@ -129,19 +152,27 @@
    1.67              {
    1.68                  CheckForRequestResults();
    1.69  
    1.70 -                //Draw to bitmap
    1.71 -                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height);
    1.72 -                tableLayoutPanel.DrawToBitmap(bmp, tableLayoutPanel.ClientRectangle);
    1.73 +                //Draw to bitmap                
    1.74 +                if (iCreateBitmap)
    1.75 +                {
    1.76 +                    iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    1.77 +                }
    1.78 +                tableLayoutPanel.DrawToBitmap(iBmp, tableLayoutPanel.ClientRectangle);
    1.79 +                //iBmp.Save("D:\\capture.png");
    1.80 +                
    1.81 +                //iBmp.
    1.82 +
    1.83                  //Send it to our display
    1.84 -                for (int i = 0; i < bmp.Width; i++)
    1.85 +                for (int i = 0; i < iBmp.Width; i++)
    1.86                  {
    1.87 -                    for (int j = 0; j < bmp.Height; j++)
    1.88 +                    for (int j = 0; j < iBmp.Height; j++)
    1.89                      {
    1.90                          unchecked
    1.91                          {
    1.92 -                        uint color=(uint)bmp.GetPixel(i, j).ToArgb();
    1.93 -                        //(checkBoxShowBorders.Checked?color!=0xFFFFFFFF:color == 0xFF000000)
    1.94 -                        iDisplay.SetPixel(i, j, Convert.ToInt32(color != 0xFFFFFFFF));
    1.95 +                            uint color = (uint)iBmp.GetPixel(i, j).ToArgb();
    1.96 +                            //For some reason when the app is minimized in the task bar only the alpha of our color is set.
    1.97 +                            //Thus that strange test for rendering to work both when the app is in the task bar and when it isn't.
    1.98 +                            iDisplay.SetPixel(i, j, Convert.ToInt32(!(color != 0xFF000000)));
    1.99                          }
   1.100                      }
   1.101                  }
   1.102 @@ -243,5 +274,15 @@
   1.103              Properties.Settings.Default.Save();
   1.104          }
   1.105  
   1.106 +        private void MainForm_Resize(object sender, EventArgs e)
   1.107 +        {
   1.108 +            if (WindowState == FormWindowState.Minimized)
   1.109 +            {
   1.110 +                // Do some stuff
   1.111 +                //iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
   1.112 +                iCreateBitmap = true;
   1.113 +            }
   1.114 +        }
   1.115 +
   1.116      }
   1.117  }