Mouse drawing causes spaces between pixels

I am creating a drawing application as a test for WPF and all is well. The problem I am facing is that if I draw a pixel under the mouse before the bitmap every time it moves, I get only one pixel per update. When the mouse moves quickly, it does not draw pixels between them. I need to know what is the best way to draw a line between pixels in WPF usingWriteableBitmap

EDIT: I now have this:

LinesNotConnected

+5
source share
2 answers

If you want to draw a line, you should not just change the colors of one pixel at a time, but rather keep the mouse position in each event processing method MouseMove.

( ) a Line . . WriteableBitmap : WPF WriteableBitmap.BackBuffer.

, :).

UPDATE

.

XAML , :

<Window x:Class="SampleWPFApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="520" Loaded="Window_Loaded" PreviewMouseDown="Window_PreviewMouseDown">
<Grid x:Name="layoutRoot" Background="Transparent">
    <Image x:Name="image" />
</Grid>

:

//set width and height of your choice
RenderTargetBitmap bmp = null;
//...
private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //initialize RenderTargetBitmap object
        bmp = new RenderTargetBitmap((int)this.ActualWidth, (int)this.ActualHeight, 90, 90, PixelFormats.Default);

        //set initialized bmp as image source
        image.Source = bmp;
    }

    /// <summary>
    /// Helper method drawing a line.
    /// </summary>
    /// <param name="p1">Start point of the line to draw.</param>
    /// <param name="p2">End point of the line to draw.</param>
    /// <param name="pen">Pen to use to draw the line.</param>
    /// <param name="thickness">Thickness of the line to draw.</param>
    private void DrawLine(Point p1, Point p2, Pen pen, double thickness)
    {
        DrawingVisual drawingVisual = new DrawingVisual();

        using (DrawingContext drawingContext = drawingVisual.RenderOpen())
        {
            //set properties of the Pen object to make the line more smooth
            pen.Thickness = thickness;
            pen.StartLineCap = PenLineCap.Round;
            pen.EndLineCap = PenLineCap.Round;

            //write your drawing code here
            drawingContext.DrawLine(pen, p1, p2);
        }
    }
+3

, , , . .

, UPDATE . , , - , , . , , , → midpoint, midpoint → , → , . .

, -, , .

+2

All Articles