无法在位图上绘制

时间:2011-05-24 06:54:15

标签: c# controls bitmap devexpress mouseevent

我正在使用PictureEdit(DevExpress控件)作为某种形式的孩子。我正在尝试使用MouseEventArgs坐标属性在加载的图像上绘制像素。

private void PictureEditorOnMouseMove(Object sender, MouseEventArgs e)
{            
    if(e.Button == MouseButtons.Left)
    {
        (this.pictureEditor.Image as Bitmap).SetPixel(e.X, e.Y, this.colorPicker.Color);                                    
    }
}

发生ArgumentOutOfRangeException,表示传递给SetPixel方法的x(或y)参数不是正数&&大于给定位图的Height属性。 我以为我使用bitmap.Widthbitmap.Height绑定的坐标。

如何绑定它们?或者我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

private void PictureEditorOnMouseMove(Object sender, MouseEventArgs e)
{   
    if(e.Button == MouseButtons.Left)
    {
        PictureEdit pce = sender as PictureEdit;
        Bitmap bmpImage = pce.Image as Bitmap;
        PictureEditViewInfo viewInfo = pce.GetViewInfo() as PictureEditViewInfo;

        var p = new Point(
            (e.Location.X - viewInfo.PictureStartX) * bmpImage.Width / viewInfo.PictureRect.Width,
            (e.Location.Y - viewInfo.PictureStartY) * bmpImage.Height / viewInfo.PictureRect.Height);

        if (p.X >= 0 && p.X < bmpImage.Width && p.Y >= 0 && p.Y < bmpImage.Height)
        {
            bmpImage.SetPixel(p.X, p.Y, this.colorPicker.Color);
        }
        else
        {
            Console.WriteLine("Out bounds");
        }
    }
}

请注意,您需要将鼠标位置转换为位图的近似像素位置,同时考虑PictureEdit的SizeMode。在样本代码中创建新点时完成此操作。