在图像上绘图

时间:2014-05-23 06:16:22

标签: c# wpf image

我可以使用以下代码在图像上绘图。但我的问题是,这不是一条连续的路线。看起来好像坏了。有关更好的理解,请参见下图。

我的XAML:

<Grid x:Name="Gridimage1" Grid.Column="0">
  <Image Name="image1" HorizontalAlignment="Left" VerticalAlignment="Top"  Stretch="Fill" >
  </Image>
</Grid>

我的c#代码:

  #region "Drawing on image"

    static WriteableBitmap writeableBitmap;
    static Image i;

    public void DrawingOnImage() // this function will be called after image load 
    {
        if (image1.Source != null)
        {
            i = new Image();
            RenderOptions.SetBitmapScalingMode(image1, BitmapScalingMode.NearestNeighbor);
            RenderOptions.SetEdgeMode(image1, EdgeMode.Aliased);
            BitmapSource BitmapSrc = new FormatConvertedBitmap(image1.Source as BitmapSource, PixelFormats.Default, null, 0);
            //writeableBitmap = new WriteableBitmap((int)image1.ActualWidth, (int)image1.ActualHeight, 96, 96, PixelFormats.Bgr32, null);
            writeableBitmap = new WriteableBitmap(BitmapSrc); 
            image1.Source = writeableBitmap;
            //image1.Stretch = Stretch.None;
            image1.HorizontalAlignment = HorizontalAlignment.Left;
            image1.VerticalAlignment = VerticalAlignment.Top;
            i = image1;
            image1.MouseMove += new MouseEventHandler(i_MouseMove);
            image1.MouseLeftButtonDown +=
            new MouseButtonEventHandler(i_MouseLeftButtonDown);
            image1.MouseRightButtonDown +=
            new MouseButtonEventHandler(i_MouseRightButtonDown);

        }
    }

    static void i_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DrawPixel(e);
    }

    static void i_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DrawPixel(e);
        }
    }

    static void DrawPixel(MouseEventArgs e)
    {
        double CR = i.Source.Height / i.ActualHeight;
        double RR = i.Source.Width / i.ActualWidth;
        int column = (int)(e.GetPosition(i).X * RR) ;
        int row = (int)(e.GetPosition(i).Y * CR);

        // Reserve the back buffer for updates.
        writeableBitmap.Lock();
        unsafe
        {
            // Get a pointer to the back buffer. 
            int pBackBuffer = (int)writeableBitmap.BackBuffer;

            // Fin d the address of the pixel to draw.
            pBackBuffer += row * writeableBitmap.BackBufferStride;
            pBackBuffer += column * 4;

            // Compute the pixel's color. 
            int color_data = 255 << 16; // R
            color_data |= 128 << 8;   // G
            color_data |= 255 << 0;   // B 

            // Assign the color data to the pixel.
            *((int*)pBackBuffer) = color_data;
        }

        // Specify the area of the bitmap that changed.
        writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));

        // Release the back buffer and make it available for display.
        writeableBitmap.Unlock();
    }

    #endregion

图片输出:

你可以看到我绘制的线条(粉红色)。它不是一条连续的线。我哪里失败了?

enter image description here

更新:
我在@ loxxy的输入之后的发现。将oldx,oldy初始化为零。

   if (oldx == 0 && oldy == 0)
   {
     writeableBitmap.DrawLineAa(column, row, column, row, SelectedColor);              
   }
   else
   {
     if (Math.Abs(oldx - column) > 10 || Math.Abs(oldy - row) > 10)
      {
          writeableBitmap.DrawLineAa(column, row, column, row, SelectedColor);
      }
      else
      {
          writeableBitmap.DrawLineAa(column, row, oldx, oldy, SelectedColor);                
      }

    }
    oldx = column;
    oldy = row;

2 个答案:

答案 0 :(得分:2)

由于您是逐像素绘制的,因此鼠标必须以更快的速度更新坐标。

而且我认为这与鼠标的DPI有关......你无能为力。

所以请尝试画一条线。在WPF中是这样的:

WriteableBitmapExtensions.DrawLine

答案 1 :(得分:0)

您收到的鼠标事件不会返回连续点数。如果快速移动鼠标,返回的点将不会彼此相邻。如果要在鼠标光标下绘制一条线,则需要从每个MouseMove或MouseButtonDown回调中绘制到当前点的上一个点绘制一条线。