c# - 在图像上绘图

标签 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 的 Inputs.Set 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;

最佳答案

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

而且我认为它与鼠标的 DPI 相关...对此您无能为力。

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

WriteableBitmapExtensions.DrawLine

关于c# - 在图像上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822198/

相关文章:

c# - 在模态对话框上显示 MessageBox 后,ALT + TAB 不起作用

c# - 从 InkCanvas 容器将描边区域裁剪为位图

javascript - 在图像预览中选择“可拖动”

c# - 如何优化以下 linq to object 查询

c# - gridview 滚动到顶部

c# - 在 C# : WPF vs. Winforms 中显示来自原始未压缩数据源的实时视频

c# - WPF - 将 F 键分配给按钮

c# - 从 ip-cam 获取快照的最快方法是什么

image - 缩放时使电影中的图像成比例?

c# - GUID 比较中的性能