/// <summary> /// Creates waveform image. /// Length of level arrays can be less then number of render frames for a partial render. /// </summary> private static Bitmap CreateWaveformImage(WaveformDirection direction, WaveformSideOrientation sideOrientation, int width, int height, float peakValue, int numFullRenderFrames, float[] leftLevelArr, float[] rightLevelArr, Brush leftSideBrush, Brush rightSideBrush, Brush centerLineBrush) { // Perform argument checks. if (width <= 0) { throw new ArgumentException("Width is not positive.", "width"); } if (height <= 0) { throw new ArgumentException("Height is not positive.", "height"); } if (leftLevelArr.Length != rightLevelArr.Length) { throw new ArgumentException("Left and right level array is not of the same length.", "rightRenderLevelArr"); } if (direction == WaveformDirection.LeftToRight || direction == WaveformDirection.RightToLeft) { // ~~ Left to right or right to left. return(CreateHorizontalWaveformImage(direction, sideOrientation, width, height, peakValue, numFullRenderFrames, leftLevelArr, rightLevelArr, leftSideBrush, rightSideBrush, centerLineBrush)); } else { // ~~ Top to bottom or bottom to top. return(CreateVerticalWaveformImage(direction, sideOrientation, width, height, peakValue, numFullRenderFrames, leftLevelArr, rightLevelArr, leftSideBrush, rightSideBrush, centerLineBrush)); } }
/// <summary> /// Creates waveform image. /// Length of level arrays can be less then number of render frames for a partial render. /// </summary> private static Bitmap CreateHorizontalWaveformImage(WaveformDirection direction, WaveformSideOrientation sideOrientation, int width, int height, float peakValue, int numFullRenderFrames, float[] leftLevelArr, float[] rightLevelArr, Brush leftSideBrush, Brush rightSideBrush, Brush centerLineBrush) { int numRenderFrames = leftLevelArr.Length; bool isPartialRender = numRenderFrames < numFullRenderFrames; double frameThickness = 1d * width / numFullRenderFrames; double sideHeight = height / 2d; double centerLineY = (height - 1) / 2; PointF[] topPointArr = new PointF[numRenderFrames + 3]; PointF[] bottomPointArr = new PointF[numRenderFrames + 3]; // Change peak value if partial render. if (isPartialRender) { peakValue = Math.Max(1f, peakValue); } // Make sure peakValue != 0 to avoid division by zero. if (peakValue == 0) { peakValue = 1f; } // Add start points. if (direction == WaveformDirection.LeftToRight) { // ~~ Left to right. topPointArr[0] = new PointF(0f, (float)centerLineY); bottomPointArr[0] = new PointF(0f, (float)centerLineY); } else { // ~~ Right to left. topPointArr[0] = new PointF(width - 1, (float)centerLineY); bottomPointArr[0] = new PointF(width - 1, (float)centerLineY); } double xLocation = -1d; // Add main points. for (int i = 0; i < numRenderFrames; i++) { if (direction == WaveformDirection.LeftToRight) { // ~~ Left to right. xLocation = (i * frameThickness + (i + 1) * frameThickness) / 2; } else { // ~~ Right to left. xLocation = width - 1 - ((i * frameThickness + (i + 1) * frameThickness) / 2); } double topRenderHeight, bottomRenderHeight; if (sideOrientation == WaveformSideOrientation.LeftSideOnTopOrLeft) { // ~~ Left side on top, right side on bottom. topRenderHeight = 1d * leftLevelArr[i] / peakValue * sideHeight; bottomRenderHeight = 1d * rightLevelArr[i] / peakValue * sideHeight; } else { // ~~ Left side on bottom, right side on top. topRenderHeight = 1d * rightLevelArr[i] / peakValue * sideHeight; bottomRenderHeight = 1d * leftLevelArr[i] / peakValue * sideHeight; } topPointArr[i + 1] = new PointF((float)xLocation, (float)(centerLineY - topRenderHeight)); bottomPointArr[i + 1] = new PointF((float)xLocation, (float)(centerLineY + bottomRenderHeight)); } // Add end points. if (direction == WaveformDirection.LeftToRight) { // ~~ Left to right. if (isPartialRender) { // Draw straight towards line, not to end point. topPointArr[numRenderFrames + 1] = new PointF((float)xLocation, (float)centerLineY); bottomPointArr[numRenderFrames + 1] = new PointF((float)xLocation, (float)centerLineY); } else { // Draw to end point. topPointArr[numRenderFrames + 1] = new PointF(width - 1, (float)centerLineY); bottomPointArr[numRenderFrames + 1] = new PointF(width - 1, (float)centerLineY); } topPointArr[numRenderFrames + 2] = new PointF(0, (float)centerLineY); bottomPointArr[numRenderFrames + 2] = new PointF(0, (float)centerLineY); } else { // ~~ Right to left. if (isPartialRender) { // Draw straight towards line, not to end point. topPointArr[numRenderFrames + 1] = new PointF((float)xLocation, (float)centerLineY); bottomPointArr[numRenderFrames + 1] = new PointF((float)xLocation, (float)centerLineY); } else { // Draw to end point. topPointArr[numRenderFrames + 1] = new PointF(0, (float)centerLineY); bottomPointArr[numRenderFrames + 1] = new PointF(0, (float)centerLineY); } topPointArr[numRenderFrames + 2] = new PointF(width - 1, (float)centerLineY); bottomPointArr[numRenderFrames + 2] = new PointF(width - 1, (float)centerLineY); } // Create bitmap. Bitmap bm = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bm)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // Draw left and right waveform. if (sideOrientation == WaveformSideOrientation.LeftSideOnTopOrLeft) { // ~~ Left side on top, right side on bottom. g.FillPolygon(leftSideBrush, topPointArr); g.FillPolygon(rightSideBrush, bottomPointArr); } else { // ~~ Left side on bottom, right side on top. g.FillPolygon(leftSideBrush, bottomPointArr); g.FillPolygon(rightSideBrush, topPointArr); } // Create pen. Pen blackPen = new Pen(Color.Black, 3); // Create rectangle. Rectangle rect = new Rectangle(0, 0, 30, 30); // Draw rectangle to screen. g.DrawRectangle(blackPen, rect); // Draw center line. g.FillRectangle(centerLineBrush, 0, (float)(centerLineY - 0.5), width, 1); } bm.Save("TEST.JPG"); return(bm); }
/// <summary> /// Creates waveform image. /// Length of level arrays can be less then number of render frames for a partial render. /// </summary> private static Bitmap CreateVerticalWaveformImage(WaveformDirection direction, WaveformSideOrientation sideOrientation, int width, int height, float peakValue, int numFullRenderFrames, float[] leftLevelArr, float[] rightLevelArr, Brush leftSideBrush, Brush rightSideBrush, Brush centerLineBrush) { int numRenderFrames = leftLevelArr.Length; bool isPartialRender = numRenderFrames < numFullRenderFrames; double frameThickness = 1d * height / numFullRenderFrames; double sideWidth = width / 2d; double centerLineX = (width - 1) / 2; PointF[] leftPointArr = new PointF[numRenderFrames + 3]; PointF[] rightPointArr = new PointF[numRenderFrames + 3]; // Change peak value if partial render. if (isPartialRender) { peakValue = Math.Max(1f, peakValue); } // Make sure peakValue != 0 to avoid division by zero. if (peakValue == 0) { peakValue = 1f; } // Add start points. if (direction == WaveformDirection.TopToBottom) { // ~~ Top to bottom. leftPointArr[0] = new PointF((float)centerLineX, 0f); rightPointArr[0] = new PointF((float)centerLineX, 0f); } else { // ~~ Bottom to top. leftPointArr[0] = new PointF((float)centerLineX, height - 1); rightPointArr[0] = new PointF((float)centerLineX, height - 1); } double yLocation = -1d; // Add main points. for (int i = 0; i < numRenderFrames; i++) { if (direction == WaveformDirection.TopToBottom) { // ~~ Top to bottom. yLocation = (i * frameThickness + (i + 1) * frameThickness) / 2; } else { // ~~ Bottom to top. yLocation = height - 1 - ((i * frameThickness + (i + 1) * frameThickness) / 2); } double leftRenderWidth, rightRenderWidth; if (sideOrientation == WaveformSideOrientation.LeftSideOnTopOrLeft) { // ~~ Left side on left, right side on right. leftRenderWidth = 1d * leftLevelArr[i] / peakValue * sideWidth; rightRenderWidth = 1d * rightLevelArr[i] / peakValue * sideWidth; } else { // ~~ Left side on right, right side on left. leftRenderWidth = 1d * rightLevelArr[i] / peakValue * sideWidth; rightRenderWidth = 1d * leftLevelArr[i] / peakValue * sideWidth; } leftPointArr[i + 1] = new PointF((float)(centerLineX - leftRenderWidth), (float)yLocation); rightPointArr[i + 1] = new PointF((float)(centerLineX + leftRenderWidth), (float)yLocation); } // Add end points. if (direction == WaveformDirection.TopToBottom) { // ~~ Top to bottom. if (isPartialRender) { // Draw straight towards line, not to end point. leftPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, (float)yLocation); rightPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, (float)yLocation); } else { // Draw to end point. leftPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, height - 1); rightPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, height - 1); } leftPointArr[numRenderFrames + 2] = new PointF((float)centerLineX, 0f); rightPointArr[numRenderFrames + 2] = new PointF((float)centerLineX, 0f); } else { // ~~ Bottom to top. if (isPartialRender) { // Draw straight towards line, not to end point. leftPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, (float)yLocation); rightPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, (float)yLocation); } else { // Draw to end point. leftPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, 0f); rightPointArr[numRenderFrames + 1] = new PointF((float)centerLineX, 0f); } leftPointArr[numRenderFrames + 2] = new PointF((float)centerLineX, height - 1); rightPointArr[numRenderFrames + 2] = new PointF((float)centerLineX, height - 1); } // Create bitmap. Bitmap bm = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bm)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; // Draw left and right waveform. if (sideOrientation == WaveformSideOrientation.LeftSideOnTopOrLeft) { // ~~ Left side on left, right side on right. g.FillPolygon(leftSideBrush, leftPointArr); g.FillPolygon(rightSideBrush, rightPointArr); } else { // ~~ Left side on right, right side on left. g.FillPolygon(leftSideBrush, rightPointArr); g.FillPolygon(rightSideBrush, leftPointArr); } // Draw center line. g.FillRectangle(centerLineBrush, (float)(centerLineX - 0.5), 0, 1, height); } return(bm); }