Ejemplo n.º 1
0
        internal void WaveformPaint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            if (_wavePeaks != null)
            {
                bool showSpectrogram = IsSpectrogramAvailable && ShowSpectrogram;
                bool showSpectrogramOnly = showSpectrogram && !ShowWaveform;
                int waveformHeight = Height - (showSpectrogram ? SpectrogramDisplayHeight : 0);

                // background
                DrawBackground(graphics);

                // grid lines
                if (ShowGridLines && !showSpectrogramOnly)
                {
                    DrawGridLines(graphics, waveformHeight);
                }

                // spectrogram
                if (showSpectrogram)
                {
                    DrawSpectrogram(graphics);
                }

                // waveform
                if (ShowWaveform)
                {
                    using (var penNormal = new Pen(Color))
                    using (var penSelected = new Pen(SelectedColor)) // selected paragraph
                    {
                        var isSelectedHelper = new IsSelectedHelper(_allSelectedParagraphs, _wavePeaks.SampleRate);
                        int baseHeight = (int)(_wavePeaks.HighestPeak / VerticalZoomFactor);
                        int halfWaveformHeight = waveformHeight / 2;
                        Func<float, float> calculateY = (value) =>
                        {
                            float offset = (value / baseHeight) * halfWaveformHeight;
                            if (offset > halfWaveformHeight)
                                offset = halfWaveformHeight;
                            if (offset < -halfWaveformHeight)
                                offset = -halfWaveformHeight;
                            return halfWaveformHeight - offset;
                        };
                        for (int x = 0; x < Width; x++)
                        {
                            float pos = (float)RelativeXPositionToSeconds(x) * _wavePeaks.SampleRate;
                            int pos0 = (int)pos;
                            int pos1 = pos0 + 1;
                            if (pos1 >= _wavePeaks.Peaks.Count)
                                break;
                            float pos1Weight = pos - pos0;
                            float pos0Weight = 1F - pos1Weight;
                            var peak0 = _wavePeaks.Peaks[pos0];
                            var peak1 = _wavePeaks.Peaks[pos1];
                            float max = peak0.Max * pos0Weight + peak1.Max * pos1Weight;
                            float min = peak0.Min * pos0Weight + peak1.Min * pos1Weight;
                            float yMax = calculateY(max);
                            float yMin = Math.Max(calculateY(min), yMax + 0.1F);
                            var pen = isSelectedHelper.IsSelected(pos0) ? penSelected : penNormal;
                            graphics.DrawLine(pen, x, yMax, x, yMin);
                        }
                    }
                }

                // time line
                if (!showSpectrogramOnly)
                {
                    DrawTimeLine(graphics, waveformHeight);
                }

                // scene changes
                if (_sceneChanges != null)
                {
                    foreach (double time in _sceneChanges)
                    {
                        int pos = SecondsToXPosition(time - StartPositionSeconds);
                        if (pos > 0 && pos < Width)
                        {
                            using (var p = new Pen(Color.AntiqueWhite))
                                graphics.DrawLine(p, pos, 0, pos, Height);
                        }
                    }
                }

                // current video position
                if (_currentVideoPositionSeconds > 0)
                {
                    int pos = SecondsToXPosition(_currentVideoPositionSeconds - StartPositionSeconds);
                    if (pos > 0 && pos < Width)
                    {
                        using (var p = new Pen(Color.Turquoise))
                            graphics.DrawLine(p, pos, 0, pos, Height);
                    }
                }

                // paragraphs
                foreach (Paragraph p in _displayableParagraphs)
                {
                    if (p.EndTime.TotalSeconds >= StartPositionSeconds && p.StartTime.TotalSeconds <= EndPositionSeconds)
                    {
                        DrawParagraph(p, graphics);
                    }
                }

                // current selection
                if (NewSelectionParagraph != null)
                {
                    int currentRegionLeft = SecondsToXPosition(NewSelectionParagraph.StartTime.TotalSeconds - StartPositionSeconds);
                    int currentRegionRight = SecondsToXPosition(NewSelectionParagraph.EndTime.TotalSeconds - StartPositionSeconds);
                    int currentRegionWidth = currentRegionRight - currentRegionLeft;
                    if (currentRegionRight >= 0 && currentRegionLeft <= Width)
                    {
                        using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                            graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height);

                        if (currentRegionWidth > 40)
                        {
                            using (var brush = new SolidBrush(Color.Turquoise))
                                graphics.DrawString(string.Format("{0:0.###} {1}", ((double)currentRegionWidth / _wavePeaks.SampleRate / _zoomFactor), Configuration.Settings.Language.Waveform.Seconds), Font, brush, new PointF(currentRegionLeft + 3, Height - 32));
                        }
                    }
                }
            }
            else
            {
                DrawBackground(graphics);

                if (ShowGridLines)
                {
                    DrawGridLines(graphics, Height);
                }

                using (var textBrush = new SolidBrush(TextColor))
                using (var textFont = new Font(Font.FontFamily, 8))
                {
                    if (Width > 90)
                    {
                        graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(Width / 2 - 65, Height / 2 - 10));
                    }
                    else
                    {
                        using (var stringFormat = new StringFormat(StringFormatFlags.DirectionVertical))
                            graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(1, 10), stringFormat);
                    }
                }
            }
            if (Focused)
            {
                using (var p = new Pen(SelectedColor))
                    graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
            }
        }
Ejemplo n.º 2
0
        internal void WaveformPaint(object sender, PaintEventArgs e)
        {
            if (_wavePeaks != null && _wavePeaks.AllSamples != null)
            {
                if (StartPositionSeconds < 0)
                    StartPositionSeconds = 0;

                if (XPositionToSeconds(Width) > _wavePeaks.Header.LengthInSeconds)
                    StartPositionSeconds = _wavePeaks.Header.LengthInSeconds - ((Width / (double)_wavePeaks.Header.SampleRate) / _zoomFactor);

                Graphics graphics = e.Graphics;
                int imageHeight = Height;

                DrawBackground(graphics);

                if (IsSpectrogramAvailable && ShowSpectrogram)
                {
                    DrawSpectrogramBitmap(StartPositionSeconds, graphics);
                    if (ShowWaveform)
                        imageHeight -= SpectrogramDisplayHeight;
                }

                // waveform
                if (ShowWaveform)
                {
                    using (var penNormal = new Pen(Color))
                    using (var penSelected = new Pen(SelectedColor)) // selected paragraph
                    {
                        var pen = penNormal;
                        var isSelectedHelper = new IsSelectedHelper(_subtitle.Paragraphs, _selectedIndices, _selectedParagraph, SecondsToXPositionNoZoom);
                        int maxHeight = (int)(Math.Max(Math.Abs(_wavePeaks.DataMinValue), Math.Abs(_wavePeaks.DataMaxValue)) / VerticalZoomFactor);
                        int start = SecondsToXPositionNoZoom(StartPositionSeconds);
                        float xPrev = 0;
                        int yPrev = Height / 2;
                        float x = 0;
                        int y;
                        for (int i = 0; i < _wavePeaks.AllSamples.Count - start && x < Width; i++)
                        {
                            int n = start + i;
                            x = (float)(_zoomFactor * i);
                            y = CalculateHeight(_wavePeaks.AllSamples[n], imageHeight, maxHeight);
                            graphics.DrawLine(pen, xPrev, yPrev, x, y);
                            xPrev = x;
                            yPrev = y;
                            pen = isSelectedHelper.IsSelected(n) ? penSelected : penNormal;
                        }
                    }
                }

                // time line
                DrawTimeLine(StartPositionSeconds, e, imageHeight);

                // scene changes
                if (_sceneChanges != null)
                {
                    foreach (double time in _sceneChanges)
                    {
                        int pos = SecondsToXPosition(time - StartPositionSeconds);
                        if (pos > 0 && pos < Width)
                        {
                            using (var p = new Pen(Color.AntiqueWhite))
                            {
                                graphics.DrawLine(p, pos, 0, pos, Height);
                            }
                        }
                    }
                }

                // current video position
                if (_currentVideoPositionSeconds > 0)
                {
                    int pos = SecondsToXPosition(_currentVideoPositionSeconds - StartPositionSeconds);
                    if (pos > 0 && pos < Width)
                    {
                        using (var p = new Pen(Color.Turquoise))
                        {
                            graphics.DrawLine(p, pos, 0, pos, Height);
                        }
                    }
                }

                // paragraphs
                using (var textBrush = new SolidBrush(TextColor))
                {
                    DrawParagraph(_currentParagraph, e, textBrush);
                    foreach (Paragraph p in _previousAndNextParagraphs)
                        DrawParagraph(p, e, textBrush);
                }

                // current selection
                if (NewSelectionParagraph != null)
                {
                    int currentRegionLeft = SecondsToXPosition(NewSelectionParagraph.StartTime.TotalSeconds - StartPositionSeconds);
                    int currentRegionRight = SecondsToXPosition(NewSelectionParagraph.EndTime.TotalSeconds - StartPositionSeconds);
                    int currentRegionWidth = currentRegionRight - currentRegionLeft;
                    using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
                    {
                        if (currentRegionLeft >= 0 && currentRegionLeft <= Width)
                        {
                            graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height);

                            if (currentRegionWidth > 40)
                            {
                                using (var tBrush = new SolidBrush(Color.Turquoise))
                                {
                                    graphics.DrawString(string.Format("{0:0.###} {1}", ((double)currentRegionWidth / _wavePeaks.Header.SampleRate / _zoomFactor), Configuration.Settings.Language.Waveform.Seconds), Font, tBrush, new PointF(currentRegionLeft + 3, Height - 32));
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                DrawBackground(e.Graphics);

                using (var textBrush = new SolidBrush(TextColor))
                {
                    using (var textFont = new Font(Font.FontFamily, 8))
                    {
                        if (Width > 90)
                        {
                            e.Graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(Width / 2 - 65, Height / 2 - 10));
                        }
                        else
                        {
                            using (var stringFormat = new StringFormat())
                            {
                                stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
                                e.Graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(1, 10), stringFormat);
                            }
                        }
                    }
                }
            }
            if (Focused)
            {
                using (var p = new Pen(SelectedColor))
                {
                    e.Graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
                }
            }
        }
        public override void AudioVisualizerPaint() //object sender, PaintEventArgs e)
        {
            InitPaint();

            if (_wavePeaks != null)
            {
                bool showSpectrogram = IsSpectrogramAvailable && ShowSpectrogram;
                bool showSpectrogramOnly = showSpectrogram && !ShowWaveform;
                int waveformHeight = Height - (showSpectrogram ? SpectrogramDisplayHeight : 0);

                // background
                DrawBackground();

                // grid lines
                if (ShowGridLines && !showSpectrogramOnly)
                {
                    DrawGridLines(waveformHeight);
                }

                // spectrogram
                if (showSpectrogram)
                {
                    DrawSpectrogram();
                }

                // waveform
                if (ShowWaveform)
                {
                    var colorNormal = Color.ToCGColor();
                    var colorSelected = SelectedColor.ToCGColor();
                    var isSelectedHelper = new IsSelectedHelper(_allSelectedParagraphs, _wavePeaks.SampleRate);
                    int baseHeight = (int)(_wavePeaks.HighestPeak / VerticalZoomFactor);
                    int halfWaveformHeight = waveformHeight / 2;
                    Func<float, float> calculateY = (value) =>
                    {
                        float offset = (value / baseHeight) * halfWaveformHeight;
                        if (offset > halfWaveformHeight)
                            offset = halfWaveformHeight;
                        if (offset < -halfWaveformHeight)
                            offset = -halfWaveformHeight;
                        return halfWaveformHeight - offset;
                    };
                    for (int x = 0; x < Width; x++)
                    {
                        float pos = (float)RelativeXPositionToSeconds(x) * _wavePeaks.SampleRate;
                        int pos0 = (int)pos;
                        int pos1 = pos0 + 1;
                        if (pos1 >= _wavePeaks.Peaks.Count)
                            break;
                        float pos1Weight = pos - pos0;
                        float pos0Weight = 1F - pos1Weight;
                        var peak0 = _wavePeaks.Peaks[pos0];
                        var peak1 = _wavePeaks.Peaks[pos1];
                        float max = peak0.Max * pos0Weight + peak1.Max * pos1Weight;
                        float min = peak0.Min * pos0Weight + peak1.Min * pos1Weight;
                        float yMax = calculateY(max);
                        float yMin = Math.Max(calculateY(min), yMax + 0.1F);
                        var c = isSelectedHelper.IsSelected(pos0) ? colorNormal : colorSelected;
                        _context.SetStrokeColor(c);
                        DrawLine(x, (int)Math.Round(yMax), x, (int)Math.Round(yMin));
                    }
                }

                // time line
                if (!showSpectrogramOnly)
                {
                    DrawTimeLine(waveformHeight);
                }

                // scene changes
                //                if (_sceneChanges != null)
                //                {
                //                    foreach (double time in _sceneChanges)
                //                    {
                //                        int pos = SecondsToXPosition(time - StartPositionSeconds);
                //                        if (pos > 0 && pos < Width)
                //                        {
                //                            using (var p = new Pen(Color.AntiqueWhite))
                //                                graphics.DrawLine(p, pos, 0, pos, Height);
                //                        }
                //                    }
                //                }

                // current video position
                if (_currentVideoPositionSeconds > 0)
                {
                    int pos = SecondsToXPosition(_currentVideoPositionSeconds - StartPositionSeconds);
                    if (pos > 0 && pos < Width)
                    {
                        _context.SetStrokeColor(Color.Turquoise.ToCGColor());
                        DrawLine(pos, 0, pos, Height);
//                        using (var p = new Pen(Color.Turquoise))
//                            graphics.DrawLine(p, pos, 0, pos, Height);
                    }
                }

                // paragraphs
                foreach (Paragraph p in _displayableParagraphs)
                {
                    if (p.EndTime.TotalSeconds >= StartPositionSeconds && p.StartTime.TotalSeconds <= EndPositionSeconds)
                    {
                        DrawParagraph(p);
                    }
                }

                // current selection
//                if (NewSelectionParagraph != null)
//                {
//                    int currentRegionLeft = SecondsToXPosition(NewSelectionParagraph.StartTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionRight = SecondsToXPosition(NewSelectionParagraph.EndTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionWidth = currentRegionRight - currentRegionLeft;
//                    if (currentRegionRight >= 0 && currentRegionLeft <= Width)
//                    {
//                        using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
//                            graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height);
//
//                        if (currentRegionWidth > 40)
//                        {
//                            using (var brush = new SolidBrush(Color.Turquoise))
//                                graphics.DrawString(string.Format("{0:0.###} {1}", ((double)currentRegionWidth / _wavePeaks.SampleRate / _zoomFactor), Configuration.Settings.Language.Waveform.Seconds), Font, brush, new PointF(currentRegionLeft + 3, Height - 32));
//                        }
//                    }
//                }
            }
            else
            {
                DrawBackground();

                if (ShowGridLines)
                {
                    DrawGridLines(Height);
                }

//                using (var textBrush = new SolidBrush(TextColor))
//                using (var textFont = new Font(Font.FontFamily, 8))
//                {
//                    if (Width > 90)
//                    {
//                        graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(Width / 2 - 65, Height / 2 - 10));
//                    }
//                    else
//                    {
//                        using (var stringFormat = new StringFormat(StringFormatFlags.DirectionVertical))
//                            graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(1, 10), stringFormat);
//                    }
//                }
            }
//            if (Focused)
//            {
//                using (var p = new Pen(SelectedColor))
//                    graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
//            }
    
            // _imageView.Image = _bitmap.ToNSImage();
            _imageView.Image = new NSImage(_context.ToImage(), new CGSize(Width, Height));
            ;

//            public CGImage ToCGImage()
//            {
//                var rawData = new byte[Width * Height * 4];
//                var bytesPerPixel = 4;
//                var bytesPerRow = bytesPerPixel * Width;
//                var bitsPerComponent = 8;
//                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
//                {
//                    using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
//                    {
//                        for (int y = 0; y < Height; y++)
//                        {
//                            for (int x = 0; x < Width; x++)
//                            {
//                                Color c = GetPixel(x, y);
//                                var i = bytesPerRow * y + bytesPerPixel * x;
//                                rawData[i + 0] = c.R;
//                                rawData[i + 1] = c.G;
//                                rawData[i + 2] = c.B;
//                                rawData[i + 3] = c.A;
//                            }
//                        }
//                        //context.Flush();
//
//                        context.SetFillColor(new CGColor(1,0,0, 1));
//                        context.FillRect(new CGRect(0,0, 20, 20));
//
//                        context.SetTextDrawingMode(CGTextDrawingMode.Clip);
//                        context.SetFillColor(new CGColor(0,0,0, 1));
//                        context.ShowTextAtPoint(20, 20, "HEJ");
//                        context.SetTextDrawingMode(CGTextDrawingMode.Fill);
//                        context.ShowTextAtPoint(30, 30, "Yo");
//                        return context.ToImage();
//                    }
//                }
//            }


        }
Ejemplo n.º 4
0
        public override void AudioVisualizerPaint() //object sender, PaintEventArgs e)
        {
            InitPaint();

            if (_wavePeaks != null)
            {
                bool showSpectrogram     = IsSpectrogramAvailable && ShowSpectrogram;
                bool showSpectrogramOnly = showSpectrogram && !ShowWaveform;
                int  waveformHeight      = Height - (showSpectrogram ? SpectrogramDisplayHeight : 0);

                // background
                DrawBackground();

                // grid lines
                if (ShowGridLines && !showSpectrogramOnly)
                {
                    DrawGridLines(waveformHeight);
                }

                // spectrogram
                if (showSpectrogram)
                {
                    DrawSpectrogram();
                }

                // waveform
                if (ShowWaveform)
                {
                    var colorNormal                = Color.ToCGColor();
                    var colorSelected              = SelectedColor.ToCGColor();
                    var isSelectedHelper           = new IsSelectedHelper(_allSelectedParagraphs, _wavePeaks.SampleRate);
                    int baseHeight                 = (int)(_wavePeaks.HighestPeak / VerticalZoomFactor);
                    int halfWaveformHeight         = waveformHeight / 2;
                    Func <float, float> calculateY = (value) =>
                    {
                        float offset = (value / baseHeight) * halfWaveformHeight;
                        if (offset > halfWaveformHeight)
                        {
                            offset = halfWaveformHeight;
                        }
                        if (offset < -halfWaveformHeight)
                        {
                            offset = -halfWaveformHeight;
                        }
                        return(halfWaveformHeight - offset);
                    };
                    for (int x = 0; x < Width; x++)
                    {
                        float pos  = (float)RelativeXPositionToSeconds(x) * _wavePeaks.SampleRate;
                        int   pos0 = (int)pos;
                        int   pos1 = pos0 + 1;
                        if (pos1 >= _wavePeaks.Peaks.Count)
                        {
                            break;
                        }
                        float pos1Weight = pos - pos0;
                        float pos0Weight = 1F - pos1Weight;
                        var   peak0      = _wavePeaks.Peaks[pos0];
                        var   peak1      = _wavePeaks.Peaks[pos1];
                        float max        = peak0.Max * pos0Weight + peak1.Max * pos1Weight;
                        float min        = peak0.Min * pos0Weight + peak1.Min * pos1Weight;
                        float yMax       = calculateY(max);
                        float yMin       = Math.Max(calculateY(min), yMax + 0.1F);
                        var   c          = isSelectedHelper.IsSelected(pos0) ? colorNormal : colorSelected;
                        _context.SetStrokeColor(c);
                        DrawLine(x, (int)Math.Round(yMax), x, (int)Math.Round(yMin));
                    }
                }

                // time line
                if (!showSpectrogramOnly)
                {
                    DrawTimeLine(waveformHeight);
                }

                // scene changes
                //                if (_sceneChanges != null)
                //                {
                //                    foreach (double time in _sceneChanges)
                //                    {
                //                        int pos = SecondsToXPosition(time - StartPositionSeconds);
                //                        if (pos > 0 && pos < Width)
                //                        {
                //                            using (var p = new Pen(Color.AntiqueWhite))
                //                                graphics.DrawLine(p, pos, 0, pos, Height);
                //                        }
                //                    }
                //                }

                // current video position
                if (_currentVideoPositionSeconds > 0)
                {
                    int pos = SecondsToXPosition(_currentVideoPositionSeconds - StartPositionSeconds);
                    if (pos > 0 && pos < Width)
                    {
                        _context.SetStrokeColor(Color.Turquoise.ToCGColor());
                        DrawLine(pos, 0, pos, Height);
//                        using (var p = new Pen(Color.Turquoise))
//                            graphics.DrawLine(p, pos, 0, pos, Height);
                    }
                }

                // paragraphs
                foreach (Paragraph p in _displayableParagraphs)
                {
                    if (p.EndTime.TotalSeconds >= StartPositionSeconds && p.StartTime.TotalSeconds <= EndPositionSeconds)
                    {
                        DrawParagraph(p);
                    }
                }

                // current selection
//                if (NewSelectionParagraph != null)
//                {
//                    int currentRegionLeft = SecondsToXPosition(NewSelectionParagraph.StartTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionRight = SecondsToXPosition(NewSelectionParagraph.EndTime.TotalSeconds - StartPositionSeconds);
//                    int currentRegionWidth = currentRegionRight - currentRegionLeft;
//                    if (currentRegionRight >= 0 && currentRegionLeft <= Width)
//                    {
//                        using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255)))
//                            graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height);
//
//                        if (currentRegionWidth > 40)
//                        {
//                            using (var brush = new SolidBrush(Color.Turquoise))
//                                graphics.DrawString(string.Format("{0:0.###} {1}", ((double)currentRegionWidth / _wavePeaks.SampleRate / _zoomFactor), Configuration.Settings.Language.Waveform.Seconds), Font, brush, new PointF(currentRegionLeft + 3, Height - 32));
//                        }
//                    }
//                }
            }
            else
            {
                DrawBackground();

                if (ShowGridLines)
                {
                    DrawGridLines(Height);
                }

//                using (var textBrush = new SolidBrush(TextColor))
//                using (var textFont = new Font(Font.FontFamily, 8))
//                {
//                    if (Width > 90)
//                    {
//                        graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(Width / 2 - 65, Height / 2 - 10));
//                    }
//                    else
//                    {
//                        using (var stringFormat = new StringFormat(StringFormatFlags.DirectionVertical))
//                            graphics.DrawString(WaveformNotLoadedText, textFont, textBrush, new PointF(1, 10), stringFormat);
//                    }
//                }
            }
//            if (Focused)
//            {
//                using (var p = new Pen(SelectedColor))
//                    graphics.DrawRectangle(p, new Rectangle(0, 0, Width - 1, Height - 1));
//            }

            // _imageView.Image = _bitmap.ToNSImage();
            _imageView.Image = new NSImage(_context.ToImage(), new CGSize(Width, Height));
            ;

//            public CGImage ToCGImage()
//            {
//                var rawData = new byte[Width * Height * 4];
//                var bytesPerPixel = 4;
//                var bytesPerRow = bytesPerPixel * Width;
//                var bitsPerComponent = 8;
//                using (var colorSpace = CGColorSpace.CreateDeviceRGB())
//                {
//                    using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
//                    {
//                        for (int y = 0; y < Height; y++)
//                        {
//                            for (int x = 0; x < Width; x++)
//                            {
//                                Color c = GetPixel(x, y);
//                                var i = bytesPerRow * y + bytesPerPixel * x;
//                                rawData[i + 0] = c.R;
//                                rawData[i + 1] = c.G;
//                                rawData[i + 2] = c.B;
//                                rawData[i + 3] = c.A;
//                            }
//                        }
//                        //context.Flush();
//
//                        context.SetFillColor(new CGColor(1,0,0, 1));
//                        context.FillRect(new CGRect(0,0, 20, 20));
//
//                        context.SetTextDrawingMode(CGTextDrawingMode.Clip);
//                        context.SetFillColor(new CGColor(0,0,0, 1));
//                        context.ShowTextAtPoint(20, 20, "HEJ");
//                        context.SetTextDrawingMode(CGTextDrawingMode.Fill);
//                        context.ShowTextAtPoint(30, 30, "Yo");
//                        return context.ToImage();
//                    }
//                }
//            }
        }