问题
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:
using
CreateGraphicswill not create persistent graphics. So: Alwas draw everything in thePaintevent of thePictureBox! TheMouseMoveshould only collect the points in aList<Point>or aList<List<Point>>. All drawing must happen in thePaintevent using itse.Graphicsobject!The drawing on the surface only gets saved if you use
DrawToBitmapto create a new bitmap, which will combine up to three layers: theBackgroundImage, theImageand 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