Scratch Image on PictureBox C#

谁说我不能喝 提交于 2019-12-25 09:04:52

问题


I coded this to scratch image on 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));  
    }

But when I saved image using this:

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;
                    }
                }

The image scratched on picturebox was not scratched.

This is scratched image on picturebox before saving:

But when I saved using above code, the scratches were gone missing:

How to saved the picture along with the scratches?, Thank you very much.


回答1:


Two common errors:

  1. using CreateGraphics will not create persistent graphics. So: Alwas draw everything in the Paint event of the PictureBox! The MouseMove should only collect the points in a List<Point> or a List<List<Point>>. All drawing must happen in the Paint event using its e.Graphics object!

  2. The drawing on the surface only gets saved if you use DrawToBitmap to create a new bitmap, which will combine up to three layers: the BackgroundImage, the Image and all persistent drawings.

See here for an example of drawing and here for an example of using DrawToBitmap!

As an alternative to this recommended way of drawing over an Image you can also draw directly into it. This is what are actually doing in the MouseMove, but later you ignore the image and draw it in a non-persistent way.

Finally: The Save routine is saving a bmp whichyou have not shown us, so we can only conclude that it is the original Image you loaded.. Maybe drawing.Save(..) would help..

But there are so many issues with the code, that you really should do a complete rewrite, starting with the confusing names! (Hint: do not call an object the name of a different type, like Graphics panel!!!) And obviously a variable of type Pen or Color would also help avoiding those repetitions...



来源:https://stackoverflow.com/questions/38821738/scratch-image-on-picturebox-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!