public override void Paint(Graphics gr) { if (PlotterControl == null) { throw new InvalidOperationException("PlotterControl is null"); } if (_function == null) { return; } int clientWidth = PlotterControl.ClientRectangle.Width; if (_linesToDraw == null || _linesToDraw.Length != PlotterControl.ClientRectangle.Width) { _linesToDraw = new Point[PlotterControl.ClientRectangle.Width]; } for (int screenX = 0; screenX < clientWidth; screenX++) { double spaceX = PlotterControl.ScreenX2SpaceX(screenX); double spaceY = _function(spaceX); int screenY = PlotterControl.SpaceY2ScreenY(spaceY); _linesToDraw[screenX] = new Point(screenX, screenY); } using (Pen pen = new Pen(ForeColor, LineWidth)) { pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round; gr.DrawLines(pen, _linesToDraw); } }
public override void Paint(System.Drawing.Graphics gr) { if (PlotterControl == null) { throw new InvalidOperationException("PlotterControl is null"); } if (_function == null) { return; } int screenWidth = PlotterControl.ClientRectangle.Width; int screenHeight = PlotterControl.ClientRectangle.Height; Bitmap bmp = new Bitmap(screenWidth, screenHeight, PixelFormat.Format32bppArgb); Rectangle wholeBitmap = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(wholeBitmap, ImageLockMode.WriteOnly, bmp.PixelFormat); try { int strideAbs = Math.Abs(bmpData.Stride); bool bottomUp = bmpData.Stride < 0; byte[] bmpBuf = new byte[strideAbs * bmpData.Height]; int scanLine = 0; int yFirst = bottomUp ? bmpData.Height - 1 : 0; int yAfterLast = bottomUp ? -1 : bmpData.Height; int dy = bottomUp ? -1 : 1; for (int screenY = yFirst; screenY != yAfterLast; screenY += dy, scanLine += strideAbs) { double y = PlotterControl.ScreenY2SpaceY(screenY); int pixelIndex = scanLine; for (int screenX = 0; screenX < bmpData.Width; screenX++) { double x = PlotterControl.ScreenX2SpaceX(screenX); Color col = _function(x, y); bmpBuf[pixelIndex++] = col.B; bmpBuf[pixelIndex++] = col.G; bmpBuf[pixelIndex++] = col.R; bmpBuf[pixelIndex++] = col.A; } } System.Runtime.InteropServices.Marshal.Copy(bmpBuf, 0, bmpData.Scan0, bmpBuf.Length); } finally { bmp.UnlockBits(bmpData); } /*for (int screenY = 0; screenY < screenHeight; screenY++) * { * double y = PlotterControl.ScreenY2SpaceY(screenY); * for (int screenX = 0; screenX < screenWidth; screenX++) * { * double x = PlotterControl.ScreenX2SpaceX(screenX); * bmp.SetPixel(screenX, screenY, _function(x, y)); * } * }*/ gr.DrawImage(bmp, 0, 0); }