# HG changeset patch # User StephaneLenclud # Date 1483624483 -3600 # Node ID a4a341accc89c6cf1a264e227b887bfa8e5913b1 # Parent 920fea7a64277cd13d1db8615a5cf8fa74c700cf Published v1.4.2 Fixing issues with Audio Visualizer bitmaps created when form is not visible. Now reusing Audio Visualizer bitmap for better performance and memory usage. diff -r 920fea7a6427 -r a4a341accc89 Server/FormMain.cs --- a/Server/FormMain.cs Wed Jan 04 18:43:28 2017 +0100 +++ b/Server/FormMain.cs Thu Jan 05 14:54:43 2017 +0100 @@ -93,6 +93,7 @@ DateTime LastTickTime; Display iDisplay; System.Drawing.Bitmap iBmp; + //TODO: Align that with what we did from Audio Visualizers bitmaps? bool iCreateBitmap; //Workaround render to bitmap issues when minimized ServiceHost iServiceHost; // Our collection of clients sorted by session id. @@ -729,8 +730,7 @@ /// private void UpdateAudioVisualization() { - // For demo draft purposes just fetch the firt picture box control and update it with current audio spectrum - + // No point if we don't have a current client if (iCurrentClientData == null) { return; @@ -755,14 +755,9 @@ if (ctrl is PictureBox) { PictureBox pb = (PictureBox)ctrl; - Image image = pb.Image; - // TODO: recycle images - var newImage = iLineSpectrum.Render(pb.Size, Color.Black, Color.Black, Color.White, false); - if (newImage != null) + if (iLineSpectrum.Render(pb.Image, Color.Black, Color.Black, Color.White, false)) { - pb.Image = newImage; - if (image != null) - image.Dispose(); + pb.Invalidate(); } } } @@ -1197,6 +1192,8 @@ } } + UpdateAudioVisualization(); + //Update our display if (iDisplay.IsOpen()) { @@ -1238,9 +1235,7 @@ iDisplay.SwapBuffers(); } - } - - UpdateAudioVisualization(); + } //Compute instant FPS toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " + @@ -2429,6 +2424,19 @@ picture.Location = new System.Drawing.Point(1, 1); picture.Margin = new System.Windows.Forms.Padding(0); picture.Name = "pictureBox" + aField; + picture.SizeChanged += (sender, e) => + { + // Somehow bitmap created when our from is invisible are not working + // Mark our form visibility status + bool visible = Visible; + // Make sure it's visible + Visible = true; + // Adjust our bitmap size when control size changes + picture.Image = new System.Drawing.Bitmap(picture.Width, picture.Height, PixelFormat.Format32bppArgb); + // Restore our form visibility + Visible = visible; + }; + control = picture; } diff -r 920fea7a6427 -r a4a341accc89 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Wed Jan 04 18:43:28 2017 +0100 +++ b/Server/SharpDisplayManager.csproj Thu Jan 05 14:54:43 2017 +0100 @@ -34,7 +34,7 @@ index.htm false 0 - 1.3.5.0 + 1.4.2.0 false true true diff -r 920fea7a6427 -r a4a341accc89 Server/Spectrum/LineSpectrum.cs --- a/Server/Spectrum/LineSpectrum.cs Wed Jan 04 18:43:28 2017 +0100 +++ b/Server/Spectrum/LineSpectrum.cs Thu Jan 05 14:54:43 2017 +0100 @@ -77,28 +77,21 @@ return SpectrumProvider.GetFftData(iFftBuffer, this); } - public Bitmap CreateSpectrumLine(Size size, Brush brush, Color background, bool highQuality) + private bool CreateSpectrumLine(Image aImage, Brush brush, Color background, bool highQuality) { - if (!UpdateFrequencyMappingIfNessesary(size)) - { - return null; - } - //get the fft result from the spectrum provider using (var pen = new Pen(brush, (float)_barWidth)) { - var bitmap = new Bitmap(size.Width, size.Height); - - using (Graphics graphics = Graphics.FromImage(bitmap)) + using (Graphics graphics = Graphics.FromImage(aImage)) { PrepareGraphics(graphics, highQuality); graphics.Clear(background); - CreateSpectrumLineInternal(graphics, pen, iFftBuffer, size); + CreateSpectrumLineInternal(graphics, pen, iFftBuffer, aImage.Size); } + } - return bitmap; - } + return true; } /// @@ -110,21 +103,26 @@ /// /// /// - public Bitmap Render(Size size, Color color1, Color color2, Color background, bool highQuality) + public bool Render(Image aImage, Color color1, Color color2, Color background, bool highQuality) { - if (!UpdateFrequencyMappingIfNessesary(size)) + if (!UpdateFrequencyMappingIfNessesary(aImage.Size)) { - return null; + return false; } - using ( - Brush brush = new LinearGradientBrush(new RectangleF(0, 0, (float)_barWidth, size.Height), color2, - color1, LinearGradientMode.Vertical)) + using (Brush brush = new LinearGradientBrush(new RectangleF(0, 0, (float)_barWidth, aImage.Size.Height), color2, color1, LinearGradientMode.Vertical)) { - return CreateSpectrumLine(size, brush, background, highQuality); + return CreateSpectrumLine(aImage, brush, background, highQuality); } } + /// + /// + /// + /// + /// + /// + /// private void CreateSpectrumLineInternal(Graphics graphics, Pen pen, float[] fftBuffer, Size size) { int height = size.Height;