PictureBox C#上的划痕图像

时间:2016-08-08 05:17:39

标签: c# visual-studio image-processing picturebox

我将其编码为图片框上的划痕图片。

bool draw = false;

int pX = -1;
int pY = -1;

Bitmap drawing;

public Form1()
{
    drawing = new Bitmap(transformedImage.Width, transformedImage.Height, transformedImage.CreateGraphics());
    Graphics.FromImage(drawing).Clear(Color.Transparent);
}

private void transformedImage_MouseMove(object sender, MouseEventArgs e)
    {
        if (draw)
        {
            int penWidth = Convert.ToInt32(penWidthValue.Value);
            if(blackCheck.Checked == true) ///black pen
            {
                Graphics panel = Graphics.FromImage(drawing);

                Pen pen = new Pen(Color.Black, penWidth);

                pen.EndCap = LineCap.Round;
                pen.StartCap = LineCap.Round;

                panel.DrawLine(pen, pX, pY, e.X, e.Y);

                transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));                    
            }
            else if(redCheck.Checked == true) /// red pen
            {
                Graphics panel = Graphics.FromImage(drawing);

                Pen pen = new Pen(Color.Red, penWidth);

                pen.EndCap = LineCap.Round;
                pen.StartCap = LineCap.Round;

                panel.DrawLine(pen, pX, pY, e.X, e.Y);

                transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
            }
            else if(yellowCheck.Checked == true) /// yellow
            {
                Graphics panel = Graphics.FromImage(drawing);

                Pen pen = new Pen(Color.Yellow, penWidth);

                pen.EndCap = LineCap.Round;
                pen.StartCap = LineCap.Round;

                panel.DrawLine(pen, pX, pY, e.X, e.Y);

                transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
            }
            else /// green
            {
                Graphics panel = Graphics.FromImage(drawing);

                Pen pen = new Pen(Color.Green, penWidth);

                pen.EndCap = LineCap.Round;
                pen.StartCap = LineCap.Round;

                panel.DrawLine(pen, pX, pY, e.X, e.Y);

                transformedImage.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
            }

        }

        pX = e.X;
        pY = e.Y;
    }

    private void transformedImage_MouseDown(object sender, MouseEventArgs e)
    {
        if (scratchCheck.Checked == true)
        {
            draw = true;
            pX = e.X;
            pY = e.Y;
        }
    }

    private void transformedImage_MouseUp(object sender, MouseEventArgs e)
    {
        draw = false;
    }

    private void transformedImage_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImageUnscaled(drawing, new Point(0, 0));  
    }

但是当我用这个保存图像时:

SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "Select Save Location";
                sfd.InitialDirectory = @"Save_Path";
                sfd.AddExtension = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    switch (Path.GetExtension(sfd.FileName).ToUpper())
                    {
                        case ".BMP":
                            bmp.Save(sfd.FileName, ImageFormat.Bmp);
                            break;
                        case ".gif":
                            bmp.Save(sfd.FileName, ImageFormat.Gif);
                            break;
                        case ".JPG":
                            bmp.Save(sfd.FileName, ImageFormat.Jpeg);
                            break;
                        case ".JPEG":
                            bmp.Save(sfd.FileName,ImageFormat.Jpeg);
                            break;
                        case ".PNG":
                            bmp.Save(sfd.FileName, ImageFormat.Png);
                            break;
                        case ".png":
                            bmp.Save(sfd.FileName, ImageFormat.Png);
                            break;
                        default:
                            break;
                    }
                }

在图片框上划伤的图像没有刮伤。

在保存之前,这是图片框上的划痕图像: enter image description here

但是当我使用上面的代码保存时,划痕就不见了: enter image description here

如何保存图片以及划痕?非常感谢。

1 个答案:

答案 0 :(得分:1)

两个常见错误:

  1. 使用CreateGraphics不会创建持久性图形。所以:Alwas在Paint的{​​{1}}事件中绘制了所有内容! PictureBox只应收集 MouseMoveList<Point>中的积分。 所有绘图必须使用List<List<Point>>对象在Paint事件中发生!

  2. 如果您使用e.Graphics创建新的位图,则表面上的绘图只会保存,该位图最多可组合三层DrawToBitmapBackgroundImage和所有持久性图纸。

  3. 有关绘图的示例,请参见here;有关使用Image的示例,请参见here

    作为这种推荐的DrawToBitmap绘图方式的替代方法,您也可以直接绘制它。这是Image中实际执行的操作,但稍后您将忽略该图像并以非持久方式绘制它。

    最后:MouseMove例程正在保存您未向我们展示的Save,因此我们只能断定它是您加载的原始bmp ..也许{{1}会帮助..

    但代码有很多问题,你真的应该做一个完整的重写,从令人困惑的名字开始! (提示:不要将对象称为其他类型的名称,如Image !!!)显然,drawing.Save(..)Graphics panel类型的变量也有助于避免重复这些...